尝试访问 WCF 服务时出错
Error when trying to access WCF service
我在名为 fn34-webservice 的 Class 库类型项目中创建了一个非常简单的服务。
这是服务的接口和 class(它们在不同的文件中):
namespace fn34_webservice
{
[ServiceContract]
public interface IEstoqueWS
{
[OperationContract]
ItemEstoque GetQuantidade(string codigo);
}
}
namespace fn34_webservice
{
public class EstoqueWS : IEstoqueWS
{
private Dictionary<string, ItemEstoque> repositorio = new Dictionary<string, ItemEstoque>();
public EstoqueWS()
{
repositorio.Add("SOA", new ItemEstoque() { Codigo = "SOA", Quantidade = 5 });
repositorio.Add("TDD", new ItemEstoque() { Codigo = "TDD", Quantidade = 1 });
repositorio.Add("RES", new ItemEstoque() { Codigo = "RES", Quantidade = 2 });
repositorio.Add("LOG", new ItemEstoque() { Codigo = "LOG", Quantidade = 4 });
repositorio.Add("WEB", new ItemEstoque() { Codigo = "WEB", Quantidade = 1 });
repositorio.Add("ARQ", new ItemEstoque() { Codigo = "ARQ", Quantidade = 2 });
}
public ItemEstoque GetQuantidade(string codigo)
{
return repositorio[codigo];
}
}
}
因此,我创建了这个简单的空网站来托管服务器并在本地进行测试。我添加了对我的项目 fn34-webservice 的引用,并在网站的 web.config 中添加了以下配置:
<system.serviceModel>
<serviceHostingEnvironment >
<serviceActivations>
<add factory="System.ServiceModel.Activation.ServiceHostFactory"
relativeAddress="./WsHost/fn34-webservice.svc"
service="fn34-webservice.fn34-webservice"/>
</serviceActivations>
</serviceHostingEnvironment>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
当我 运行 它并尝试访问 url http://localhost:49487/WsHost/fn34-webservice.svc
时,我收到以下错误:
The type 'fn34-webservice.fn34-webservice', provided as the Service attribute value in the ServiceHost directive, or provided in the configuration element system.serviceModel/serviceHostingEnvironment/serviceActivations could not be found.
我的猜测是它可能在我的 web.config 文件中有些愚蠢,但我无法找到它。
非常感谢!
我认为您为服务使用了错误的完全限定名称。
在您的配置文件中有 fn34-webservice.fn34-webservice
。但是,您的 Web 服务实际上名为 EstoqueWS
,命名空间为 fn34_webservice
(注意 _
)。
尝试更新您的配置文件以使用完全限定名称 fn34_webservice.EstoqueWS
,如下所示:
<serviceActivations>
<add factory="System.ServiceModel.Activation.ServiceHostFactory"
relativeAddress="./WsHost/fn34-webservice.svc"
service="fn34_webservice.EstoqueWS"/>
</serviceActivations>
我在名为 fn34-webservice 的 Class 库类型项目中创建了一个非常简单的服务。
这是服务的接口和 class(它们在不同的文件中):
namespace fn34_webservice
{
[ServiceContract]
public interface IEstoqueWS
{
[OperationContract]
ItemEstoque GetQuantidade(string codigo);
}
}
namespace fn34_webservice
{
public class EstoqueWS : IEstoqueWS
{
private Dictionary<string, ItemEstoque> repositorio = new Dictionary<string, ItemEstoque>();
public EstoqueWS()
{
repositorio.Add("SOA", new ItemEstoque() { Codigo = "SOA", Quantidade = 5 });
repositorio.Add("TDD", new ItemEstoque() { Codigo = "TDD", Quantidade = 1 });
repositorio.Add("RES", new ItemEstoque() { Codigo = "RES", Quantidade = 2 });
repositorio.Add("LOG", new ItemEstoque() { Codigo = "LOG", Quantidade = 4 });
repositorio.Add("WEB", new ItemEstoque() { Codigo = "WEB", Quantidade = 1 });
repositorio.Add("ARQ", new ItemEstoque() { Codigo = "ARQ", Quantidade = 2 });
}
public ItemEstoque GetQuantidade(string codigo)
{
return repositorio[codigo];
}
}
}
因此,我创建了这个简单的空网站来托管服务器并在本地进行测试。我添加了对我的项目 fn34-webservice 的引用,并在网站的 web.config 中添加了以下配置:
<system.serviceModel>
<serviceHostingEnvironment >
<serviceActivations>
<add factory="System.ServiceModel.Activation.ServiceHostFactory"
relativeAddress="./WsHost/fn34-webservice.svc"
service="fn34-webservice.fn34-webservice"/>
</serviceActivations>
</serviceHostingEnvironment>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
当我 运行 它并尝试访问 url http://localhost:49487/WsHost/fn34-webservice.svc
时,我收到以下错误:
The type 'fn34-webservice.fn34-webservice', provided as the Service attribute value in the ServiceHost directive, or provided in the configuration element system.serviceModel/serviceHostingEnvironment/serviceActivations could not be found.
我的猜测是它可能在我的 web.config 文件中有些愚蠢,但我无法找到它。
非常感谢!
我认为您为服务使用了错误的完全限定名称。
在您的配置文件中有 fn34-webservice.fn34-webservice
。但是,您的 Web 服务实际上名为 EstoqueWS
,命名空间为 fn34_webservice
(注意 _
)。
尝试更新您的配置文件以使用完全限定名称 fn34_webservice.EstoqueWS
,如下所示:
<serviceActivations>
<add factory="System.ServiceModel.Activation.ServiceHostFactory"
relativeAddress="./WsHost/fn34-webservice.svc"
service="fn34_webservice.EstoqueWS"/>
</serviceActivations>