IIS

Install the ASP.NET Core Module/Hosting Bundle

前后端独立部署

后端API 和 前端程序分开部署,对于前后端没有任何限制。

此方法的限制:需要给前端和后端分配单独的域名,具有跨域问题需要配置跨域,因为有跨域,在调用API时还有会额外的 HTTP OPTIONS 请求。

Kestrel

此方法是将前端项目发布后,Copy 到后端 WebApi 项目下的 wwwroot 目录下(没有就新建),让 Kestrel 来同时提供 api 和 前端静态资源服务,适合内部使用小型项目,不建议用在中大型项目。

此方法的限制:前端必须使用基于 hash 的路由方式,基于 history 的不行;后端 WebApi 项目需要添加静态文件中间件和默认文件中间件
这样进行发布后,即便是打包成 Docker 镜像也只需要一个,比较方便。

此方法部署没有跨域问题,后端无需配置跨域,没有额外的 HTTP OPTIONS 请求。

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseDefaultFiles();
app.UseStaticFiles();
}

Nginx 全代理法

此方法是 nginx 根据请求路径来指向前端资源或者代理后端 api,和上面的方法一样,也只使用一个域名,没有跨域问题、

此方法的限制:后端必须设置给 api 设置统一的前缀。

api 的前缀,是自定义的,一般以 api作为前缀,例如:/api/apple/add。

最好本地vs发布环境和服务器上的core环境相一致。vs 发布环境安装 SDK3.1.2。IIS服务器上安装 core Runtime 3.1.2和Hosting Bundle 3.1.2 这2个安装包。(注意,如果iis服务器环境安装了以前版本的core,比如说3.0,发布后可能会有500错误。删除旧版本统一新版本解决

https://dotnet.microsoft.com/en-us/download/dotnet/3.1

安装IIS和Windows Server Hosing之后 ,重启IIS服务,检查IIS “Modules” 中是否有 “AspNetCoreModuleV2”
Application pool 设置 No Managed Code, pipe line mode: integrated
advanced settings: 32bit-application: false

Linux

  • Nginx
  • Apache
    Windows
  • IIS
  • Windows Service

The Kestrel web server is a new web server as part of ASP.NET Core.
It is now the preferred web server for all new ASP.NET applications.

it is still recommended to use IIS, Apache, or NGINX as a reverse proxy in front of it.

20220720_113459.png

Q&A

System.UnauthorizedAccessException: Filename: redirection.config
Error: Cannot read configuration file due to insufficient permissions

THE SOLUTION
The service account for the IIS App Pool does not have the required read rights on folder %windows%\system32\inetsrv\config.

Therefore, You must grant the account Read rights on this folder and then recycle the App Pool in use for the Log API. As an alternative, you should make sure the account is part of the local IIS_IUSRS group and then assign this group read rights on the folder with the redirection.config file.

查看表生成的DDL

show create table table_name;

鸡翅根洗净,泡水20分钟,备好葱姜蒜,冰糖
用刀划几下,方便入味。
冷水下锅翅根过水,去掉浮沫和血水,淋干。
热锅凉油,放入冰糖化开,下入鸡翅根翻炒,放入姜蒜,蚝油,翻炒均匀,料酒去腥
倒入开水,没过翅根,可以在15分时放些土豆
炖20分钟左右,注意中间翻炒防止糊底,大火收汁即可。

database first

use entity generate models

// toolset of entity framework
dotnet tool install --global dotnet-ef
dotnet tool update --global dotnet-ef

// install package
Pomelo.EntityFrameWorkCore.MySql
Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.Design

// dotnet command reference https://docs.microsoft.com/en-us/ef/core/cli/dotnet
// change to project folder that contains .csproj file, not sln file's folder, output files will be present in models folder
dotnet ef dbcontext scaffold "Server=192.168.0.129;User=root;Password=lanbe123;Database=monitor" "Pomelo.EntityFrameworkCore.MySql" -o ./models

// 在生成的文件夹里面找到 monitorContext.cs 里面有生成的工具类

// Startup.cs
public void ConfigureServices(IServiceCollection services)
{
// Replace with your connection string.

services.AddDbContext<monitorContext>(
dbContextOptions => dbContextOptions
.UseMySql(Configuration["ConnectionStrings:ConnMySql"], new MySqlServerVersion(new Version(5, 7, 19)))
// The following three options help with debugging, but should
// be changed or removed for production.
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging()
.EnableDetailedErrors()
);

services.AddControllers();
}

EF query

Entity framework supports three types of queries: 1) LINQ-to-Entities, 2) Entity SQL, and 3) Native SQL

Entity SQL

Entity SQL is another way to create a query. It is processed by the Entity Framework’s Object Services directly. It returns ObjectQuery instead of IQueryable.
You need an ObjectContext to create a query using Entity SQL.
The following code snippet shows the same query result as the LINQ query above.

//Querying with Object Services and Entity SQL
string sqlString = "SELECT VALUE st FROM SchoolDBEntities.Students " +
"AS st WHERE st.StudentName == 'Bill'";

var objctx = (ctx as IObjectContextAdapter).ObjectContext;

ObjectQuery<Student> student = objctx.CreateQuery<Student>(sqlString);
Student newStudent = student.First<Student>();

Native SQL

using (var ctx = new SchoolDBEntities())
{
var studentName = ctx.Students.SqlQuery("Select studentid, studentname, standardId from Student where studentname='Bill'").FirstOrDefault<Student>();
}

delete

var us = db.user.FirstOrDefault(x => x.uid == uid);

db.user.Attach(us);
db.user.Remove(us);

use serilog for asp.net core 3.1

appsettings.json

"AllowedHosts": "*",
"Serilog": {
"Using": [ "Serilog.Sinks.File", "Serilog.Sinks.Async" ],
"MinimumLevel": {
"Default": "Information"
},
"Override": {
"Microsoft": "Warning",
"System": "Warning",
"Microsoft.AspNetCore": "Warning"
},
"WriteTo": [
{
"Name": "AsyncFile",
"Args": {
"configure": [
{
"Name": "File",
"Args": {
"path": "Logs/serilogDemo_.log",
"rollingInterval": "Day"
}
}
]
}
}
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
"Properties": {
"Application": "KVMServerApi"
}
},
public class Program
{
public static IConfiguration Configuration { get; } = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true)
.AddJsonFile($"appsettings.{Environment.MachineName}.json", optional: true)
.AddEnvironmentVariables()
.Build();

public static void Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(Configuration)
.CreateLogger();

try
{
Log.Information("Starting up");
CreateHostBuilder(args).Build().Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "Application start-up failed");
}
finally
{
Log.CloseAndFlush();
}

//CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSerilog()
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
public class MsgController : BaseController
{
private readonly ILogger<MsgController> _logger;

public MsgController(IRegInfoService ri, IRedisCacheClient redisCacheClient , ILogger<MsgController> logger) : base(ri,redisCacheClient)
{
_logger = logger;
}
...
_logger.LogInformation("GetSystem executing...");
}

using MySql.Data.MySqlClient;

MySql.Data
MySql.Data.EntityFrameworkCore


String connetStr = "server=192.168.0.129;port=3306;user=root;password=lanbe123; database=monitor;";
MySqlConnection con = new MySqlConnection(connetStr);

con.Open();
Console.WriteLine("数据库连接成功");

MySqlCommand cmd = new MySqlCommand("select * from module", con);
IDataReader read = cmd.ExecuteReader();
while (read.Read())
{
Console.WriteLine(read[0].ToString());
Console.WriteLine(read[1].ToString());
Console.WriteLine(read[2].ToString());
}
con.Close();

data types

Strings

服务端:
redis

客户端:
目前使用 newlife.redis 尚未遇到问题

https://newlifex.com/core/redis

//默认读取配置文件:ConnectionStrings:Redis
services.AddRedisCacheManager();

//指定链接字符串
services.AddRedisCacheManager("server=127.0.0.1:6379;password=xxx;db=4");



private readonly ILogger<Worker> _logger;
private readonly IRedisCacheManager _redisCacheManager;

public Worker(ILogger<Worker> logger, IRedisCacheManager redisCacheManager)
{
_logger = logger;
this._redisCacheManager = redisCacheManager;

var data = _redisCacheManager.Get<string>("test");
}

StackExchange.Redis 这个有 timeout 问题
还有个 ServiceStack.Redis 有限制 6000次/小时访问,不推荐使用
ServiceStack.LicenseException: ‘The free-quota limit on ‘6000 Redis requests per hour’ has been reached. Please see https://servicestack.net to upgrade to a commercial license or visit https://github.com/ServiceStackV3/ServiceStackV3 to revert back to the free ServiceStack v3.’

扩展提供更多方便的接口
https://github.com/imperugo/StackExchange.Redis.Extensions

安装 Install-Package StackExchange.Redis.Extensions.Newtonsoft 包就会自动安装依赖包了

Q&A

https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/development-time-iis-support?view=aspnetcore-3.1

Visual Studio may prompt a restart if not running as an administrator. If prompted, restart Visual Studio.

Enable development-time IIS support in Visual Studio

Launch the Visual Studio installer.

Select Modify for the Visual Studio installation that you plan to use for IIS development-time support.

For the ASP.NET and web development workload, locate and install the Development time IIS support component.

The component is listed in the Optional section under Development time IIS support in the Installation details panel to the right of the workloads. The component installs the ASP.NET Core Module, which is a native IIS module required to run ASP.NET Core apps with IIS.

IIS launch profile

Create a new launch profile to add development-time IIS support:

Right-click the project in Solution Explorer. Select Properties. Open the Debug tab.

For Profile, select the New button. Name the profile “IIS” in the popup window. Select OK to create the profile.

For the Launch setting, select IIS from the list.

Select the checkbox for Launch browser and provide the endpoint URL.

When the app requires HTTPS, use an HTTPS endpoint (https://). For HTTP, use an HTTP (http://) endpoint.

Provide the same host name and port as the IIS configuration specified earlier uses, typically localhost.

Provide the name of the app at the end of the URL.

For example, https://localhost/WebApplication1 (HTTPS) or http://localhost/WebApplication1 (HTTP) are valid endpoint URLs.

In the Environment variables section, select the Add button. Provide an environment variable with a Name of ASPNETCORE_ENVIRONMENT and a Value of Development.

In the Web Server Settings area, set the App URL to the same value used for the Launch browser endpoint URL.

For the Hosting Model setting in Visual Studio 2019 or later, select Default to use the hosting model used by the project. If the project sets the property in its project file, the value of the property (InProcess or OutOfProcess) is used. If the property isn’t present, the default hosting model of the app is used, which is in-process. If the app requires an explicit hosting model setting different from the app’s normal hosting model, set the Hosting Model to either In Process or Out Of Process as needed.

Save the profile.

Q&A

Config Error: Cannot read configuration file due to insufficient permissions
Config File: C:\Users\me\Documents\wwwroot\httpdocs\web.config

You need to give permission to C:\Windows\System32\inetsrv\config folder to IIS_IUSRS user.

w3wp.exe keeping files locked after processing in webservice

Try to either recycle the AppPool which your webservices runs under, or use iisreset.

iisreset /stop will stop IIS and release the dll’s. iisreset /start will start IIS back up again.

{
"virtualPath": {
"videos": "C:\\Users\\Dev\\Documents\\videos"
},
"runtimeEnv": {
"isDocker": true
}
}
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IConfiguration>(Configuration);
}

// 使用
public class ValuesController : Controller
{
IConfiguration configuration;

public ValuesController(IConfiguration configuration)
{
this.configuration = configuration;
}
}