Host WCF in MVC2 Site

梦想与她 提交于 2019-11-27 15:49:37

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!