【发布时间】:2012-01-24 14:15:12
【问题描述】:
我有这样的事情:
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(类型 服务类型)+12229075
System.ServiceModel.ServiceHost.CreateDescription(IDictionary`2& 实施合同)+55
System.ServiceModel.ServiceHostBase.InitializeDescription(UriSchemeKeyedCollection 基本地址)+154
System.ServiceModel.ServiceHost.InitializeDescription(Type serviceType, UriSchemeKeyedCollection baseAddresses) +49
System.ServiceModel.ServiceHost..ctor(Type serviceType, Uri[] baseAddresses) +151
System.ServiceModel.Activation.ServiceHostFactory.CreateServiceHost(类型 serviceType, Uri[] baseAddresses) +30
System.ServiceModel.Activation.ServiceHostFactory.CreateServiceHost(字符串 构造函数字符串,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 结果) +679246
System.ServiceModel.Activation.HostedHttpRequestAsyncResult.End(IAsyncResult 结果)+190
System.ServiceModel.Activation.HostedHttpRequestAsyncResult.ExecuteSynchronous(HttpApplication 上下文,字符串 routeServiceVirtualPath,布尔值 flowContext,布尔值 确保WFService) +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& completedSynchronously) +75
我无法弄清楚我错过了什么。
【问题讨论】: