appsettings.json Copy to Output Directory

在vscode中遇到问题,读取不到配置文件,发现appsettings.json并未复制到编译的目录中,所以,在project.csproj中添加一下代码

<ItemGroup>
    <Content Include="appsettings.json">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
      <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
    </Content>
  </ItemGroup>

重新编译后可以复制此文件到目标目录。

WSL-Debian/Ubuntu系统tail -f 命令无法查看文件更新

tail -f命令可以查看文件更新的记录,但是在wsl中,可能无法正常工作。

经查找发现,Linux是通过inotify来获取文件变动的,但是不知道是bug还是什么原因,感知不到文件变动,造成此问题。

解决方案:使用tail -f ---disable-inotify 2019-02-21.log这个命令开查看日志,就可以了!

补充:在wsl中感应Windows系统中的文件变化,需要加上 —disable-inotify 此参数

Linux修改时区

时区文件位于`/etc/localtime`

系统的时区文件位于/usr/share/zoneinfo目录下,所以,需要做一个软连接,(centos)

ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
退出再重新登陆下,就可以了.

PS: 之前遇到问题,按照以上修改后仍不能改变时间,于是在看`/etc/profile` 文件中,发现有名为TZ的环境变量,修改此变量TZ=’Asia/Shanghai’; export TZ 保存退出,执行命令:source /etc/profile 。

Linux配置.Net Core环境

  1. 安装.net sdk

执行命令sudo pacman -S dotnet-sdk(这里用的manjaro)

运行完后,运行dotnet --info 查看版本,显示如下,说明安装成功。

.NET Core SDK (reflecting any global.json):
 Version:   2.2.102
 Commit:    96ff75a873

Runtime Environment:
 OS Name:     manjaro
 OS Version:  
 OS Platform: Linux
 RID:         arch-x64
 Base Path:   /opt/dotnet/sdk/2.2.102/

Host (useful for support):
  Version: 2.2.1
  Commit:  878dd11e62

.NET Core SDKs installed:
  2.2.102 [/opt/dotnet/sdk]

.NET Core runtimes installed:
  Microsoft.NETCore.App 2.2.1 [/opt/dotnet/shared/Microsoft.NETCore.App]

To install additional .NET Core runtimes or SDKs:
  https://aka.ms/dotnet-download

2. 创建项目

执行命令 dotnet new web -o web 这句命令的意思是,创建一个ASP. NET Core Empty项目,并将该项目命名为web,显示一下信息说明项目创建成功

The template "ASP.NET Core Empty" was created successfully.

Processing post-creation actions...
Running 'dotnet restore' on weba/web.csproj...
  Restoring packages for /home/txl/csharp/weba/web.csproj...
  Generating MSBuild file /home/txl/csharp/weba/obj/web.csproj.nuget.g.props.
  Generating MSBuild file /home/txl/csharp/weba/obj/web.csproj.nuget.g.targets.
  Restore completed in 2.3 sec for /home/txl/csharp/web/web.csproj.

Restore succeeded.

3. 运行项目

运行dotnet build  命令,先编译下项目

Microsoft (R) Build Engine version 15.9.20.63311 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  Restore completed in 609.99 ms for /home/txl/csharp/weba/weba.csproj.
  webtestapi -> /home/txl/csharp/weba/bin/Debug/netcoreapp2.2/weba.dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:03.70

在运行项目,dotnet run

第一运行时可能会出现错误:

info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]
      User profile is available. Using '/home/txl/.aspnet/DataProtection-Keys' as key repository; keys will not be encrypted at rest.
crit: Microsoft.AspNetCore.Server.Kestrel[0]
      Unable to start Kestrel.
System.InvalidOperationException: Unable to configure HTTPS endpoint. No server certificate was specified, and the default developer certificate could not be found.
To generate a developer certificate run 'dotnet dev-certs https'. To trust the certificate (Windows and macOS only) run 'dotnet dev-certs https --trust'.
For more information on configuring HTTPS see https://go.microsoft.com/fwlink/?linkid=848054.
   at Microsoft.AspNetCore.Hosting.ListenOptionsHttpsExtensions.UseHttps(ListenOptions listenOptions, Action`1 configureOptions)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.AddressesStrategy.BindAsync(AddressBindContext context)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindAsync(IServerAddressesFeature addresses, KestrelServerOptions serverOptions, ILogger logger, Func`2 createBinding)
   at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer.StartAsync[TContext](IHttpApplication`1 application, CancellationToken cancellationToken)

Unhandled Exception: System.InvalidOperationException: Unable to configure HTTPS endpoint. No server certificate was specified, and the default developer certificate could not be found.
To generate a developer certificate run 'dotnet dev-certs https'. To trust the certificate (Windows and macOS only) run 'dotnet dev-certs https --trust'.
For more information on configuring HTTPS see https://go.microsoft.com/fwlink/?linkid=848054.
   at Microsoft.AspNetCore.Hosting.ListenOptionsHttpsExtensions.UseHttps(ListenOptions listenOptions, Action`1 configureOptions)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.AddressesStrategy.BindAsync(AddressBindContext context)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindAsync(IServerAddressesFeature addresses, KestrelServerOptions serverOptions, ILogger logger, Func`2 createBinding)
   at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer.StartAsync[TContext](IHttpApplication`1 application, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.StartAsync(CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Hosting.WebHostExtensions.RunAsync(IWebHost host, CancellationToken token, String shutdownMessage)
   at Microsoft.AspNetCore.Hosting.WebHostExtensions.RunAsync(IWebHost host, CancellationToken token)
   at Microsoft.AspNetCore.Hosting.WebHostExtensions.Run(IWebHost host)
   at webtestapi.Program.Main(String[] args) in /home/txl/csharp/weba/Program.cs:line 17

这个错误说明要安装一个证书,开启https,错误中已经给出提示,如果是linux系统,执行dotnet dev-certs https 命令。

如果是windows或mac系统,执行`dotnet dev-certs https –trust`。

运行dotnet dev-certs https, 出现以下问题。

Cannot find command 'dotnet dev-certs', please run the following command to install

dotnet tool install --global dotnet-dev-certs

所以,又执行dotnet tool install --global dotnet-dev-certs 命令,却又出现这个…

Tool 'dotnet-dev-certs' is already installed.

这里查了好久,发现dotnet-dev-certs是一个执行文件,位于$HOME/.dotnet/tools目录下,所以执行$HOME/.dotnet/tools/dotnet-dev-certs https,本以为没问题,结果又出现

A fatal error occurred, the required library libhostfxr.so could not be found.
If this is a self-contained application, that library should exist in [/home/txl/.dotnet/tools/.store/dotnet-dev-certs/2.2.0/dotnet-dev-certs/2.2.0/tools/netcoreapp2.2/any/].
If this is a framework-dependent application, install the runtime in the default location [/usr/share/dotnet] or use the DOTNET_ROOT environment variable to specify the runtime location.

到这里,真是有点崩溃了,于是再找之,发现需要设置环境变量DOTNET_ROOT

设置环境变量export DOTNET_ROOT=/opt/dotnet,这个目录是不是很眼熟,没错,

就是dotnet --info命令中的Bash Path的前半段。

然后在在执行命令$HOME/.dotnet/tools/dotnet-dev-certs https

The HTTPS developer certificate was generated successfully.

ok,终于看到希望了,再执行dotnet run ,成功!

项目发布,发布目标平台为linux x64,并且包含dotent 运行时,这意味着目标服务器可以不用装dotnet 环境也可以运行

dotnet publish -c Release -r linux-x64 --self-contained true

Couldn’t find a valid ICU package installed on the system

今天安装了.net core sdk,使用命令`dotnet –info`,出现错误

FailFast:
Couldn't find a valid ICU package installed on the system. Set the configuration flag System.Globalization.Invariant to true if you want to run with no globalization support.

   at System.Environment.FailFast(System.String)
   at System.Globalization.GlobalizationMode.GetGlobalizationInvariantMode()
   at System.Globalization.GlobalizationMode..cctor()
   at System.Globalization.CultureData.CreateCultureWithInvariantData()
   at System.Globalization.CultureData.get_Invariant()
   at System.Globalization.CultureInfo..cctor()
   at System.StringComparer..cctor()
   at System.AppDomain.InitializeCompatibilityFlags()
   at System.AppDomain.Setup(System.Object)
已放弃 (核心已转储)

于是发现,系统没有安装icu,遂安装之,`pacman -S icu`

再执行`dotnet –info`

.NET Core SDK (reflecting any global.json):
 Version:   2.2.102
 Commit:    96ff75a873

Runtime Environment:
 OS Name:     manjaro
 OS Version:  
 OS Platform: Linux
 RID:         arch-x64
 Base Path:   /opt/dotnet/sdk/2.2.102/

Host (useful for support):
  Version: 2.2.1
  Commit:  878dd11e62

.NET Core SDKs installed:
  2.2.102 [/opt/dotnet/sdk]

.NET Core runtimes installed:
  Microsoft.NETCore.App 2.2.1 [/opt/dotnet/shared/Microsoft.NETCore.App]

To install additional .NET Core runtimes or SDKs:
  https://aka.ms/dotnet-download

成功!

MariaDb忘记root密码

在MariaDB配置文件/etc/my.cnf ,加入 skip-grant-tables

重启数据库,`service mariadb restart` 。

执行命令 `mysql`,可以直接进入数据库cmd

切换到mysql数据库中,`use mysql;`

执行修改密码语句 `UPDATE user SET Password = password ( ‘123456’) WHERE User = ‘root’ ;`

完毕后,修改配置文件,将新加入的一行去掉,重启数据库

使用新的密码进入!

.Net获取配置信息

做开发时,需要读取配置文件信息。.Net项目已经有配置文件,比如:Web.config,App.config,要读取这些配置文件里的信息,需要引用`System.Configuration`

以appSettings为例,使用`System.Configuration.ConfigurationManager.AppSettings[keyName] ` 即可获取配置信息

Mariadb创建用户和授权

创建用户

命令: CREATE USER 'username'@'host' IDENTIFIED BY 'password';
说明:
username – 创建的用户名
host- 指定该用户在哪个主机上可以登陆,如果是本地可用为localhost, 可以从任意远程主机登陆,可以使用%
password
– 该用户的登陆密码,密码可以为空,如果为空则该用户可以不需要密码登陆服务器.

比如:

CREATE USER 'tester'@'localhost' IDENTIFIED BY '123456';
-- 创建仅能用于本地登录的账号tester,密码为123456
CREATE USER 'tester'@'%' IDENTIFIED BY '123456';
-- 创建可以在任何地方登陆䣌账号tester

授权

如果需要给用户权限,需要用到以下语法:`
GRANT privileges ON databasename.tablename TO ‘username’@’host’ `

privileges 表示权限,包括SELECT,UPDATE,DELETE,INSERT, 如果要授予所的权限则使用ALL

databasename – 数据库名

tablename – 表名

指定数据库所有表可以用database.*表示

GRANT ALL ON database.* TO ‘tester’@’%’;