侧边栏壁纸
  • 累计撰写 185 篇文章
  • 累计创建 77 个标签
  • 累计收到 17 条评论

目 录CONTENT

文章目录

使用控制台应用程序创建WEBAPI自托管程序

码峰
2022-10-20 / 0 评论 / 0 点赞 / 1,013 阅读 / 615 字 / 正在检测是否收录...
广告 广告

前言

创建 .NET ASP的WEBAPI可以选择WEBAPI项目类型,但需要使用IIS托管,有些情况下,只想做简单的WEB API服务,也有办法可以使用Console应用程序创建WEBAPI,在提到控制台应用程序,很多人会怀疑服务的URL是如何通过生成的可执行文件(.EXE)托管的?下面,我们将使用Owin,以自托管方式创建WEBAPI应用程序(RESTfull)。

实现过程

新建项目

在Visual C#中选择控制台应用程序类型:“Console application”

安装 nuget 包Owin和Cors

  • 在工程上右键“管理Nuget程序包”,搜索“Microsoft.AspNet.WebApi.OwinSelfHost”执行安装:
    nuget安装Owin

  • 搜索“Microsoft.AspNet.WebApi.Cors”执行安装
    image-1666272631630

添加Startup.cs

工程中新建源文件Startup.cs,参考源码如下:

using System;
using Owin;
using System.Web.Http;
using System.Net.Http;

namespace firstMicroService
{
    public class Startup
    {
        public void Configuration(IAppBuilder appBuilder)
        {
            HttpConfiguration CONFIG = new HttpConfiguration();
            CONFIG.EnableCors();
            CONFIG.Routes.MapHttpRoute(
                name: "createUserApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
                );
            appBuilder.UseWebApi(CONFIG);
        }
    }
}

创建Controller

创建一个名为Controller类,类名为EmployeeController源文件为EmployeeController.cs,代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Cors;

namespace firstMicroService
{
    public class Employee
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Salary { get; set; }
    }

    [EnableCors(origins:"*", headers:"*", methods:"*")]
    public class EmployeeController : ApiController
    {
        Employee[] employees = new Employee[]
        {
            new Employee{Id=0,Name="Morris",Salary="151110"},
            new Employee{Id=1,Name="John", Salary="120000"},
            new Employee{Id=2,Name="Chris",Salary="140000"},
            new Employee{Id=3,Name="Siraj", Salary="90000"}
        };

        public IEnumerable<Employee> Get()
        {
            return employees.ToList();
        }

        public Employee Get(int Id)
        {
            try
            {
                return employees[Id];
            }
            catch (Exception)
            {
                return new Employee();
            }
        }

    }
}

修改Program.cs

修改Program.cs的代码如下:

using Microsoft.Owin.Hosting;
using System;

namespace firstMicroService
{
    class Program
    {
        static void Main(string[] args)
        {
            string domainAddress = "http://20.201.36.49/";

            using (WebApp.Start(url: domainAddress))
            {
                Console.WriteLine("Service Hosted " + domainAddress);
                System.Threading.Thread.Sleep(-1);
            }
        }
    }
}

修改app.config

修改的内容如下:

<configuration>
  <appSettings>
    <add key="owin:AutomaticAppStartup" value="false" />
  </appSettings>
  ...
</configuration>

运行程序

程序启动后,保持控制台窗口开启,如下图:
image-1665308161733

测试WEBAPI

服务中实现了一个GET方法,调用格式如下:

{服务器地址}/{api}/{Controller名称}/

这里,我们用POSTMAN测试WEBAPI,测试访问地址:

 http://20.201.36.49/api/Employee/1

如下图所示:
自托管WEBAPI测试

0
广告 广告

评论区