Bas*_*sic 3 asp.net-mvc wcf web-services asp.net-mvc-2
我们有一个非常庞大,复杂的MVC2网站.我们想为一些内部工具添加API,并决定使用WCF.
理想情况下,我们希望MVC本身托管WCF服务.原因包括:
我编写了一个自定义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>
Run Code Online (Sandbox Code Playgroud)
如果有什么看起来明显错误,有人可以告诉我吗?
我期待找到端点http://localhost/MyApplication/Job.svc和元数据http://localhost/MyApplication/Job.svc?mex然而,两者都给出了404 - 据我所知,没有明显迹象表明WCF正在运行.我可能需要对我的路线做点什么吗?
注意:如果其他人遇到这个问题,我必须添加routes.IgnoreRoute("{MyJob}.svc/{*pathInfo}")到路由设置中Global.asax以防止MVC拦截呼叫.
Ole*_*leg 10
对的,这是可能的.我在我的项目中使用它并且很开心.
要将WCF服务添加到项目,您需要添加新项目Ctrt+Shift+A并选择"WCF服务".在这种方式中,将添加简单的WCF服务并修改web.config.可能需要修改创建的SVC文件,例如使用"System.ServiceModel.Activation.WebServiceHostFactory".从.NET 4.0开始,您可以删除SVC文件并使用<serviceActivation>相同的信息.例如,
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"
aspNetCompatibilityEnabled="true">
<serviceActivations>
<add relativeAddress="My.svc"
service="MyNamespace.MyClass"
factory="System.ServiceModel.Activation.WebServiceHostFactory" />
</serviceActivations>
</serviceHostingEnvironment>
Run Code Online (Sandbox Code Playgroud)
一个小小的评论.如果从MVC内部调用一些WCF方法,则需要使用HttpContext.Current而不是WebOperationContext.Current.我总是在WCF代码中测试if WebOperationContext.Current == NULL,然后使用HttpContext.Current.
| 归档时间: |
|
| 查看次数: |
3338 次 |
| 最近记录: |