nancy 文件服务器,用Web api /Nancy 通过Owin Self Host简易实现一个 Http 服务器(示例代码)...

本文介绍了如何使用C#通过Nancy和WebApi框架实现HTTP服务器的自主托管。通过避免使用IIS,采用SelfHost方式简化了服务器部署。详细步骤包括安装必要的Nuget包,配置OwinStartup类,创建控制器并启动WebApp。同时,对比了Nancy和WebApi在SelfHost上的应用,并展示了如何在Nancy中添加模块和处理请求。
摘要由CSDN通过智能技术生成

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

C#做 http 有2个轻量级的框架, 一个是Nancy ,一个是 微软官方的Web Api 都可以通过owin 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 }

);

appBuilder.UseWebApi(config);

}

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

然后在Main 函数里面加入 WebApp.Start() 就会在SelfHost 上面运行你的Web Api,十分简洁

class Program

{

static void Main(string[] args)

{

string baseAddress = "http://localhost:9000/";

// Start OWIN host

using (WebApp.Start(url: baseAddress))

{

// Create HttpCient and make a request to api/values

HttpClient client = new HttpClient();

var response =client.GetAsync(baseAddress + "api/home").Result;

Console.WriteLine(response);

Console.WriteLine(response.Content.ReadAsStringAsync().Result);

Console.ReadLine();

}

Console.ReadLine();

}

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

尝试添加一个Controller

public class HomeController : ApiController

{

// GET api/values

public IHttpActionResult Get()

{

return Ok(125);

}

// GET api/values/5

public string Get(int id)

{

return "value";

}

// POST api/values

public void Post([FromBody]string value)

{

}

// PUT api/values/5

public void Put(int id, [FromBody]string value)

{

}

// DELETE api/values/5

public void Delete(int id)

{

}

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

尝试运行的结果

f38ffe106a535ce48c63778aafa473d4.png

17856e118a82c7eb5e83aa8b54521e5e.png

Nancy

Nancy是很多人都推荐的一个轻量级的Http 库,可以架在Mono,.net.core 上,也是开源

安装Nancy可以 Install-Package Nancy

而要 在SelfHost 监听则需要安装以下3个

Install-Package Microsoft.Owin.Hosting

Install-Package Microsoft.Owin.Host.HttpListener

Install-Package Nancy.Owin

1

2

3

在OwinStartup中表明使用Nancy

using Owin;

public class Startup

{

public void Configuration(IAppBuilder app)

{

app.UseNancy();

}

}

1

2

3

4

5

6

7

8

入口也是通过WebApp.Start(url)

using Microsoft.Owin.Hosting;

class Program

{

static void Main(string[] args)

{

var url = "http://+:8080";

using (WebApp.Start(url))

{

Console.WriteLine("Running on {0}", url);

Console.WriteLine("Press enter to exit");

Console.ReadLine();

}

}

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

///

/// 发现

/// 可以通过 Before 进行 安全的验证

///

public class HomeModule : NancyModule

{

public HomeModule()

{

Get["/"] = x => "Hello";

Get["/login"] = x => { return "person name :" + Request.Query.name + " age : " + Request.Query.age; };

}

}

1

2

3

4

5

6

7

8

9

10

11

12

运行结果

36bfd21fd0cbbceb6601872d1d97ad17.png 

0c026e2734b3986d7c1ea3fe32f25f27.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值