将wcf服务托管到网站问题:System.ArgumentException:ServiceHost仅支持类服务类型

gig*_*igi 8 .net wcf

我有这样的事情:

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>
Run Code Online (Sandbox Code Playgroud)

如果我运行这个,我可以看到WCF测试客户端,一切正常.

现在我想将此服务托管到IIS中,因此我创建了一个网站并添加了对它的引用MathServiceLibrary.

我有这个 ms.svc

<%@ ServiceHost Language="C#" Debug="true" 
    Service="MathServiceLibrary.IMathService" %> 
Run Code Online (Sandbox Code Playgroud)

还有这个 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>
Run Code Online (Sandbox Code Playgroud)

当我ms.svc在浏览器中右键单击视图时,我得到:

描述:执行当前Web请求期间发生未处理的异常.请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息.

异常详细信息:System.ArgumentException:ServiceHost仅支持类服务类型.

来源错误:

在执行当前Web请求期间生成了未处理的异常.可以使用下面的异常堆栈跟踪来识别有关异常的起源和位置的信息.

堆栈跟踪:

[ArgumentException:ServiceHost仅支持类服务类型.]
System.ServiceModel.Description.ServiceDescription.GetService(Type serviceType)+12229075
System.ServiceModel.ServiceHost.CreateDescription(IDictionary`2&implementedContracts)+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(Type serviceType,Uri [] baseAddresses)+30
System.ServiceModel.Activation.ServiceHostFactory.CreateServiceHost(String constructorString,Uri [] baseAddresses)+420
System.ServiceModel.HostingManager.CreateService(String normalizedVirtualPath)+1440
System.ServiceModel.HostingManager.ActivateService( String normalizedVirtualPath)+44
System.ServiceModel.HostingManager.EnsureServiceA vailable(String normalizedVirtualPath)+615

[ServiceActivationException:由于编译期间发生异常,无法激活服务'/MathWebSite/ms.svc'.异常消息是:ServiceHost仅支持类服务类型.]
System.Runtime.AsyncResult.End(IAsyncResult result)+679246
System.ServiceModel.Activation.HostedHttpRequestAsyncResult.End(IAsyncResult result)+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&completedSynchronously)+75

我无法弄清楚我错过了什么.

Pav*_*van 14

更改您的ms.svc如下

<%@ ServiceHost Language="C#" Debug="true" Service="MathServiceLibrary.MathService" %    > 
Run Code Online (Sandbox Code Playgroud)

您必须提供类名而不是接口名