WCF服务为MVC应用程序的一部分 [英] WCF service as a part of MVC application
问题描述
我在MVC4的Web应用程序。我要举办的一个共享主机提供商。我想它来上传文件的WCF服务扩展。 (这里将通过一个WPF的桌面应用程序,允许用户直接从自己的电脑上传文件。)
我宁愿举办它在某种程度上在一起,以避免问题的读/写访问存储目录,但我不知道如何做到这一点。
-
我应承载的WCF在MVC应用程序中的selfhost?
-
我应该做的WCF服务的应用程序或一个类库?
-
如何使用MVC应用程序绑在一起?
我终于找到了如何使它工作。
在你的MVC应用程序Web.config文件中添加:
< system.serviceModel>
< serviceHostingEnvironment aspNetCompatibilityEnabled =真/>
< /system.serviceModel>
在你的路由配置--->的Global.asax或App_Start / RoutesConfig(取决于MVC版本)地址:
公共静态无效的RegisterRoutes(RouteCollection路由)
{
...
routes.Add(新ServiceRoute(你好,新ServiceHostFactory()的typeof(YourServiceClass)));
...
}
就是这样。现在出现在本地主机服务/个招呼,或者无论你部署应用程序。
本使用BasicHttpBinging默认。如果你需要其他的,你必须实现自己的ServiceHostFactory。据我观察,这种方法只适用于HTTP绑定。当我尝试添加一个NetTcpBinding的失败。
I have a web application in MVC4. I'm going to host in on a shared hosting provider. I want to extend it with a WCF service for uploading files. (There will by a WPF desktop application that will allow users to upload files directly from their PCs.)
I'd rather host it somehow "together" to avoid problems with read/write access to storage directory, but I have no idea how to do this.
Should I host WCF as a selfhost in MVC app?
Should I make the WCF service an application or a class library?
How to tie it together with the MVC app?
I finally found how to make it work.
In your MVC app Web.config add:
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
</system.serviceModel>
In your routes config ---> Global.asax or App_Start/RoutesConfig (depending on MVC version) add:
public static void RegisterRoutes(RouteCollection routes)
{
...
routes.Add(new ServiceRoute("hello", new ServiceHostFactory(), typeof(YourServiceClass)));
...
}
And that's it. Now your service appears under localhost/hello or wherever you deploy your app.
This uses BasicHttpBinging by default. If you need other you have to implement your own ServiceHostFactory. From what I observed, this method only works for HTTP bindings. When I tried to add a NetTcpBinding it failed.
这篇关于WCF服务为MVC应用程序的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!