我有这样的事情:
MathServiceLibrary (WCF服务库)
[ServiceContract]
public interface IMathService
{
[OperationContract]
int Add(int x, int y);
[OperationContract]
int Multiply(int x, int y);
}
public class MathService : IMathService
{
public int Add(int x, int y)
{
return x + y;
}
public int Multiply(int x, int y)
{
return x * y;
}
}
<behaviors>
<serviceBehaviors>
<behavior name="defaultServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="defaultServiceBehavior"
name="MathServiceLibrary.MathService">
<endpoint
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
<endpoint
address="math"
binding="wsHttpBinding"
contract="MathServiceLibrary.IMathService" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080/" />
</baseAddresses>
</host>
</service>
</services>
如果我运行这个,我可以看到WCF测试客户端,一切正常。
现在我想将此服务托管到IIS中,因此我创建了一个网站并添加了对MathServiceLibrary
的引用。
我有ms.svc
<%@ ServiceHost Language="C#" Debug="true"
Service="MathServiceLibrary.IMathService" %>
和web.config
<system.serviceModel>
<services>
<service behaviorConfiguration="defaultServiceBehavior" name="MathServiceLibrary.MathService">
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
<endpoint address="" binding="wsHttpBinding" contract="MathServiceLibrary.IMathService">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="defaultServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
当我在浏览器中右键单击ms.svc
视图时,我得到了这个:
描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪了解更多信息 有关错误的信息以及它在代码中的起源。
异常详细信息:System.ArgumentException:ServiceHost仅支持类服务类型。
来源错误:
在执行当前Web请求期间生成了未处理的异常。有关的来源和位置的信息 可以使用下面的异常堆栈跟踪来识别异常。
堆栈追踪:
[ArgumentException:ServiceHost仅支持类服务类型。]
System.ServiceModel.Description.ServiceDescription.GetService(类型 serviceType)+12229075
System.ServiceModel.ServiceHost.CreateDescription(IDictionary`2&安培; 已实施的合同)+55
System.ServiceModel.ServiceHostBase.InitializeDescription(UriSchemeKeyedCollection baseAddresses)+154
System.ServiceModel.ServiceHost.InitializeDescription(类型serviceType,UriSchemeKeyedCollection baseAddresses)+49
System.ServiceModel.ServiceHost..ctor(类型serviceType,Uri [] baseAddresses)+151
System.ServiceModel.Activation.ServiceHostFactory.CreateServiceHost(类型 serviceType,Uri [] baseAddresses)+30
System.ServiceModel.Activation.ServiceHostFactory.CreateServiceHost(字符串 constructorString,Uri [] baseAddresses)+420
System.ServiceModel.HostingManager.CreateService(String normalizedVirtualPath)+1440
System.ServiceModel.HostingManager.ActivateService(String normalizedVirtualPath)+44
System.ServiceModel.HostingManager.EnsureServiceAvailable(字符串 normalizedVirtualPath)+615[ServiceActivationException:由于编译期间发生异常,无法激活服务'/MathWebSite/ms.svc'。该 异常消息是:ServiceHost仅支持类服务类型。]
System.Runtime.AsyncResult.End(IAsyncResult result)+679246
System.ServiceModel.Activation.HostedHttpRequestAsyncResult.End(IAsyncResult的 结果)+190
System.ServiceModel.Activation.HostedHttpRequestAsyncResult.ExecuteSynchronous(HttpApplication的 context,String routeServiceVirtualPath,Boolean flowContext,Boolean ensureWFService)+234
System.ServiceModel.Activation.HttpModule.ProcessRequest(Object sender,EventArgs e)+355
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +148
System.Web.HttpApplication.ExecuteStep(IExecutionStep step,Boolean&amp; completedSynchronously)+75
我无法弄清楚我错过了什么。
答案 0 :(得分:13)
将ms.svc更改为
<%@ ServiceHost Language="C#" Debug="true" Service="MathServiceLibrary.MathService" % >
您必须提供类名而不是接口名
答案 1 :(得分:3)
您的.svc文件错误。它引用了接口,而不是实现。把它改成这个:
<%@ ServiceHost Language="C#" Debug="true" Service="MathServiceLibrary.MathService" %>
答案 2 :(得分:3)
svc文件需要具有类名而不是接口名。示例svc文件具有以下内容:
<%@ ServiceHost Language="C#" Debug="true" Service="SampleService.Service1" CodeBehind="Service1.svc.cs" %>
希望有所帮助。
答案 3 :(得分:3)
您的svc文件中的条目错误:
而不是:
<%@ ServiceHost Language="C#" Debug="true"
Service="MathServiceLibrary.IMathService" %>
你需要:
<%@ ServiceHost Language="C#" Debug="true"
Service="MathServiceLibrary.MathService" %>
您需要在Service=
属性 - NOT 服务合同中定义服务实施类!