What is the correct way to create a WCF service in separate assembly but then expose its endpoint through a Web Project in the same solution?
I've done it this way:
- Build your WCF service in a new project of type class library
- Put your interfaces and implementations in this library in a namespace like
MyServiceLib
Add to your web project a file like
MyService.svc
with only one statement, the ServiceHost directive:<%@ ServiceHost Service="MyServiceLib.MyService" %>
where
MyServiceLib
is the name of the namespace of your WCF service andMyService
the name of your service implementation class. (This simple setup is for the case when you deploy your service as a compliled assembly (in theBin
directory for instance). If you want to deploy with source and let complile on first request you need to put some more attributes to the service host directive (Programming language, Source file, etc.)- Put the configuration of the service into
web.config
in the<system.serviceModel>
section.
If you have control of both the server and the client you could use the method described in this link: http://www.dnrtv.com/default.aspx?showNum=122
I prefer separating my contracts and implementations in their own assemblies, this lends itself to alternate implementations based on the same contracts down the road.