2012-01-24 32 views
7

我有這樣的事情:託管的WCF服務爲一體的網站問題信息:System.ArgumentException:ServiceHost的只支持類服務類型

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瀏覽器查看我得到這個:

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

我想不通我失去了什麼。

回答

13

更改ms.svc如下

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

你必須給類的名稱,而不是接口名

+0

謝謝,它的工作原理。 – gigi

3

你.svc文件是錯誤的。它引用了接口,而不是實現。將其更改爲: <%@ ServiceHost Language="C#" Debug="true" Service="MathServiceLibrary.MathService" %>

+0

謝謝,它的工作原理。 – gigi

+0

我在另一個項目上執行了我的實現,而不是在同一個命名空間中,是否有任何問題? – kbvishnu

3

svc文件需要具有類名稱而不是接口名稱。示例svc文件具有以下內容:

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

希望有所幫助。

3

在SVC文件中的條目是錯誤的:

代替:

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

你需要有:

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

您需要定義服務實現類Service=屬性 - 不是服務合同!