我正在尝试使用 Visual Studio 2017 (v15.4.5) 将现有的 WCF Web API(针对 .NET Framework 4.6.1)移植到 ASP.Net Core 2,但在找出组织项目的良好/通用/受支持的方法时遇到困难,以及它们应该是什么类型的项目。
例如:
我偷偷怀疑我缺少一些基本的东西,这意味着一种方法应该比其他方法更受青睐,甚至是强制性的,但发现很难从ASP.Net Core 2 文档 https://learn.microsoft.com/en-us/aspnet/core/.
具体要求
应针对 .NET Framework 4.6.1
由于我需要从 Web API 的内部访问 Windows 注册表,因此我的目标是 .NET Framework 4.6.1 而不是 .NET Core。
使用 HTTP.sys 的主机
我打算将来使用 Windows 身份验证,并相信 Kestrel 不会支持它(基于HTTP.sys 功能 https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/httpsys).
主机应作为 Windows 服务运行
最终,Web API 应由 Windows Installer 安装并作为服务运行。
目前,我只是想让 API 独立构建/正确工作,但也希望了解项目选择如何影响发布/部署的任何见解。
首选解决方案
虽然默认情况下,新的 ASP.NET Core 2 Web 应用程序是输出控制台应用程序的单个项目,但我更愿意将解决方案拆分为 2 个主要项目:
- “服务器” - ASP.NET Core 2 (.NET Framework)类库包含Web API、控制器和Startup类的业务逻辑
- “Host” - 引用“Server”并负责托管 Web API 的控制台应用程序
这种安排对我来说似乎很合适,因为任何单元/集成测试项目都可以引用“服务器”类库,而不是可执行文件(根据在 C# 项目中引用 exe 文件是一种不好的做法吗 https://stackoverflow.com/questions/22835635/is-it-a-bad-practice-to-reference-an-exe-file-in-c-sharp-project/22836012#22836012).
此外,我相信这是合理的,因为我可以在项目属性中将输出类型更改为“类库”。
我假设由于我的目标是 .Net Framework,只要我安装与“服务器”项目相同的 NuGet 包,“主机”项目就没有理由不能成为 Windows 经典桌面控制台应用程序。
是否存在任何隐藏的问题(可能与发布/部署相关)使得这种安排成为一个坏主意?
将托管项目设为 Web 应用程序(仍会引用服务器项目)是否有任何优势?
Attempt
以下是我用来尝试实现我喜欢的项目组织的步骤。虽然它有效,但我在构建过程中收到了生成的警告 - 见下文。
Server
-
添加新项目“服务器”
- 将项目属性中的输出类型更改为“类库”
- 删除程序.cs
Host
- Add New Project "Host"
- 选择 Visual C#、Windows 经典桌面、控制台应用程序 (.NET Framework)
-
将 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();
}
}
添加对服务器项目的引用
- Install these NuGet packages
-
Microsoft.AspNetCore
(2.0.1)
-
Microsoft.AspNetCore.Mvc
(2.0.1)
-
Microsoft.AspNetCore.Server.HttpSys
(2.0.2)
- 设置为启动项目
问题:构建生成警告
该解决方案在调试中运行,并正确响应http://localhost:8080/api/values
and http://localhost:8080/api/values/1
, but构建解决方案后会显示以下警告:
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
如果这是一个合理的项目安排,为什么我会收到这些警告?