8

I have a little bit of a "strange practise" question. The requirement on an architecture of our project is to setup Web API with (if possible) all MVC goodness within WCF project. That means that WCF and Web API web services would be stood up along each other within one project.

Is this even doable? I've given it a short go and found that even merging the web configs of two projects is very hard.

Thank you for your suggestions and comments,

Jakub

3 Answers 3

10

I followed these steps and it worked fine:

  1. Make sure your WCF service is working correctly.
  2. Install WebAPI to your project through Nuget Package Manager

    Install-Package Microsoft.AspNet.WebApi

  3. Create Controller folder and write your controller class and methods.

  4. Create global.asax file
  5. Register routes for your services in Application_Start method.

    protected void Application_Start(object sender, EventArgs e)
    { 
        RegisterRoutes(RouteTable.Routes);
    }
    
    private void RegisterRoutes(RouteCollection routes)
    {
        routes.MapHttpRoute(
    
             "webapi_route",
    
              "/{controller}/{action}/{id}",
    
             new { controller = "controller_name", action = "method_name", id = RouteParameter.Optional }
    
       );
    
        RouteTable.Routes.Add(new ServiceRoute("", new WebServiceHostFactory(), typeof(service_name)));
    }
    
Sign up to request clarification or add additional context in comments.

5 Comments

what is service_name here?
Your WCF class name.
@jcmordan i had the same question... if you dont have an existing svc class, you can create a new one
Thanks @AjayAradhya for the step-by-step. Worked like a charm!
service_name is still confusion, could you add a sample
0

In fact there are no major restrictions on the use of the two technologies within the same project. By default wcf has a different "pipe line" than asp.net, but until this can be changed. Using the configuration below in web.config it is possible to configure wcf to use the same asp.net pipe line thus sharing the whole life cycle of the objects of the request. But do not believe that this approach is usual for all cases, other factors need to be considered to make that decision, for example, how do you plan to distribute your application? When you release a version of wcf you will also be releasing the web.api, in many cases you may not want this result.

<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />

Comments

-1

It is possible but with losing the MVC goodness (that means that you will not be able to use for example the built in automatic online documentation for your web services).

Just install Web API through NuGet and then register your route in global.asax. Create your api controller and it should be all good.

EDIT 01.02.2017:

This is no longer true. Since Microsoft approach was to merge MVC and Web API controller. Now anything is possible.

3 Comments

not strictly true. You can have MVC, Web API and WCF all in the same application. They are independent pieces of technology. Just ensure your routing doesn't overlap, but apart from that it just works. I have a solution in production with exactly the same architecture.
@reckface this is true now, was not in 2013. Please see the dates before commenting on it. It's an outdated solution.
@reckface it's all good, happens :) to be honest I should probably update my answer :]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.