C#端口到Linux单声道或.NET核心

问题描述:

我有用C#编写的用于Windows的服务。我需要让它在Linux上运行。我已经研究过使用Java或C++重写代码的可能性,我可能会这样做,但作为一个中间解决方案,我正在考虑将项目移植到Mono或.NET Core。关键是它是一个相当小的应用程序,因此进行必要的代码更改不应该太多。C#端口到Linux单声道或.NET核心

服务侦听特定的URL端点来执行任务。

例如基于下面的代码示例:

http://localhost:5000/checkStatus?configId=1234

http://localhost:5000/echo

http://localhost:5000/version

事实上,我几乎把它编译期待本节。这一节将产生我所有剩余的构建错误。我知道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如何开始使用。

+0

谢谢,我认为这可能是最好的解决方案。 – Chris

如果你想在.NET核心运行ServiceStack你应该引用1.0.* ServiceStack包作为ServiceStack's .NET Core Release Notes表示:

"dependencies" : { 
    "ServiceStack.Core": "1.0.*", 
    "ServiceStack.Common.Core": "1.0.*", 
    "ServiceStack.Client.Core": "1.0.*", 
    "ServiceStack.Interfaces.Core": "1.0.*", 
    "ServiceStack.Text.Core": "1.0.*" 
}, 

或者,如果你不使用https://servicestack.net应删除引用你的project.json中的“ServiceStack”。

+0

谢谢。我只是在一次尝试ServiceStack。我忽略了删除参考。 – Chris