2016-12-27 33 views
1

我有用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%" 
    } 
} 

有沒有另一種方法可以做到這一點?

回答

1

您使用WebGetAttribute表示您打算創建RESTful服務。雖然WCF在.NET Core中不可用,但WebApi受支持。藉助WebApi,您可以創建REST服務,這可能是您所需要的。你可以檢查this tutorial by Microsoft如何開始使用。

+0

謝謝,我認爲這可能是最好的解決方案。 – Chris

0

如果你想在.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