Wcf service hosting in website issue: System.ArgumentException: ServiceHost only supports class service types

I have something like this:

MathServiceLibrary (WCF Service Library)

[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> 

If I run this, I see the WCF test client, and everything is fine.

Now I want to host this service in IIS in order to create a website and add a link to the MathServiceLibrary .

I have this ms.svc

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

and 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> 

When I right click on ms.svc in the browser, I get the following:

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

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

Source Error:

An unhandled exception was thrown during the execution of the current web request. Information about 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: service /MathWebSite/ms.svc cannot be activated due to an exception at compile time. exception message: ServiceHost only supports class service types.]
System.Runtime.AsyncResult.End (result of IAsyncResult) +679246
System.ServiceModel.Activation.HostedHttpRequestAsyncResult.End (IAsyncResult result) +190
System.ServiceModel.Activation.HostedHttpRequestAsyncResult.ExecuteSynchronous (HttpApplication context, String routeServiceVirtualPath, Boolean flowContext, Boolean provide WFS service) +234
System.ServiceModel.Activation.HttpModule.ProcessRequest (object sender, EventArgs e) +355
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute () +148
System.Web.HttpApplication.ExecuteStep (step IExecutionStep, Boolean & completed synchronously) +75

I can’t understand what is missing.

+7
source share
4 answers

Change your ms.svc as below

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

You must specify the class name instead of the interface name

+13
source

Your .svc file is invalid. It refers to an interface, not an implementation. Change it like this: <%@ ServiceHost Language="C#" Debug="true" Service="MathServiceLibrary.MathService" %>

+3
source

The svc file should have a class name, not an interface name. The sample svc file has the following contents:

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

Hope this helps.

+3
source

The entry in the svc file is incorrect:

instead:

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

you need:

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

You need to define the service implementation class in the attribute Service= - NOT a service contract!

+3
source

All Articles