We've got a very large, complex MVC2 website. We want to add an API for some internal tools and decided to use WCF.
Ideally, we want MVC itself to host the WCF service. Reasons include:
- Although there's multiple tiers to the application, some functionality we'd like in the API requires the website itself (e.g. formatting emails).
- We use TFS to auto-build (continuous integration) and deploy - The less we need to modify the build and release mechanism the better
- We use the Unity container and Inversion of Control throughout the application. Being part of the Website would allow us to re-use configuration classes and other helper methods.
I've written a custom ServiceBehavior which in turn has a custom InstanceProvider - This allows me to instantiate and configure a container which is then used to service all requests for class instances from WCF.
So my question is; Is it possible to host a WCF service from within MVC itself?
I've only had experience in Services / Standard Asp.Net websites before and didn't realise MVC2 might be different until I actually tried to wire it into the config and nothing happened. After some googling, there don't seem to be many references to doing this - so thought I'd ask here.
More Detail:
Thanks to those of you who replied but I'm still having issues getting this to work... My current config looks like this:
<system.serviceModel>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"
aspNetCompatibilityEnabled="true">
<serviceActivations>
<add relativeAddress="Job.svc"
service="MyApplication.WebJobManager"
factory="System.ServiceModel.Activation.WebServiceHostFactory" />
</serviceActivations>
</serviceHostingEnvironment>
<extensions>
<behaviorExtensions>
<add name="WCFDIBehavior" type="MyApplication.Jobs.WCFDIBehaviorExtension, MyApplication.Jobs, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</behaviorExtensions>
</extensions>
<standardEndpoints>
<mexEndpoint>
<standardEndpoint name="WebJobManagerMex" />
</mexEndpoint>
</standardEndpoints>
<behaviors>
<serviceBehaviors>
<behavior name="JobServiceBehavior">
<serviceMetadata />
<WCFDIBehavior />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="" name="MyApplication.Jobs.WebJobManager">
<endpoint binding="basicHttpBinding"
bindingConfiguration="" name="HTTPEndpoint" contract="MyApplication.JobService.Interfaces.IWebJobManager" />
</service>
</services>
</system.serviceModel>
Can someone please tell me if anything looks obviously wrong?
I was expecting to find the endpoint at http://localhost/MyApplication/Job.svc
and metadata at http://localhost/MyApplication/Job.svc?mex
however, both give a 404 - As far as I can tell, there's no obvious sign that WCF is running at all. Do I perhaps need to do something to my routes?
NB: In case others have this problem, I had to add routes.IgnoreRoute("{MyJob}.svc/{*pathInfo}")
to the Route setup in Global.asax
to prevent MVC intercepting the call.