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

标签 .net wcf

我有这样的事情:

数学服务库 (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在浏览器中查看我得到这个:

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: ServiceHost only supports class service types.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[ArgumentException: ServiceHost only supports class service types.]
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(Type serviceType, UriSchemeKeyedCollection baseAddresses) +49
System.ServiceModel.ServiceHost..ctor(Type 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.EnsureServiceAvailable(String normalizedVirtualPath) +615

[ServiceActivationException: The service '/MathWebSite/ms.svc' cannot be activated due to an exception during compilation. The exception message is: ServiceHost only supports class service types..]
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



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

最佳答案

如下更改您的 ms.svc

<%@ ServiceHost Language="C#" Debug="true" Service="MathServiceLibrary.MathService" %    > 

您必须给出类名而不是接口(interface)名

关于.net - 将 wcf 服务托管到网站问题 : System. ArgumentException:ServiceHost 仅支持类服务类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8988300/

相关文章:

c# - 是否可以创建一些 IGrouping 对象

c# - 表拆分 EF Core

java - 反射和远程处理在内部如何工作?

c# - 使用 Dispatcher 切换到 UI 线程的正确语法

WCF发送大量数据

c# - 死简单的 WCF - 405 方法不允许

c# - 设计问题 - 用 C# 更新多行 - 关注多次往返性能

asp.net - 如何在 REST Web 服务中处理使用 100 Continue?

c# - WCF 到数据库连接

wcf - BasicHttpBinding 与 TransportWithMessageCredential 和 clientCredentialType ="Windows"