主持人WCF在MVC2网站 [英] Host WCF in MVC2 Site

查看:131
本文介绍了主持人WCF在MVC2网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们已经有了一个非常大的,复杂的MVC2网站。我们要添加一个API,一些内部工具,并决定使用WCF。

We've got a very large, complex MVC2 website. We want to add an API for some internal tools and decided to use WCF.

在理想情况下,我们希望MVC本身承载WCF服务。原因包括:

Ideally, we want MVC itself to host the WCF service. Reasons include:


  • 虽然有多个层到应用程序,一些我们想在API功能需要网站本身(如格式化电子邮件)。

  • 我们使用TFS自动生成(持续集成)和部署 - 我们需要的少修改构建和发布机制,更好

  • 我们使用控制的统一容器倒置在整个应用程序。作为该网站的一部分,使我们能够重新使用的配置类和其他辅助方法。

我写了一个自定义的ServiceBehavior这反过来有一个自定义InstanceProvider - 这让我实例化和配置的容器中,然后使用从WCF服务类实例的所有请求

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.

所以我的问题是;是否有可能从内部MVC本身?

So my question is; Is it possible to host a WCF service from within MVC itself?

我只有前服务/标准Asp.Net网站的经验并没有意识到MVC2可能有所不同,直到我真的试图将其连接到配置和的什么也没发生的。一些google搜索后,似乎并没有是这样做的许多参考 - 所以认为我会问这里

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.

详细资料:

感谢那些谁回答,但我仍然有得到这个工作的问题......我现在的配置是这样的:

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?

我期待找到 HTTP端点://localhost/MyApplication/Job.svc http和元数据://本地主机/ ?所有MyApplication / Job.svc MEX 然而,无论是给一个404 - 据我所知,有这么WCF是运行在所有没有明显的迹象。我也许需要做一些事来我的路线?

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?

注:如果别人这个问题,我不得不添加 routes.IgnoreRoute({} MyJob .SVC / {*} PATHINFO)来的路由设置在的Global.asax 来prevent MVC拦截来电。

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.

要WCF服务添加到您的项目中需要添加新项目<大骨节病> + CTRT Shift + A 选择WCF服务。在这样简单的WCF服务将被添加在web.config将被修改。它可以根据需要来修改创建的SVC文件,例如用System.ServiceModel.Activati​​on.WebServiceHostFactory。使用.NET 4.0开始,你可以删除SVC文件,并使用&LT; serviceActivati​​on&GT; 相同的信息来代替。例如,

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>

一小句话。如果从MVC它冯内部调用WCF的一些方法需要使用 HttpContext.Current 而不是 WebOperationContext.Current 。我在WCF code总是测试,如果 WebOperationContext.Current == NULL ,然后用 HttpContext.Current

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.

这篇关于主持人WCF在MVC2网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆