我习惯于使用类库(.Net Framework)创建.Net Framework控制台应用程序,并通过WCF服务从头开始公开Add(int x, int y)函数.然后,我使用控制台应用程序在服务器内代理调用此函数.

但是,如果我使用控制台应用程序(.Net Core)和类库(.Net Core),系统就会崩溃.ServiceModel不可用.我在谷歌上搜索了一下,但我还没有弄清楚在这个例子中是什么"取代"了WCF.

如何将类库中的Add(int x, int y)函数公开给控制台应用程序.净核心?我明白了.服务模型.由于这是一个跨平台的服务,我需要创建一个RESTful服务吗?

推荐答案

.NET Core不支持WCF,因为它是Windows特定的技术,而.NET Core应该是跨平台的.

如果您正在执行进程间通信,请考虑try IpcServiceFramework项目.

它允许创建WCF风格的服务,如下所示:

  1. Create service contract

    public interface IComputingService
    {
        float AddFloat(float x, float y);
    }
    
  2. Implement the service

    class ComputingService : IComputingService
    {
        public float AddFloat(float x, float y)
        {
            return x + y;
        }
    }
    
  3. Host the service in Console application

    class Program
    {
        static void Main(string[] args)
        {
            // configure DI
            IServiceCollection services = ConfigureServices(new ServiceCollection());
    
            // build and run service host
            new IpcServiceHostBuilder(services.BuildServiceProvider())
                .AddNamedPipeEndpoint<IComputingService>(name: "endpoint1", pipeName: "pipeName")
                .AddTcpEndpoint<IComputingService>(name: "endpoint2", ipEndpoint: IPAddress.Loopback, port: 45684)
                .Build()
                .Run();
        }
    
        private static IServiceCollection ConfigureServices(IServiceCollection services)
        {
            return services
                .AddIpc()
                .AddNamedPipe(options =>
                {
                    options.ThreadCount = 2;
                })
                .AddService<IComputingService, ComputingService>();
        }
    }
    
  4. Invoke the service from client process

.net相关问答推荐

AutoMapper在实体框架查询后不知从哪里带来数据

使用React路由加载器获取数据不能正常工作

Web API 中基于令牌的身份验证,无需任何用户界面

在 C# 中获取 log4net 日志(log)文件

如何获得友好的操作系统版本名称?

单击关闭按钮时隐藏表单而不是关闭

Owin Twitter登录-根据验证程序远程证书无效

C# 的部分类是糟糕的设计吗?

哪个单元测试框架?

为什么 ?: 会导致转换错误,而 if-else 不会?

覆盖方法上的 C# 可选参数

我们应该总是在类中包含一个默认构造函数吗?

WCF服务客户端:内容类型text/html;响应消息的charset=utf-8 与绑定的内容类型不匹配

.NET 事件 - 什么是对象发送者和 EventArgs e?

如何在 Dapper.Net 中编写一对多查询?

在 C# 中使用 Bitmap 对象查找图像格式

使用没有catch块的try-finally块

Windows 服务在哪个目录中运行?

从 Visual Studio 2015 发布 - 允许不受信任的证书

MVVM 没有意义吗?