Owin Self Host

Source: Internet
Author: User
Tags hosting


Owin defines the standard interface between Webserver and WebApplication, and the goal is to understand the dependence of webapplication on webserver,

This means that you can easily build a lightweight httpserver in the future,

1.Basic Sample Self Host

The following is a basic self Host http Server Via Owin, all of which is to get the client HTTP request, and then respond, when the unhandled exception occurs, will automatically jump to the error page,

STEP1:Install-Package Microsoft.Owin.SelfHost

Step2:configuration in Startup.css

Using system;using system.threading.tasks;using microsoft.owin;using owin;namespace owinselfhostbasic{public    Class Startup    {public        void Configuration (Iappbuilder app)        {            app. Useerrorpage ();            For more information about to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888            app. Run (context = {                if (context. Request.Path.Value = = "/fail")                {                    throw new Exception ("Random Exception");                }                Context. Response.ContentType = "Text/plain";                Return to context. Response.writeasync ("Hello, World");}}}    

App. Useerrorpage ();

Specify an error page for WebApplication,

App. Run Join response logic

STEP3: Specifying a listening port

Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Namespace owinselfhostbasic{    class program    {        static void Main (string[] args)        {            using ( Microsoft.owin.hosting.webapp.start<startup> ("http://localhost:9000"))            {                Console.WriteLine (" Press [Enter] to quit ... ");                Console.ReadLine ();}}}    

OK, everything is ready, start the console program, request Http://localhost:9000/This address, looks more convenient than Nodejs.

2.Self Host Web API using Owin

STEP1:Install-Package Microsoft.AspNet.WebApi.OwinSelfHost


Using owin;using System.Web.Http; namespace owinwebapiselfhost{public    class Startup    {        //This code configures WEB API. The Startup class is specified as a type        //parameter in the Webapp.start method.        public void Configuration (Iappbuilder appBuilder)        {            //Configure Web API for self-host.             httpconfiguration config = new httpconfiguration ();            Config. Routes.maphttproute (                name: "Defaultapi",                routetemplate: "Api/{controller}/{id}",                defaults:new {id = Routeparameter.optional}            );            Appbuilder.usewebapi (config);        }    } }


Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Using System.web.http;namespace owinwebapiselfhost{public    class Valuescontroller:apicontroller    {        // Get api/values public         ienumerable<string> get ()        {            return new string[] {"value1", "value2"};        }    }}

STEP4: Setting the Listening port

Using microsoft.owin.hosting;using system;using system.net.http;namespace owinwebapiselfhost{Public    class Program    {        static void Main ()        {            string baseaddress = "Http://localhost:9000/";            Start OWIN host             using (webapp.start<startup> (url:baseaddress))            {                //Create httpcient and make a req Uest to api/values                 HttpClient client = new HttpClient ();                var response = client. Getasync (baseaddress + "Api/values"). Result;                Console.WriteLine (response);                Console.WriteLine (Response. Content.readasstringasync (). Result);                Console.ReadLine ();}}}    

STEP5: Start the console program, here we see the return of the data is in JSON format, when we use Firefox browser request, we will find that the return is the XML format of the data, because Firefox default accept XML

Owin Self Host

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.