首页 .Net .NET Core 使用WCF的替代方案(IpcServiceFramework)

.NET Core 使用WCF的替代方案(IpcServiceFramework)

1、IpcServiceFramework简介

.NET Core轻量级进程间通信框架,允许通过命名管道和/或TCP调用服务(与WCF类似,目前.NET Core不可用)。还支持通过SSL进行安全通信。

支持在服务契约中使用原始或复杂类型。

支持服务器端的多线程,具有可配置的线程数(仅限命名管道端点)。

ASP.NET核心依赖注入框架友好。

2、IpcServiceFramework使用示例代码

1)创建 service contract

public interface IComputingService
{
float AddFloat(float x, float y);
}

2)实现service

>ComputingService : IComputingService
{
public float AddFloat(float x, float y)
{
return x + y;
}
}

3)在控制台应用程序中托管service

>Program
{
    static void Main(string[] args)
    {
        // conp DI
        IServiceCollection services = ConpServices(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 ConpServices(IServiceCollection services)
    {
        return services
            .AddIpc()
            .AddNamedPipe(options =>
            {
                options.ThreadCount = 2;
            })
            .AddService<IComputingService, ComputingService>();
    }
}

4)从客户端进程调用service

IpcServiceClient<IComputingService> client = new IpcServiceClientBuilder<IComputingService>()
    .UseNamedPipe("pipeName") // or .UseTcp(IPAddress.Loopback, 45684) to invoke using TCP
    .Build();
float result = await client.InvokeAsync(x => x.AddFloat(1.23f, 4.56f));

IpcServiceFrameworkhttps://github.com/jacqueskang/IpcServiceFramework

特别声明:本站部分内容收集于互联网是出于更直观传递信息的目的。该内容版权归原作者所有,并不代表本站赞同其观点和对其真实性负责。如该内容涉及任何第三方合法权利,请及时与824310991@qq.com联系,我们会及时反馈并处理完毕。