Setting up Web API within WCF Project

前端 未结 3 960
不思量自难忘°
不思量自难忘° 2021-01-12 13:32

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

相关标签:
3条回答
  • 2021-01-12 13:37

    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.

    0 讨论(0)
  • 2021-01-12 13:39

    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)));
      }
      
    0 讨论(0)
  • 2021-01-12 13:49

    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" />
    
    0 讨论(0)
提交回复
热议问题