gpt4 book ai didi

c# - 什么在 .Net Core 中取代了 WCF?

转载 作者:IT王子 更新时间:2023-10-29 03:55:35 25 4
gpt4 key购买 nike

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

但是,如果我使用控制台应用程序 (.Net Core) 和类库 (.Net Core),则 System.ServiceModel 不可用。我已经进行了一些谷歌搜索,但我还没有弄清楚在这种情况下是什么“取代”了 WCF。

如何将类库中的 Add(int x, int y) 函数公开给 .Net Core 中的控制台应用程序?我看到了 System.ServiceModel.Web,因为这是跨平台的,我是否必须创建 RESTful 服务?

最佳答案

您可以使用 gRPC 在 .NET 核心应用程序中托管 Web 服务。

enter image description here

简介

  1. gRPC 是一个高性能的开源 RPC 框架,最初由 Google 开发。
  2. 该框架基于远程过程调用的客户端-服务器模型。客户端应用程序可以像调用本地对象一样直接调用服务器应用程序的方法。

示例

服务器代码

class Program
{
static void Main(string[] args)
{
RunAsync().Wait();
}

private static async Task RunAsync()
{
var server = new Grpc.Core.Server
{
Ports = { { "127.0.0.1", 5000, ServerCredentials.Insecure } },
Services =
{
ServerServiceDefinition.CreateBuilder()
.AddMethod(Descriptors.Method, async (requestStream, responseStream, context) =>
{
await requestStream.ForEachAsync(async additionRequest =>
{
Console.WriteLine($"Recieved addition request, number1 = {additionRequest.X} --- number2 = {additionRequest.Y}");
await responseStream.WriteAsync(new AdditionResponse {Output = additionRequest.X + additionRequest.Y});
});
})
.Build()
}
};

server.Start();

Console.WriteLine($"Server started under [127.0.0.1:5000]. Press Enter to stop it...");
Console.ReadLine();

await server.ShutdownAsync();
}
}

客户端代码

class Program
{
static void Main(string[] args)
{
RunAsync().Wait();
}

private static async Task RunAsync()
{
var channel = new Channel("127.0.0.1", 5000, ChannelCredentials.Insecure);
var invoker = new DefaultCallInvoker(channel);
using (var call = invoker.AsyncDuplexStreamingCall(Descriptors.Method, null, new CallOptions{}))
{
var responseCompleted = call.ResponseStream
.ForEachAsync(async response =>
{
Console.WriteLine($"Output: {response.Output}");
});

await call.RequestStream.WriteAsync(new AdditionRequest { X = 1, Y = 2});
Console.ReadLine();

await call.RequestStream.CompleteAsync();
await responseCompleted;
}

Console.WriteLine("Press enter to stop...");
Console.ReadLine();

await channel.ShutdownAsync();
}
}

客户端和服务器之间的共享类

[Schema]
public class AdditionRequest
{
[Id(0)]
public int X { get; set; }
[Id(1)]
public int Y { get; set; }
}

[Schema]
public class AdditionResponse
{
[Id(0)]
public int Output { get; set; }
}

服务描述符

using Grpc.Core;
public class Descriptors
{
public static Method<AdditionRequest, AdditionResponse> Method =
new Method<AdditionRequest, AdditionResponse>(
type: MethodType.DuplexStreaming,
serviceName: "AdditonService",
name: "AdditionMethod",
requestMarshaller: Marshallers.Create(
serializer: Serializer<AdditionRequest>.ToBytes,
deserializer: Serializer<AdditionRequest>.FromBytes),
responseMarshaller: Marshallers.Create(
serializer: Serializer<AdditionResponse>.ToBytes,
deserializer: Serializer<AdditionResponse>.FromBytes));
}

序列化器/反序列化器

public static class Serializer<T>
{
public static byte[] ToBytes(T obj)
{
var buffer = new OutputBuffer();
var writer = new FastBinaryWriter<OutputBuffer>(buffer);
Serialize.To(writer, obj);
var output = new byte[buffer.Data.Count];
Array.Copy(buffer.Data.Array, 0, output, 0, (int)buffer.Position);
return output;
}

public static T FromBytes(byte[] bytes)
{
var buffer = new InputBuffer(bytes);
var data = Deserialize<T>.From(new FastBinaryReader<InputBuffer>(buffer));
return data;
}
}

输出

Sample client output

Sample Server output

引用资料

  1. https://blogs.msdn.microsoft.com/dotnet/2018/12/04/announcing-net-core-3-preview-1-and-open-sourcing-windows-desktop-frameworks/
  2. https://grpc.io/docs/
  3. https://grpc.io/docs/quickstart/csharp.html
  4. https://github.com/grpc/grpc/tree/master/src/csharp

基准

  1. http://csharptest.net/787/benchmarking-wcf-compared-to-rpclibrary/index.html

关于c# - 什么在 .Net Core 中取代了 WCF?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48522849/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com