我有一个用 C# 为 Windows 编写的服务。我需要让它在 Linux 上运行。我已经研究过用 Java 或 C++ 重写代码的可能性,我很可能会在未来这样做,但作为一种中间解决方案,我正在考虑将项目移植到 Mono 或 .NET Core。关键是它是一个相当小的应用程序,因此进行必要的代码更改应该不会太麻烦。
该服务监听特定的 URL 端点以执行任务。
基于以下代码示例的示例:
http://localhost:5000/checkStatus?configId=1234
事实上,我几乎已经为这部分编译了 expect。这一部分正在生成我所有剩余的构建错误。我了解 WCF 服务在 .NET Core 中尚不可用。所以我一直在探索替代方案,但我很难找到在 .NET Core 中的 Linux 上运行的东西。我还想避免实现等同于重写整个内容或在完整的 NGINX 或 Apache 服务器中硬塞的东西。
namespace CheckService
{
[ServiceContract]
interface IService
{
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json)]
string checkStatus(string configId);
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json)]
string echo(string value);
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json)]
string version();
}
}
我也不完全熟悉 WCF,因为我从未使用过它,而且这个项目最初是由其他人编写的。
这是我的 project.json。我知道里面可能有一些不必要的或不正确的元素。我已经做了很多测试:
{
"buildOptions": {
"emitEntryPoint": true,
"debugType": "portable"
},
"dependencies" : {
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
"ServiceStack": "4.5.4",
"System": "4.1.311.2",
"System.IO": "4.3.0",
"System.IO.Pipes": "4.0.0",
"System.Collections": "4.0.11",
"System.Data.Common": "4.0.0",
"System.Data.SQLite": "1.0.0",
"System.ServiceModel": "1.0.0",
"System.ServiceModel.Web": "1.0.0",
"System.ServiceModel.Primitives": "4.3.0",
"System.ServiceModel.Security": "3.9.0",
"System.ServiceModel.Http": "4.0.0",
"slf4net.log4net": "0.1.32.1"
},
"frameworks": {
"net461": {
"frameworkAssemblies": {
"System.Runtime.Serialization": "4.0.0.0",
"System.ServiceModel": "4.0.0"
}
}
},
"tools": {
"Microsoft.AspNetCore.Server.IISIntegration.Tools": {
"version": "1.0.0-preview2-final"
}
},
"scripts": {
"postpublish": "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%"
}
}
还有其他方法吗?
最佳答案
您对 WebGetAttribute
的使用表明您打算创建一个 RESTful 服务。虽然 WCF 在 .NET Core 中不可用,但支持 WebApi。使用 WebApi,您可以创建 REST 服务,这可能就是您所需要的。你可以查看this tutorial by Microsoft关于如何开始这个。
关于C# 移植到 Linux Mono 或 .NET Core,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41351099/