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?
3 Answers
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.

- 175,098
- 59
- 401
- 420
-
Helpful Hint: if you try to paste the above link into an .svc file, Visual Studio will try to help you by adding an empty value "" to the 'ServiceHost' element (so that it looks like ServiceHost=""). This will screw up your attempt to launch the service with a message that says 'Parser Error Message: The name of the directive is missing.' This is easily fixed by removing the empty value. – Quark Soup Mar 31 '16 at 00:03
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.

- 31,040
- 13
- 70
- 99
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

- 64,065
- 34
- 143
- 252