Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF service as a part of MVC application

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.

  1. Should I host WCF as a selfhost in MVC app?

  2. Should I make the WCF service an application or a class library?

  3. How to tie it together with the MVC app?

like image 616
Andrzej Gis Avatar asked Oct 13 '13 22:10

Andrzej Gis


1 Answers

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.

like image 101
Andrzej Gis Avatar answered Oct 01 '22 05:10

Andrzej Gis