ASP.NET Web API Self-Host

The sample demonstrates how to use ASP.NET Web API to self host a simple web API in your own process.

C# (268.9 KB)
 
 
 
 
 
4.5 Star
(12)
11,974 times
Add to favorites
8/15/2012
E-mail Twitter del.icio.us Digg Facebook
Sign in to ask a question


  • SSL in a Self-hosted WebApi context
    2 Posts | Last post June 11, 2013
    • Hi,
      
      This question seems to have been asked before but has not been answered. 
      
      I found a potential solution at http://pfelix.wordpress.com/2012/02/26/enabling-https-with-self-hosted-asp-net-web-api/ . However, this is not working in Visual Studio 2012. I get an error saying that: The type or namespace name 'BindingParameterCollection' could not be found (are you missing a using directive or an assembly reference?)
      
      This error occurs even though I add a reference and the corresponding using statement for the System.ServiceModel.Channels namespace that has the BindingParameterCollection class. It looks like the class is an internal class. I was not able to figure out an alternative for enabling SSL.
      
      Thanks in advance.
      Sandeep
      
    • I got it to work! Actually, we do not need the MyHttpsSelfHostConfiguration override. It turns out that HttpsSelfHostConfiguration automatically enables SSL if we use https://localhost:443 … in the initialization step (var config = new HttpSelfHostConfiguration(“https://localhost:443″);)
      
      Just follow the steps in http://pfelix.wordpress.com/2012/02/26/enabling-https-with-self-hosted-asp-net-web-api/ without the MyHttpsSelfHostConfiguration override and it should work fine.
      
      thanks
      sandeep
  • Using post
    2 Posts | Last post October 24, 2012
    • Hello I have tried to use Self Host Web Api to do a post. I try to post to the method using fidler but the data I sent do not get into the method its like I am sending null. I can not find an example of doing a post could you extend your example to how to use post that would be great.
      
      Thx in advance
    • I did some more research and found this post http://www.hanselman.com/blog/TinyHappyFeatures2ASPNETWebAPIInVisualStudio2012.aspx and this solved my question and now I can use post in a Self Host Web Api and it works realy great!
  • Is this only in process?
    1 Posts | Last post September 19, 2012
    • "ASP.NET Web API gives you the flexiblity to host your web APIs anywhere you would like"  
      
      I'm still processing this, but it sounds as though you could easily eliminate IIS entirely from data servers using Web API. Am I missing something, like scaleability?  
  • Performance & Scaling: self-host vs IIS
    2 Posts | Last post September 03, 2012
    • Hi Daniel,
      
      Can you provide some more information about the performance & scaling characteristics of self-hosted Web API? I am especially interested in a comparison between self-hosted and IIS hosted. 
      
      What would you advice?
      
      Thanks
    • This comparison from Dave Ward on Encosia shows a measured comparison: http://encosia.com/i/asp-net-mvc-vs-web-api-vs-self-hosted-web-api/
  • How about SSL?
    1 Posts | Last post August 21, 2012
    • Thanks for the example Daniel!  Do you know if there is a way to enable SSL in a self-hosted scenario?
      
      Thanks again for the help.
  • How to user authorization
    2 Posts | Last post August 14, 2012
    • I am trying to realize authorization with ActionFilterAttribute. But I always get HttpContext.Current is null. How to implement Authotization here?
    • You probably want to implement an AuthorizationFilterAttribute instead of ActionFilterAttribute if your goal is to authorize requests. HttpContext.Current is only available when web hosted. You will need to use the provided HttpActionContext instead.
  • Same question about the performance.
    2 Posts | Last post June 17, 2012
    • It seems no answer, Waiting...
    • The difference between self-host and web-host will of course vary depending on your specific scenario and configuration, but in general self-host has better throughput characteristics than web-host.
  • Could not find the vb.net code
    2 Posts | Last post June 17, 2012
    • Please post
    • Only a C# version is available at this time.