Nor*_*ini 7 .net c# asp.net-core-mvc asp.net-core-2.0
我正在尝试使用Visual Studio 2017(v15.4.5)将现有的WCF Web API(以.NET Framework 4.6.1为目标)移植到ASP.Net Core 2,并且无法确定组织项目的良好/通用/支持方式,以及他们应该是什么类型的项目.
例如:
用于服务器和主机的单个项目,或用于托管的服务器和控制台应用程序的类库
用于控制台应用程序的Windows经典桌面项目和(新样式csproj)服务器库或两个Web应用程序的Web应用程序(一个输出类库,另一个输出控制台应用程序)
我有一种潜在的怀疑,即我缺少一些基本的东西,这意味着一种方法应该优于其他方法,或者甚至是强制性的,但是发现很难从ASP.Net Core 2文档中发现这种方法.
应该针对.NET Framework 4.6.1
因为我需要从Web API的内部访问Windows注册表,所以我的目标是.NET Framework 4.6.1而不是.NET Core.
主机使用HTTP.sys
我打算将来使用Windows身份验证,并相信Kestrel不支持(基于HTTP.sys功能).
主机应作为Windows服务运行
最终,Web API应由Windows Installer安装并作为服务运行.
目前,我只是想让API独立构建/工作,但也希望能够深入了解项目选择如何影响发布/部署.
虽然默认情况下,新的ASP.NET Core 2 Web应用程序是输出控制台应用程序的单个项目,但我更倾向于将解决方案拆分为2个主要项目:
这种安排对我来说似乎很合适,因为任何单元/集成测试项目都可以引用"Server"类库,而不是可执行文件(根据建议,在C#项目中引用exe文件是不好的做法).
此外,我认为这是合理的,因为我可以在项目属性中将输出类型更改为"类库".
我假设,因为我的目标是.Net Framework,只要我安装与"Server"项目相同的NuGet包,就没有理由认为"Host"项目不能是Windows Classic桌面控制台应用程序.
是否有任何隐藏的问题,可能与发布/部署有关,这使得这种安排成为一个坏主意?使Hosting项目成为Web应用程序(仍会引用Server项目)会有什么好处吗?
以下是我用来尝试实现我首选项目组织的步骤.虽然它有效,但我会在构建期间收到警告 - 见下文.
添加新项目"服务器"
选择Visual C#,Web,ASP.NET Core Web Application
在以下对话框中,以.NET Framework,ASP.NET Core 2.0为目标,然后选择Web API
将Program.cs的内容替换为:
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Logging;
using Server;
namespace Host
{
internal class Program
{
private static void Main(string[] args)
{
BuildWebHost(args).Run();
}
private static IWebHost BuildWebHost(string[] args) =>
new WebHostBuilder()
.UseStartup<Startup>()
.UseHttpSys(options =>
{
options.UrlPrefixes.Add("http://localhost:8080/");
})
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddConsole();
logging.AddDebug();
})
.Build();
}
}
Run Code Online (Sandbox Code Playgroud)添加对Server项目的引用
Microsoft.AspNetCore (2.0.1)Microsoft.AspNetCore.Mvc (2.0.1)Microsoft.AspNetCore.Server.HttpSys (2.0.2)此解决方案在调试下运行,并正确响应http://localhost:8080/api/values和http://localhost:8080/api/values/1,但在构建解决方案后会显示以下警告:
Warning The referenced component 'System.Security.Cryptography.Encoding' could not be found. Host
Warning The referenced component 'System.Xml.XPath' could not be found. Host
Warning The referenced component 'System.IO.FileSystem.Primitives' could not be found. Host
Warning The referenced component 'System.IO.FileSystem' could not be found. Host
Warning The referenced component 'System.Diagnostics.StackTrace' could not be found. Host
Warning The referenced component 'System.Xml.XmlDocument' could not be found. Host
Warning The referenced component 'System.Security.Cryptography.X509Certificates' could not be found. Host
Warning The referenced component 'System.Diagnostics.FileVersionInfo' could not be found. Host
Warning The referenced component 'System.Security.Cryptography.Algorithms' could not be found. Host
Warning The referenced component 'System.Xml.ReaderWriter' could not be found. Host
Warning The referenced component 'System.Security.Cryptography.Primitives' could not be found. Host
Warning The referenced component 'System.ValueTuple' could not be found. Host
Warning The referenced component 'System.Xml.XPath.XDocument' could not be found. Host
Warning The referenced component 'System.IO.Compression' could not be found. Host
Warning The referenced component 'System.AppContext' could not be found. Host
Warning The referenced component 'System.Threading.Thread' could not be found. Host
Warning The referenced component 'System.Console' could not be found. Host
Run Code Online (Sandbox Code Playgroud)
如果这是一个合理的项目安排,为什么我会收到这些警告?
我们正在做类似的事情。我们的宿主是一个 .NET Framework 4.7.1 项目:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net471</TargetFramework>
<IsPackable>true</IsPackable>
<PlatformTarget>x86</PlatformTarget>
<OutputType>Exe</OutputType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNet.WebApi.Core" Version="5.2.6" />
<PackageReference Include="Microsoft.AspNetCore" Version="2.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="2.1.0" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.1.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.1.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="2.1.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.0" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Business.csproj" />
<ProjectReference Include="..\Apis.Shared.csproj" />
<ProjectReference Include="..\Apis.Contracts.csproj" />
</ItemGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)
而我们的库项目是NetStandard2.0:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>full</DebugType>
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)
Program.cs 看起来像这样:
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.ConfigureServices(services => services.AddAutofac())
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
Run Code Online (Sandbox Code Playgroud)
Startup.cs 看起来像这样:
public class Startup
{
public Startup(IHostingEnvironment hostingEnvironment)
{
Settings = new AppSettings(hostingEnvironment);
}
public AppSettings Settings { get; private set; }
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddOptions();
// any other services registrations...
var builder = new ContainerBuilder();
// all autofac registrations...
builder.Populate(services);
return new AutofacServiceProvider(builder.Build(););
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
}
}
Run Code Online (Sandbox Code Playgroud)