国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Kestrel封裝在WindowService中(.net5,.net6,.net7三個版本的介紹)

這篇具有很好參考價值的文章主要介紹了Kestrel封裝在WindowService中(.net5,.net6,.net7三個版本的介紹)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

背景

在一些開發(fā)過程中,會在局域網(wǎng)內(nèi)搭建webapi服務(wù)作為移動端的服務(wù)接口使用,但是每次實施人員要到客戶現(xiàn)場安裝iis等工具,還有一些web的配置,非常繁瑣,所以想著把webapi封裝到WindowService中,可以通過自定義的安裝程序進(jìn)行一鍵部署,豈不美哉!
這篇文章主要是記錄如何將Kestrel的服務(wù)封裝在WindowService中

關(guān)于WindowsServer

請參考如下這篇文章

.netcore worker service (輔助角色服務(wù)) 的上手入門,包含linux和windows服務(wù)部署

開發(fā)服務(wù)

之前做過.net5版本的處理,覺得挺簡單的,但是到.net6的時候遇到了一些問題,所以下面都會記錄

.NET5版本

建項目

新建一個webapi項目,如下圖
Kestrel封裝在WindowService中(.net5,.net6,.net7三個版本的介紹)

添加Controller

Kestrel封裝在WindowService中(.net5,.net6,.net7三個版本的介紹)

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace WebApiNet_v5.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class TestController : ControllerBase
    {
        [HttpGet]
        public string Get(string name)
        {
            return $"Hello {name}";
        }
    }
}

添加引用

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net5.0-windows</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
  <!-- 千萬不要引用7.0版本,不兼容 -->
    <PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="6.0.0" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
  </ItemGroup>

</Project>

修改Startup.cs

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WebApiNet_v5
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {

            services.AddControllers();
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "WebApiNet_v5", Version = "v1" });
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
        //這里注釋一下是為了在發(fā)布以后還可以查看Swagger
            //if (env.IsDevelopment())
            //{
                app.UseDeveloperExceptionPage();
                app.UseSwagger();
                app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebApiNet_v5 v1"));
            //}

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}

修改Program.cs

using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;

namespace WebApiNet_v5
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                })
            //添加服務(wù)
            .UseWindowsService(cfg =>
            {
                cfg.ServiceName = "WebApiNet_v5";
            })

            ;
    }
}

配置Kestrel監(jiān)聽

參考文章

.Net Core 通過配置文件(appsetting.json)修改Kestrel啟動端口

實際配置效果

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "Kestrel": {
    "EndPoints": {
      "Http": {
        "Url": "http://0.0.0.0:5003" // 端口自己改吧
      }
    }
  },
  "AllowedHosts": "*"
}

發(fā)布程序

發(fā)布到本地目錄,如下圖
Kestrel封裝在WindowService中(.net5,.net6,.net7三個版本的介紹)

通過命令行創(chuàng)建服務(wù)

注意:一定要以管理員身份運行,否則無權(quán)限
例如出現(xiàn)如下錯誤:
[SC] OpenSCManager 失敗 5:
Kestrel封裝在WindowService中(.net5,.net6,.net7三個版本的介紹)

關(guān)于SC命令

Kestrel封裝在WindowService中(.net5,.net6,.net7三個版本的介紹)

啟動服務(wù)查看效果

sc.exe start 1_v5
測試效果

Kestrel封裝在WindowService中(.net5,.net6,.net7三個版本的介紹)


.NET6

因為.net6的改版,已經(jīng)沒有Startup文件了,而且程序的啟動已經(jīng)不再使用IHostBuilder接口了。
所以如下記錄的內(nèi)容都是在.net5版本上的差異與變動
代碼如下:

using Microsoft.Extensions.Hosting.WindowsServices;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Models;
using System.Net;

namespace WebApiNet_v6
{
    public class Program
    {
        public static void Main(string[] args)
        {
        //配置啟動參數(shù)
            var options = new WebApplicationOptions
            {
                Args = args,
                ContentRootPath = WindowsServiceHelpers.IsWindowsService()
                                     ? AppContext.BaseDirectory : default
            };

            var builder = WebApplication.CreateBuilder(options);

            // Add services to the container.

            builder.Services.AddControllers();

            // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
            builder.Services.AddEndpointsApiExplorer();
            builder.Services.AddSwaggerGen();
//啟動服務(wù)
            builder.Host.UseWindowsService();

            var app = builder.Build();

            // Configure the HTTP request pipeline.
            //if (app.Environment.IsDevelopment())
            //{
            app.UseSwagger();
            app.UseSwaggerUI();
            //}

            //app.UseHttpsRedirection();

            app.UseAuthorization();

            app.MapControllers();

            app.Run();
        }
    }
}

代碼上的調(diào)整就這么多,但是在修改的過程中遇到了一些錯誤

錯誤1

出現(xiàn) URL scheme must be http or https for CORS request

Kestrel封裝在WindowService中(.net5,.net6,.net7三個版本的介紹)

解決辦法:

禁用https重定向,或者完全使用https都可以
禁用辦法就是注釋這行代碼

//app.UseHttpsRedirection();

錯誤2

安裝了服務(wù)怎么都無法開啟
Kestrel封裝在WindowService中(.net5,.net6,.net7三個版本的介紹)

解決辦法:因為沒有證書,所以不配置https的終結(jié)點就可以了。

運行效果如下圖

Kestrel封裝在WindowService中(.net5,.net6,.net7三個版本的介紹)

.NET7版本(和6版本一樣就可以)

源碼下載

https://download.csdn.net/download/iml6yu/87377783

在 Windows 服務(wù)中托管 ASP.NET Core文章來源地址http://www.zghlxwxcb.cn/news/detail-482595.html

到了這里,關(guān)于Kestrel封裝在WindowService中(.net5,.net6,.net7三個版本的介紹)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • net6支持的windows版本

    net6支持的windows版本

    微軟官方文檔https://learn.microsoft.com/zh-cn/dotnet/core/install/windows?tabs=net60 .NET 6 支持下列 Windows 版本: (OS)-------------------------------------Version-----------------------體系結(jié)構(gòu) Windows 11---------------------------21H2--------------------------x64、Arm64 Windows 10 客戶端 -----------------1607±-----------------------x

    2024年02月07日
    瀏覽(20)
  • .Net6 Web Core API --- AOP -- log4net 封裝 -- MySQL -- txt

    .Net6 Web Core API --- AOP -- log4net 封裝 -- MySQL -- txt

    目錄 一、引入 NuGet 包 二、配置log4net.config?? 三、編寫Log4net封裝類 四、編寫日志記錄類 五、AOP -- 攔截器 -- 封裝 六、案例編寫 七、結(jié)果展示 log4net? Microsoft.Extensions.Logging.Log4Net.AspNetCore? ? MySql.Data? ? ? ? ?----? MySQL數(shù)據(jù)庫需要 Newtonsoft.Json Autofac Autofac.Extensions.DependencyInj

    2024年02月14日
    瀏覽(29)
  • 使用Autofac進(jìn)行服務(wù)注冊,適用版本.Net6(程序集、泛型)

    具體的也可以去參考官網(wǎng):https://autofac.readthedocs.io/en/latest/integration/aspnetcore.html 首先在Program.cs所屬的層中引用nuget包: nuget網(wǎng)址:https://www.nuget.org/packages? 可以使用NuGet包管理器進(jìn)行搜索安裝 在Program.cs中加入如下代碼: 代碼中SmartHealthcare.Application可以替換為具體自己項目中Ap

    2024年02月16日
    瀏覽(24)
  • .NET6 獨立模式部署應(yīng)用程序(無需客戶機安裝指定版本.NET運行時)

    .NET6 獨立模式部署應(yīng)用程序(無需客戶機安裝指定版本.NET運行時)

    下圖對于.NET開發(fā)人員一定不陌生,尤其是CS架構(gòu),客戶電腦要運行基于.NET開發(fā)的程序,無論是使用C#,還是VB.NET、F#,發(fā)布后的程序的運行環(huán)境都需要有相應(yīng)版本的.NET的運行時,否則應(yīng)用程序?qū)o法正常運行。 BS架構(gòu)下,在服務(wù)器上安裝指定版本.NET運行時,工作量可以忽略不

    2024年02月11日
    瀏覽(32)
  • Kestrel封裝在Winform中

    Kestrel封裝在Winform中

    另外一篇文章 Kestrel封裝在WindowService中(.net5,.net6,.net7三個版本的介紹) 在很久以前為了滿足需求,已經(jīng)開發(fā)了一款winform程序,并且是4.6.1版本的,如今為了和第三方對接,需要在這個winform上提供WebAPI的接口。因為第三方的程序是一份沒有源碼的程序。 網(wǎng)上有很多自寫web服

    2024年02月04日
    瀏覽(41)
  • 什么是 .Net5?.Net5和.Net Core 有什么關(guān)系?

    什么是 .Net5?.Net5和.Net Core 有什么關(guān)系?

    2021年即將結(jié)束,使用 .net開發(fā)已經(jīng)有多年的經(jīng)驗,微軟自2016年發(fā)布 .net core1.0 之后,.net core的熱度蒸蒸日上,asp.net core3.1 的性能以及穩(wěn)定性也超越了java,特別是云原生開發(fā)這一塊,看的出 .net core有很好的前景,但目前國內(nèi)的熱度不夠,大部分公司還是在使用.net framework ,而

    2024年02月11日
    瀏覽(86)
  • .NET5從零基礎(chǔ)到精通:全面掌握.NET5開發(fā)技能

    .NET5從零基礎(chǔ)到精通:全面掌握.NET5開發(fā)技能

    C#版本新語法-官網(wǎng): C#7:https://docs.microsoft.com/zh-cn/dotnet/csharp/whats-new/csharp-7 C#8:https://docs.microsoft.com/zh-cn/dotnet/csharp/whats-new/csharp-8 C#9:https://docs.microsoft.com/zh-cn/dotnet/csharp/whats-new/csharp-9 章節(jié) 第一章:https://www.cnblogs.com/kimiliucn/p/17613434.html 第二章:https://www.cnblogs.com/kimiliucn/p

    2024年02月14日
    瀏覽(47)
  • 記錄一次.NET6環(huán)境使用Visual Studio 2022 V17.6.2版本的異常

    記錄一次.NET6環(huán)境使用Visual Studio 2022 V17.6.2版本的異常

    C#開發(fā)環(huán)境Visual Studio 2022 V17.6.2版本。 .NET 6.0 系統(tǒng)是Blazor Server框架的系統(tǒng)頁面,在使用Visual Studio 2022 V17.6.2版本編譯后,執(zhí)行出現(xiàn): 先使用了Visual Studio 2022 V17.4.0版本編譯后可以正常。 經(jīng)過分析:Visual Studio 2022 V17.4.0還在使用的目標(biāo)框架為:.NET 6.0,Visual Studio 2022 V17.6.2版本的

    2024年02月08日
    瀏覽(26)
  • .NET5從零基礎(chǔ)到精通:全面掌握.NET5開發(fā)技能【第一章】

    .NET5從零基礎(chǔ)到精通:全面掌握.NET5開發(fā)技能【第一章】

    C#版本新語法-官網(wǎng): C#7:https://docs.microsoft.com/zh-cn/dotnet/csharp/whats-new/csharp-7 C#8:https://docs.microsoft.com/zh-cn/dotnet/csharp/whats-new/csharp-8 C#9:https://docs.microsoft.com/zh-cn/dotnet/csharp/whats-new/csharp-9 章節(jié) 第一章:https://www.cnblogs.com/kimiliucn/p/17613434.html 第二章:https://www.cnblogs.com/kimiliucn/p

    2024年02月13日
    瀏覽(18)
  • .NET5從零基礎(chǔ)到精通:全面掌握.NET5開發(fā)技能【第二章】

    .NET5從零基礎(chǔ)到精通:全面掌握.NET5開發(fā)技能【第二章】

    章節(jié) 第一章:https://www.cnblogs.com/kimiliucn/p/17613434.html 第二章:https://www.cnblogs.com/kimiliucn/p/17620153.html 第三章:https://www.cnblogs.com/kimiliucn/p/17620159.html 5.1-使用Session 5.2-Log4Net組件使用 (1)管理Nuget程序,下載【log4net】和【Microsoft.Extensions.Logging.Log4Net.AspNetCore】 (2)新建一個文件

    2024年02月13日
    瀏覽(26)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包