部署dotnet Core Web API
上一步的文章,是我们公司最核心的一个ORM组件,在中标麒麟系统完成了一个插入数据的任务,这一步是将正式的从dot net framework 迁移到 dotnet core 的项目,部署运行在中标麒麟系统中。
项目预览
现有从dotnet framework 迁移过来的一个 dotnet core web api 项目,准备部署中标麒麟系统上测试运行,下面是项目截图:
运行 & 解决
将文件上传到服务器上,然后执行命令运行:
[root@gumis02 API]# dotnet Beyondbit.One.BUA.API.dll
Error:
An assembly specified in the application dependencies manifest (Beyondbit.One.BUA.API.deps.json) was not found:
package: 'Microsoft.ApplicationInsights.AspNetCore', version: '2.1.1'
path: 'lib/netstandard1.6/Microsoft.ApplicationInsights.AspNetCore.dll'
This assembly was expected to be in the local runtime store as the application was published using the following target manifest files:
aspnetcore-store-2.0.0-linux-x64.xml;aspnetcore-store-2.0.0-osx-x64.xml;aspnetcore-store-2.0.0-win7-x64.xml;aspnetcore-store-2.0.0-win7-x86.xml
[root@gumis02 API]#
果不既然错误可能会迟到,但是绝不缺席,显示的错误是在开发过程中应用的nuget dll 找不到。这个问题是个小问题,已经有同学写了文章,Fix *.deps.json was not found,文章提到的解决办法:
增加如下一行到 csjproj文件中即可:
最后csjproj文件看上去像这个样子:
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<PublishWithAspNetCoreTargetManifest>false</PublishWithAspNetCoreTargetManifest>
</PropertyGroup>
重新发布项目,然后运行上面的问题已经解决,出现其他的问题:
[root@gumis02 API]# dotnet Beyondbit.One.BUA.API.dll
warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
No XML encryptor configured. Key {0372b52e-5124-4655-871b-906a0467a642} may be persisted to storage in unencrypted form.
Application startup exception: System.InvalidOperationException: No service for type 'Microsoft.AspNetCore.Http.IHttpContextAccessor' has been registered.
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
at Beyondbit.Framework.StaticHttpContextExtensions.UseStaticHttpContext(IApplicationBuilder app, HttpContextOptions options)
at Beyondbit.One.BUA.API.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env) in E:\u5de5u4f5cu533a 3 u4e92u8054u7f51 3 u7edfu4e00u7cfbu5217 1_TFS 3u7248u672cu7ba1u7406Beyondbit E-GOV Base System Suite\u7edfu4e00u6388u6743CoreV1Beyondbit.One.BUA.APIStartup.cs:line 49
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.Configure(IApplicationBuilder app)
at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
crit: Microsoft.AspNetCore.Hosting.Internal.WebHost[6]
Application startup exception
System.InvalidOperationException: No service for type 'Microsoft.AspNetCore.Http.IHttpContextAccessor' has been registered.
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
at Beyondbit.Framework.StaticHttpContextExtensions.UseStaticHttpContext(IApplicationBuilder app, HttpContextOptions options)
at Beyondbit.One.BUA.API.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env) in E:\u5de5u4f5cu533a 3 u4e92u8054u7f51 3 u7edfu4e00u7cfbu5217 1_TFS 3u7248u672cu7ba1u7406Beyondbit E-GOV Base System Suite\u7edfu4e00u6388u6743CoreV1Beyondbit.One.BUA.APIStartup.cs:line 49
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.Configure(IApplicationBuilder app)
at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
Unhandled Exception: System.InvalidOperationException: No service for type 'Microsoft.AspNetCore.Http.IHttpContextAccessor' has been registered.
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
at Beyondbit.Framework.StaticHttpContextExtensions.UseStaticHttpContext(IApplicationBuilder app, HttpContextOptions options)
at Beyondbit.One.BUA.API.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env) in E:\u5de5u4f5cu533a 3 u4e92u8054u7f51 3 u7edfu4e00u7cfbu5217 1_TFS 3u7248u672cu7ba1u7406Beyondbit E-GOV Base System Suite\u7edfu4e00u6388u6743CoreV1Beyondbit.One.BUA.APIStartup.cs:line 49
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.Configure(IApplicationBuilder app)
at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
at Beyondbit.One.BUA.API.Program.Main(String[] args) in E:\u5de5u4f5cu533a 3 u4e92u8054u7f51 3 u7edfu4e00u7cfbu5217 1_TFS 3u7248u672cu7ba1u7406Beyondbit E-GOV Base System Suite\u7edfu4e00u6388u6743CoreV1Beyondbit.One.BUA.APIProgram.cs:line 17
u5df2u653eu5f03 (core dumped)
[root@gumis02 API]#
错误提示确实依赖:
在Starpup.cs 加入一段话:
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
如:
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddMvc(options =>
{
options.Filters.Add<IdentityActionFilter>();
options.Filters.Add<JsonpActionFilter>();
});
services.AddDistributedMemoryCache();
services.AddSession();
services.AddCors();
}
最后运行成功了:
[root@gumis02 API]# dotnet Beyondbit.One.BUA.API.dll
Hosting environment: Production
Content root path: /var/ftp/uffice/BUA One/API
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.
增加多域名,多IP支持
如上面的步骤,在服务器内部使用http://localhost:5000,但是在服务器外用 http://服务器IP:5000 访问的时候访问不到,而且是防火墙在禁用的情况下。 在服务器查看端口开放,使用以下命令:
[root@gumis02 ~]# netstat -an | grep 5000
tcp 0 0 127.0.0.1 5000 :::* LISTEN
tcp 0 0 ::1 5000 :::* LISTEN
[root@gumis02 ~]#
可以看到服务器5000端口是针对 127.0.0.1和localhost才可以访问到,这两个都是在服务器内部使用的。
按照.net core 启动域名及端口配置 文章中 linux环境一节说明的配置,将代码改造以下,支持在服务器外访问。
- 首先在解决方案中添加一个文件hosting.josn,内容如下:
{
"server.urls": "http://*:5000"
}
然后在program.cs文件中修改启动代码:
namespace Beyondbit.One.BUA.API
{
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args)
{
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("hosting.json", optional: true)
.Build();
var host = new WebHostBuilder()
.UseKestrel()
.UseConfiguration(config)
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
return host;
}
}
}
编译发布到服务器上,运行站点,然后查看命令:
[root@gumis02 ~]# netstat -an | grep 5000
tcp 0 0 ::: 5000 :::* LISTEN
[root@gumis02 ~]#
可以看到 5000前面变成了 :::,而不是第一次的127.0.0.1或::1,这代表5000端口任何ip指向到这个服务器,都可以通过IP:5000访问,在服务器外用浏览器测试看到效果正常。