WCF service as a part of MVC application

前端 未结 3 717
执念已碎
执念已碎 2020-12-31 14:04

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 wil

相关标签:
3条回答
  • 2020-12-31 14:41

    Since you're already building an MVC4 app, you might as well consider using MVC4 + Web API since those work together without any issues (Web API is previously known WCF REST).

    If you still decide to stick with WCF, I wouldn't mix it in the same project with MVC4 application but rather host it as additional IIS application, side-by-side with MVC4.

    0 讨论(0)
  • 2020-12-31 15:01

    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.

    0 讨论(0)
  • 2020-12-31 15:06

    We do this all the time in the following way:

    1. Create a WCF class library
    2. Reference the WCF class library in your ASP.NET MVC 4 webapplication
    3. Create a Svc file in your ASP.NET web app making the WCF endpoint available as such
    4. Maybe you need to play a bit with the settings in the web.config file, which is now including WCF service endpoint settings next to the ASP.NET settings.
    0 讨论(0)
提交回复
热议问题