请不要将此视为重复的问题。我打算将我的 ASP.NET RESTful web-api
(.Net Full)项目迁移到.Net Standard
或.Net Core
,但是我遇到了一个严重的问题,我找不到答案还有其他类似的问题。
我对这个问题的看法是在意识到可以选择.Net Core
的同时,为什么将我的库程序集定位在.Net Standard
上?
也许一个原因是使这些程序集可以随时在.Net Framework
项目中使用。无论如何,还有其他理由说服我将目标保持在.Net Standard
上吗?
注意:我不需要保留项目的.Net Full
版本,而只想拥有它的cross-platform
版本。
答案 0 :(得分:5)
别无选择。 .NET Standard仅适用于.NET类库。它不是运行时,因此无法创建针对它的应用程序。运行时是.NET Core和.NET Full框架。
.NET Core是唯一的跨平台选项。
ASP.NET Core本身是一个可以同时在两个运行时上运行的Web堆栈。您可以在public class Service1 : IService1
{
public string GetData(int value)
{
// send msg to clients
var hub = GlobalHost.ConnectionManager.GetHubContext<ServiceMonitorHub>();
hub.Clients.All.BroadcastMessage();
return string.Format("You entered: {0}", value);
}
对话框中选择一个。
对于库,选择取决于所需的内容。 .NET标准库可以针对任何兼容的运行时。 ASP.NET Core自己的Razor Class Libraries是.NET Standard 2.0项目。大多数NuGet软件包都与.NET Standard兼容,从而避免了在每次运行时打包不同版本的麻烦。
另一方面,最新的Span相关功能仅出现在.NET Core 2.1及更高版本中。它们aren't available in .NET Standard 2.0,但它们将出现在2.1中。来自All About Span: Exploring a New .NET Mainstay的此示例仅适用于.NET Core 2.1 +:
public partial class MainWindow : Window
{
private HubConnection Connection;
private IHubProxy HubProxy;
public MainWindow()
{
InitializeComponent();
Task.Run(ConnectAsync);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
using (var service = new ServiceReference1.Service1Client())
service.GetData(1);
}
public async Task ConnectAsync()
{
try
{
this.Connection = new HubConnection("http://localhost:59082/");
this.HubProxy = this.Connection.CreateHubProxy("ServiceMonitorHub");
HubProxy.On("BroadcastMessage", () => MessageBox.Show("Received message!"));
await this.Connection.Start();
}
catch (Exception ex)
{
}
}
}