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 multipleSiteBindingsE开发者_开发知识库nabled="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.
Yes it is possible. I use it in my projects and be happy.
To add WCF Service to your project you need add new item Ctrt+Shift+A and choose "WCF Service". In the way the simple WCF Service will be added and the web.config will be modified. It can be needed to modify SVC file created, for example to use "System.ServiceModel.Activation.WebServiceHostFactory". Starting with .NET 4.0 you can delete the SVC file and use <serviceActivation>
with the same information instead. For example,
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"
aspNetCompatibilityEnabled="true">
<serviceActivations>
<add relativeAddress="My.svc"
service="MyNamespace.MyClass"
factory="System.ServiceModel.Activation.WebServiceHostFactory" />
</serviceActivations>
</serviceHostingEnvironment>
One small remark. If you call some WCF methods internally from the MVC it yon be needed to use HttpContext.Current
instead of WebOperationContext.Current
. I test always in the WCF code if WebOperationContext.Current == NULL
, then use HttpContext.Current
.
Yes it is possible. The only thing you need is to add ".svc" file that is pointing to you service class and has factory property set to you custom host factory. Like this:
<%@ServiceHost language="c#" Debug="true"
Service="Services.Implementations.UserValidator"
Factory="Services.Factories.CustomServiceHostFactory" %>
Other things are the same, like adding configuratin to config file etc.
精彩评论