ServiceHost directive error

By | September 7, 2011

Today while running a unit test to test a WCF service I started to get an exception which stopped me from running the unit test. I decided to look in the Windows event log and found several entries like this:

WebHost failed to process a request.

Sender Information: System.ServiceModel.ServiceHostingEnvironment+HostingManager/12036987

Exception: System.ServiceModel.ServiceActivationException: The service ‘/LeadManagerSvc.svc’ cannot be activated due to an exception during compilation.  The exception message is: The type ‘Crm.Service.Prototype.LeadManagerSvc’, provided as the Service attribute value in the ServiceHost directive, or provided in the configuration element system.serviceModel/serviceHostingEnvironment/serviceActivations could not be found.. —> System.InvalidOperationException: The type ‘Crm.Service.Prototype.LeadManagerSvc’, provided as the Service attribute value in the ServiceHost directive, or provided in the configuration element system.serviceModel/serviceHostingEnvironment/serviceActivations could not be found.

at System.ServiceModel.Activation.ServiceHostFactory.CreateServiceHost(String constructorString, Uri[] baseAddresses)

at System.ServiceModel.ServiceHostingEnvironment.HostingManager.

CreateService(String normalizedVirtualPath)

at System.ServiceModel.ServiceHostingEnvironment.HostingManager.

ActivateService(String normalizedVirtualPath)

at System.ServiceModel.ServiceHostingEnvironment.HostingManager.

EnsureServiceAvailable(String normalizedVirtualPath)

— End of inner exception stack trace —

at System.ServiceModel.ServiceHostingEnvironment.HostingManager.

EnsureServiceAvailable(String normalizedVirtualPath)

at System.ServiceModel.ServiceHostingEnvironment.

EnsureServiceAvailableFast(String relativeVirtualPath)

Process Name: WebDev.WebServer40

Process ID: 3892

After spending quite some time unsuccessfully trying to understand the meaning of this error and how to fix the problem I decided to run a full search on the entire solution for “ServiceHost”. As soon as the search result came up I realized what the problem was.

While doing some refactoring of the service I renamed its namespace without renaming the namespace in the service host file (extension <service name>.svc).  This file contains the following line “

The namespace in the parameter “Service” has to match the namespace in the code behind file.

The whole solution will build without any errors but you will get the exception above when you try calling the service.

Leave a Reply