If you’d like to hide WCF implementation class behind an interface to simplify future changes you should deal with some IoC. I use Autofac and have several times struggled upon the same point when trying to configure WCF to work with it. This trouble begins when you end up with failed service and message “The service configured for WCF is not registered with the Autofac container”.
Here I’d like to describe which points should be taken into account when configuring the service, but before that I’d like to add more context about using of the service.
The service is host in WAS. On the picture below you can see how the service is consumed by an application. The answer to question “why is it so?” lays out of the text scope, but I’d like to point out that Autofac configuration for WCF is located only in the WCF package.
Now you can find error which might appear during configuration and steps to solve them.
Metadata publishing for this service is currently disabled
This message most likely means you don’t have correct entry in web.config file. You should check that it has the following:
<services> <service name="WCF.Service"> <endpoint address="" binding="basicHttpBinding" contract="Contracts.IService" /> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> <host> <baseAddresses> <add baseAddress="http://localhost/" /> </baseAddresses> </host> </service> </services>
So WCF.Service represent the full service class name. Contracts.IService represents the full interface name. This entry should be placed in the section <system.webServer> at any place of its child section <system.serviceModel>. More about metadata on MSDN:
- How to: Publish Metadata for a Service Using a Configuration File
- How to: Publish Metadata for a Service Using Code
The service configured for WCF is not registered with the Autofac container
This is the main subject of the text and there are two points to remember. First of all, the markup file should have a correct information about interface, and the second, the interface and the client should be located in the same assembly.
<%@ ServiceHost Service="Contracts.IService, Contracts" CodeBehind="service.svc.cs" Factory="Autofac.Integration.Wcf.AutofacServiceHostFactory, Autofac.Integration.Wcf"%>
The main point is the structure of the attribute Service. You should write there a full interface name and full assembly name where the interface is located. In general case the assembly name should be the same as qualified in the project settings.