【问题标题】:Hosting an wcf service into a website issue : System.ArgumentException: ServiceHost only supports class service types将 wcf 服务托管到网站问题:System.ArgumentException: ServiceHost 仅支持类服务类型
【发布时间】: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

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

【问题讨论】:

    标签: .net wcf


    【解决方案1】:

    如下更改你的 ms.svc

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

    你必须给出类名而不是接口名

    【讨论】:

      【解决方案2】:

      您的 .svc 文件有误。它引用接口,而不是实现。将其更改为: &lt;%@ ServiceHost Language="C#" Debug="true" Service="MathServiceLibrary.MathService" %&gt;

      【讨论】:

      • 我在另一个项目上实现了,不在同一个命名空间中,有什么问题吗?
      【解决方案3】:

      svc 文件需要有类名而不是接口名。一个示例 svc 文件具有以下内容:

      <%@ ServiceHost Language="C#" Debug="true" Service="SampleService.Service1" CodeBehind="Service1.svc.cs" %>
      

      希望对您有所帮助。

      【讨论】:

        【解决方案4】:

        你的svc文件中的条目是错误的:

        代替:

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

        你需要:

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

        您需要在Service= 属性中定义服务实现类 - 不是服务契约!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-04-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-07-14
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多