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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With