用Web api /Nancy 通过Owin Self Host简易实现一个 Http 服务器

本文介绍了如何利用C#的轻量级框架Nancy和Web API,通过Owin Self Host在应用程序内部创建Http服务器,避免了IIS的配置与管理。详细步骤包括安装必要的NuGet包,创建Owin Startup类,配置Web App启动,以及创建Controller和Nancy Module,展示了一个简洁的自我托管HTTP服务的实现过程。
摘要由CSDN通过智能技术生成

过去做 端游的Http 服务器 用的WebApi 或者Mvc架构,都是放在iis。。。而我已经是懒出一个地步,并不想去配iis,或者去管理iis,所以我很喜欢 Self host 的启动方式。

C#做 http 有2个轻量级的框架, 一个是Nancy ,一个是 微软官方的Web Api 都可以通过owin self host 在应用程序中启动监听

Web Api

官方教程 :https://www.asp.net/web-api/overview/hosting-aspnet-web-api/use-owin-to-self-host-web-api

新建一个控制台从程序
在Nuget控制台上 安装包 Install-Package Microsoft.AspNet.WebApi.OwinSelfHost
然后添加一个Owin Startup类
以往所有的配置都正常的放在Startup中进行配置就可以

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.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
            a
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值