码迷,mamicode.com
首页 > Windows程序 > 详细

Owin Self Host

时间:2016-12-18 10:06:35      阅读:277      评论:0      收藏:0      [点我收藏+]

标签:asp   for   target   ros   定义   asi   isp   context   param   

http://owin.org/

Owin 定义了webserver和webapplication之间的标准接口,目标就是为了解耦webapplication对webserver的依赖,

就是说以后可以轻松的建立一个轻量级的HttpServer,

1.Basic Sample  Self Host

下面建立一个Basic Self Host Http Server Via Owin ,全部功能就是获取客户端的Http请求,然后做出回应,当发生Unhandled Exception的时候,会自动跳转到错误页,

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 on how 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 context.Response.WriteAsync("Hello, world.");
            });
        }
    }
}

 

app.UseErrorPage();

为WebApplication指定错误页,

app.Run加入响应逻辑

Step3:指定监听端口

 

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,一切准备就绪,启动这个Console程序,请求 http://localhost:9000/ 这个地址,看起来比NodeJS更加方便。

 2.Self Host Web API using Owin

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

Step2:Startup.cs

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);
        }
    } 
}

 Step3:APIController

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:设置监听端口

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 request 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:启动Console程序,这里我们看到返回的是Json格式的数据,当我们用火狐浏览器进行请求,会发现返回的是XML格式的数据,因为火狐浏览器默认的Accept为XML

 

技术分享

技术分享

 

技术分享

 

Owin Self Host

标签:asp   for   target   ros   定义   asi   isp   context   param   

原文地址:http://www.cnblogs.com/LittleFeiHu/p/6193739.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!