我在一个名为fn34-webservice的类库类型项目中创建了一个非常简单的服务。
这是服务的接口和类(它们位于不同的文件中):
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>
当我运行它并尝试访问网址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文件中可能有些愚蠢,但我无法找到它。
非常感谢!
答案 0 :(得分:0)
我认为你的服务使用了错误的完全限定名称。
在您的配置文件中,您有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>