在 MVC2 站点中托管 WCF

发布于 2024-10-12 03:55:46 字数 2595 浏览 1 评论 0原文

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

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

  • 尽管应用程序有多个层,但我们希望 API 中的某些功能需要网站本身(例如,格式化电子邮件)。
  • 我们使用 TFS 来自动构建(持续集成)和部署 - 我们需要修改的构建和发布机制越少越好
  • 我们在整个应用程序中使用 Unity 容器和控制反转。作为网站的一部分,我们可以重用配置类和其他辅助方法。

我编写了一个自定义 ServiceBehavior,它又具有一个自定义 InstanceProvider - 这允许我实例化和配置一个容器,然后使用该容器为来自 WCF 的类实例的所有请求提供服务。

所以我的问题是;是否可以从 MVC 本身托管 WCF 服务?

我之前只在服务/标准 Asp.Net 网站上有过经验,并且没有意识到 MVC2 可能有所不同,直到我实际尝试将其连接到配置中并且什么也没发生。经过一番谷歌搜索后,似乎没有太多关于这样做的参考 - 所以我想在这里问。

更多详细信息:

感谢那些回复的人,但我仍然遇到问题使其正常工作...我当前的配置如下所示:

<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>

有人可以告诉我是否有任何明显错误吗?

我希望在 http://localhost/MyApplication/Job.svc 找到端点,并在 http://localhost/MyApplication/Job.svc?mex 找到元数据,都给出 404 - 据我所知,没有明显迹象表明 WCF 正在运行。我是否需要对我的路线做一些事情?

注意:如果其他人遇到此问题,我必须将 routes.IgnoreRoute("{MyJob}.svc/{*pathInfo}") 添加到 < code>Global.asax 以防止 MVC 拦截调用。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深空失忆 2024-10-19 03:55:46

是的,这是可能的。我在我的项目中使用它并且很高兴。

要将 WCF 服务添加到您的项目中,您需要添加新项目 Ctrt+Shift+A 并选择“WCF 服务”。通过这种方式,将添加简单的 WCF 服务并修改 web.config。可能需要修改创建的 SVC 文件,例如使用“System.ServiceModel.Activation.WebServiceHostFactory”。从 .NET 4.0 开始,您可以删除 SVC 文件并使用具有相同信息的 。例如,

<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 代码中测试如果 WebOperationContext.Current == NULL,则使用 HttpContext.Current

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.

风向决定发型 2024-10-19 03:55:46

是的,这是可能的。您唯一需要的就是添加指向您的服务类并将工厂属性设置为您的自定义主机工厂的“.svc”文件。像这样:

<%@ServiceHost language="c#" Debug="true" 
    Service="Services.Implementations.UserValidator"
    Factory="Services.Factories.CustomServiceHostFactory"  %>

其他事情是相同的,例如将配置添加到配置文件等。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文