Cola.ColaGrpc 1.0.2

dotnet add package Cola.ColaGrpc --version 1.0.2                
NuGet\Install-Package Cola.ColaGrpc -Version 1.0.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Cola.ColaGrpc" Version="1.0.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Cola.ColaGrpc --version 1.0.2                
#r "nuget: Cola.ColaGrpc, 1.0.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Cola.ColaGrpc as a Cake Addin
#addin nuget:?package=Cola.ColaGrpc&version=1.0.2

// Install Cola.ColaGrpc as a Cake Tool
#tool nuget:?package=Cola.ColaGrpc&version=1.0.2                
Cola.ColaGrpc 框架
1. 注入

注入可以通过代码直接注入

builder.Services
    .AddGrpcClient<Greeter.GreeterClient>(o =>
    {
        o.Address = new Uri("https://localhost:5005");
        o.Interceptors.Add(new ClientGrpcInterceptor(builder.Services.BuildServiceProvider().GetService<IColaLogs>()!,
            config));
    })
    .AddCallCredentials((context, metadata) =>
    {
        if (!string.IsNullOrEmpty(_token))
        {
            metadata.Add("Authorization", $"odinsam Bearer {_token}");
        }
        return Task.CompletedTask;
    })
    .ConfigureChannel(options =>
    {
        options.CreateGrpcClientChannelOptions(config);
    });
2. 链接
var client = builder.Services.BuildServiceProvider().GetService<Greeter.GreeterClient>();
3. 调用
// IPC进程内调用grpc
// net core 3.x 显式的指定HTTP/2不需要TLS支持
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);

var credentials = CallCredentials.FromInterceptor((context, metadata) =>
{
    if (!string.IsNullOrEmpty(_token))
    {
        metadata.Add("Authorization", $"Bearer {_token}");
    }
    return Task.CompletedTask;
});
var channel = new ColaGrpcHelper(new ColaWindowsGrpc()).CreateChannel("https://localhost:5005",config,null,credentials);
var invoker =
    channel.Intercept(new ClientGrpcInterceptor(builder.Services.BuildServiceProvider().GetService<IColaLogs>()!,
        config));
var client = new Greeter.GreeterClient(invoker);

// 一元调用
ConsoleHelper.WriteInfo("一元调用 start");
var headers = new Metadata();
headers.Add("customHeader","odin-custom-header");
var helloReply = client.SayHelloAsync(new HelloRequest
{
    Name = "odin-sam"
},headers);
var resultOnce = await helloReply;
Console.WriteLine(resultOnce.Message);
ConsoleHelper.WriteInfo("一元调用 over");
Console.WriteLine();Console.WriteLine();

// 客户端流调用
ConsoleHelper.WriteInfo("客户端流调用 start");
var clientStreamMethodAsync = client.ClientStreamMethodAsync();
var requestStream = clientStreamMethodAsync.RequestStream;
for (int k = 1; k < 11; k++)
{
    await requestStream.WriteAsync(new ClientStreamMethodParam { Par = k });
    Console.WriteLine($"send par {k}");
    await Task.Delay(500);
}
await requestStream.CompleteAsync();
var resultClientStream = await clientStreamMethodAsync;
Console.WriteLine($"server response result: {resultClientStream.Result}");
ConsoleHelper.WriteInfo("客户端流调用 over");
Console.WriteLine();Console.WriteLine();

//服务端流调用
ConsoleHelper.WriteInfo("服务端流调用 start");
var param = new ClientMethodParam();
param.Lst.Add(new RepeatedField<int>() { 1, 2, 3, 4, 5 });
var clientServerStreamMethodAsync = client.ServerStreamMethodAsync(param);
var responseStream = clientServerStreamMethodAsync.ResponseStream;
// Define the cancellation token.
CancellationTokenSource source = new CancellationTokenSource();
CancellationToken token = source.Token;
while (await responseStream.MoveNext(token))
{
    Console.WriteLine($"server response result { responseStream.Current.Result }");
}
ConsoleHelper.WriteInfo("服务端流调用 over");
Console.WriteLine();Console.WriteLine();

//双向流调用
ConsoleHelper.WriteInfo("双向流调用 start");
var clientServerStream = client.ClientServerStreamMethodAsync();
var clientServerRequestStream = clientServerStream.RequestStream;
for (int k = 0; k < 10; k++)
{
    await clientServerRequestStream.WriteAsync(new ClientStreamMethodParam { Par = k });
    Console.WriteLine($"send par {k}");
    await Task.Delay(500);
}
await clientServerRequestStream.CompleteAsync();
var clientServerResponse = clientServerStream.ResponseStream;
while (await clientServerResponse.MoveNext(token))
{
    Console.WriteLine($"{ clientServerResponse.Current.Result }");
}
ConsoleHelper.WriteInfo("双向流调用 over");
Console.WriteLine();Console.WriteLine();

// 一元复杂调用
ConsoleHelper.WriteInfo("一元复杂调用 start");
var par = new ServiceMethodParamts
{
    Id = 100,
    Name = "ali yun",
    IsExists = true,
};
par.Roles.Add(new []{"刘备","关羽","张飞"});
par.Attributes.Add(new Dictionary<string, string>()
{
    {"董卓","吕布"},
    {"王允","貂蝉"}
});
par.Detail = Any.Pack(new Person{ Start = Timestamp.FromDateTimeOffset(DateTime.Now)});
var testResult = await client.ServiceMethodAsync(par);
if (testResult.ResultCase == ServiceMethodResponse.ResultOneofCase.Person)
{
    Console.WriteLine(JsonConvert.SerializeObject(testResult.Person).ToJsonFormatString());
}
ConsoleHelper.WriteInfo("一元复杂调用 over");
Console.WriteLine();Console.WriteLine();
2. 使用

详见 ConsoleApp1Test

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.2 202 1/4/2024