C# port to Linux Mono or .NET Core

I have a service written in C# for Windows. I need to get it to run on Linux. I've looked at potentially rewriting the code in either Java or C++, and I likely will do that down the road, but as an intermediate solution I'm looking at porting the project to Mono or .NET Core. The key is that it's a fairly small application, so making the necessary code changes shouldn't be too much work.

The service listens to specific URL endpoints to performs tasks.

Examples based on code sample below:

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

http://localhost:5000/echo

http://localhost:5000/version

In fact, I nearly have it compiling expect for this section. This one section is generating all of my remaining build errors. I understand that WCF services are not yet available in .NET Core. So I've been exploring alternatives to this but I'm having a hard time with what will work on Linux in .NET Core. I also want to avoid implementing something that would be tantamount to rewriting the entire thing or shoehorning in a full NGINX or Apache server.

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();
    }
}

I'm not entirely familiar with WCF either as I've never worked with it and this project was originally written by someone else.

Here's my project.json. I'm aware I might have some unnecessary or incorrect items in there. I've been testing a lot:

{
  "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%"
  }
}

Is there another way I could do this?


Your usage of the WebGetAttribute indicates that you intend to create a RESTful service. While WCF is not available in .NET Core, WebApi is supported. With WebApi you can create REST Services, which might be all you need. You can check this tutorial by Microsoft on how to get started with this.


If you're trying to run ServiceStack on .NET Core you should be referencing the 1.0.* ServiceStack packages as indicated in 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.*"
},

Or if you're not using https://servicestack.net you should remove the reference to "ServiceStack" in your project.json.

链接地址: http://www.djcxy.com/p/15950.html

上一篇: 有用的Eclipse Java代码模板

下一篇: C#端口到Linux Mono或.NET Core