2012-01-24 12 views
7

Tôi có một cái gì đó như thế này:Hosting một dịch vụ WCF thành một vấn đề trang web: System.ArgumentException: ServiceHost chỉ hỗ trợ loại hình dịch vụ lớp

MathServiceLibrary (Thư viện dịch vụ 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> 

Nếu tôi chạy điều này tôi có thể thấy WCF Test Client và mọi thứ đều ổn.

Bây giờ tôi muốn lưu trữ dịch vụ này vào IIS vì vậy tôi tạo một trang web và thêm tham chiếu đến MathServiceLibrary.

Tôi có điều này ms.svc

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

và điều này 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> 

Khi tôi bấm chuột phải vào ms.svc xem trong trình duyệt của tôi có được điều này:

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

tôi không thể hình những gì tôi còn thiếu.

Trả lời

13

Thay đổi ms.svc của bạn như sau

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

Bạn cần phải cung cấp cho các tên lớp thay cho tên giao diện

+0

cảm ơn, nó hoạt động. – gigi

3

tập tin .svc của bạn là sai. Nó tham chiếu đến giao diện chứ không phải thực hiện. Thay đổi nó thành: <%@ ServiceHost Language="C#" Debug="true" Service="MathServiceLibrary.MathService" %>

+0

cảm ơn, nó hoạt động. – gigi

+0

Tôi đã thực hiện dự án của mình trên một dự án khác, không phải trong cùng một không gian tên, có vấn đề gì không? – kbvishnu

3

Tệp svc cần phải có tên lớp thay vì tên giao diện. Tệp svc mẫu có nội dung bên dưới:

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

Hy vọng điều đó sẽ hữu ích.

3

Phần thêm vào tập tin svc của bạn là sai:

thay vì:

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

bạn cần phải có:

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

Bạn cần phải xác định việc thực hiện lớp dịch vụ trong thuộc tính Service= - NOT hợp đồng dịch vụ!

Các vấn đề liên quan