Sponsored by Gluten Free Waffles and Sliced Bananas

Tuesday, April 2, 2013

ASP.NET Web API, Autofac & Self-Hosting

8:40 PM Posted by Tyson Nero , , , , 12 comments

Well Structured Projects


Separation of concerns comes into play even when laying out the projects in your code solution. For instance, you may have separate projects for your contract classes (DTOs), data layer, service layer, and sometimes even finer granularity among those.

When building such projects using ASP.NET's Web API for example, you may be tempted to lump everything into a single MVC 4 project, but each code solution has different needs and requirements, and even this type of solution can be broken out into multiple projects.

Self-Hosted Web API w/ external Controllers


Let's say you are building an IIS hosted Web API solution. Mainly, all of the clients connecting to your API will be web applications using AJAX. However, your API is also expected to have the ability to expose itself to other .NET clients who may be using the HttpClient object to perform operations. In this case, you may decide to break your solution out into 2 separate projects (I'm simplifying for brevity):
  1. Web Host Project (Empty MVC 4 Project)
  2. API/Service Project (Class Library)
The Web Host Project will be that which actually hosts the service, and the API/Service Project will be the implementation.

I highly recommend taking some time to review the PingYourPackage open source project on GitHub for a reference to such an implementation.


Implementing a Self-Hosted Web API with external assembly dependencies


API Host


Create a Console Application project to host the Web API by following this tutorial: Self-Host a Web API.


Controllers & Service Implementation


Create a new Class Library project for your controllers and service implementation
Implement a controller that inherits ApiController (ProductsController.cs)

Reference AutoFac.WebApi


Add an AutoFac.WebApi reference to your your Class Library project. This can be done via NuGet.



Wire Up AutoFac


You basically have two options here...

1) Add the AutoFac.WebApi dependency registration code to Program.cs.

2) Or, optionally create a configuration class (AutoFacConfig.cs). This will most likely be created in your service implementation project so any host could use it. Be sure to register your controller within this code.

With this, you will be able to call AutoFacConfig.Initialize(config) within your host.


Final Implementation


12 comments:

  1. Thanks for putting this together, I was struggling to figure out how to separate my Controllers from the hosting layer.

    Is there a reason you need to use the Autofac? Does .NET not support this workflow natively? Are there any other options?

    ReplyDelete
  2. Yes it does. You only have to implement IAssembliesResolver and set the DependencyResolver to your class.
    This post has more information on how to do it "manually"
    http://www.strathweb.com/2012/06/using-controllers-from-an-external-assembly-in-asp-net-web-api/

    ReplyDelete
  3. I will come to you to know the handy article which is very essential for me as well. I must follow the content which is very essential for me as well. Keep it up.
    Darrell Issa

    ReplyDelete
  4. This comment has been removed by a blog administrator.

    ReplyDelete
  5. This comment has been removed by a blog administrator.

    ReplyDelete
  6. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru Dot Net Training in Chennai. Nowadays Dot Net has tons of job opportunities on various vertical industry.
    or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.

    ReplyDelete
  7. Its very informative blog and useful article thank you for sharing with us , keep posting learn
    .NET Online Training Hyderabad

    ReplyDelete
  8. The new model is converting to the https model and that is considered to be more secured than any other new model so homepage is also recommending this model to make surfing more safe.

    ReplyDelete
  9. Very nice post here and thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's.
    rpa training in bangalore
    best rpa training in bangalore
    RPA training in bangalore
    rpa course in bangalore
    rpa training in chennai
    rpa online training

    ReplyDelete
  10. Played on BGAOC with big wins? NOT? Come to us as soon as possible and win with us. perfect all slots casino Come get your chance to win.

    ReplyDelete