Host WCF service library in ASP.NET MVC 4 application

By | October 10, 2013

Advertisement from Google

If you have existed WCF service library and want to host this WCF serivce in your ASP.NET MVC4 application I have prepared this short how-to.

For example you have WCF service located in FooWCF assebly and  MVC4 application located in BarWeb project.

Open BarWeb project and add reference to FooWCF assebly.

Now, add testService.svc WCF Service file in to BarWeb project, remove ItestService.cs and testService.svc.cs files from project.

Open testService.svc and replace

Advertisement from Google

<%@ ServiceHost Language="C#" Debug="true" Service="testForBlock.testService" CodeBehind="testService.svc.cs" %>


<%@ ServiceHost Language="C#" Debug="true" Service="FooWCF.Service1" %>

Then open web.config and add:

      <service behaviorConfiguration="FooWCF.Behavior" name="FooWCF.Service1">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="" name="BasicHttpBindingEndpoint" contract="FooWCF.IService">
            <dns value="localhost" />
        <endpoint address="/rest" binding="webHttpBinding" contract="FooWCF.IService" behaviorConfiguration="restBehavior" />
        <endpoint address="mex" binding="mexHttpBinding" bindingConfiguration="" name="MexHttpBindingEndpoint" contract="IMetadataExchange" />
           <add baseAddress="http://localhost/testService.svc"/>
        <behavior name="FooWCF.Behavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        <behavior name="restBehavior">
          <webHttp />
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />

We have used this behaviour in Advanced Tic Tac Toe because when we start creating web based version of this game we already have WCF service library with all game logic.

4 thoughts on “Host WCF service library in ASP.NET MVC 4 application

  1. Andrzej

    I tried to follow your instructions, but fialed. Can you please provide a sample source code?

      1. Andrzej

        Thanks a lot! This seems to work. I’m just wandering why after I run WebServiceHost WcfTestClient can still connect to “http://localhost:8733/Design_Time_Addresses/TicTacToe.Service/TicTacToeService/mex” (TicTacToe.Service/App.config) but cannot connect to “http://localhost:8733/TicTacToe.svc/mex” (WebServiceHost/Web.config)


Leave a Reply

Your email address will not be published. Required fields are marked *