您的位置:首页 > 娱乐 > 明星 > 写网站策划书需要注意什么_北京网站制作南昌_最新新闻今天最新新闻_小红书如何引流推广

写网站策划书需要注意什么_北京网站制作南昌_最新新闻今天最新新闻_小红书如何引流推广

2024/12/26 14:53:56 来源:https://blog.csdn.net/czjnoe/article/details/144268226  浏览:    关键词:写网站策划书需要注意什么_北京网站制作南昌_最新新闻今天最新新闻_小红书如何引流推广
写网站策划书需要注意什么_北京网站制作南昌_最新新闻今天最新新闻_小红书如何引流推广

net6.0

通过CancellationTokenSource 客户端取消Grpc,服务端判断 IsCancellationRequested 是否取消。

proto:

syntax = "proto3";// 引用可空类型
import "google/protobuf/wrappers.proto";option csharp_namespace = "Grpc.Common";package greet;// The greeting service definition.
service Greeter {rpc StreamApi (HelloRequest) returns (stream HelloResponse);
}// The request message containing the user's name.
message HelloRequest {string name = 1;
}message HelloResponse {string message = 1;
}

服务端:

nuget包:Grpc.AspNetCore、Grpc.AspNetCore.Server.Reflection

GreeterService.cs

using Grpc.Core;
using Grpc.Common;public class GreeterService : Greeter.GreeterBase{private readonly ILogger<GreeterService> _logger;public GreeterService(ILogger<GreeterService> logger){_logger = logger;}public override async Task StreamApi(HelloRequest request, IServerStreamWriter<HelloResponse> responseStream, ServerCallContext context){try{for (var i = 0; i < 50; i++){if (context.CancellationToken.IsCancellationRequested){await responseStream.WriteAsync(new HelloResponse{Message = i.ToString()});await Task.Delay(1000 * 3);}}}catch (OperationCanceledException ex){throw;}}}

program:

    public class Program{public static void Main(string[] args){var builder = WebApplication.CreateBuilder(args);// Additional configuration is required to successfully run gRPC on macOS.// For instructions on how to configure Kestrel and gRPC clients on macOS, visit https://go.microsoft.com/fwlink/?linkid=2099682// Add services to the container.builder.Services.AddGrpc();builder.Services.AddGrpcReflection();//添加gRPC反射var app = builder.Build();IWebHostEnvironment env = app.Environment;// Configure the HTTP request pipeline.app.MapGrpcService<GreeterService>();if (env.IsDevelopment()){//映射gRPC反射服务app.MapGrpcReflectionService();}app.MapGet("/", () => "Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909");app.Run();}}

客户端:

nuget包:Google.Protobuf、Grpc.Net.Client、Grpc.Net.ClientFactory、Grpc.Tools

GreeterService.cs

public class GreeterService
{private readonly GreeterClient _greeterClient;public GreeterService(GreeterClient greeterClient){_greeterClient = greeterClient;}private static CancellationTokenSource ProbeLoopCancellationTokenSource = new CancellationTokenSource();public async Task StreamApiAsync(HelloRequest request){using var call = _greeterClient.StreamApi(request, cancellationToken: ProbeLoopCancellationTokenSource.Token);await foreach (var response in call.ResponseStream.ReadAllAsync()){Console.WriteLine(response.Message);}}public async Task CancleAsync(){ProbeLoopCancellationTokenSource.Cancel();}
}

program:

    public class Program{public static void Main(string[] args){var builder = WebApplication.CreateBuilder(args);// Add services to the container.builder.Services.AddControllers();// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbucklebuilder.Services.AddEndpointsApiExplorer();builder.Services.AddSwaggerGen();builder.Services.AddTransient<GreeterService>();builder.Services.AddGrpcClient<GreeterClient>(options =>{options.Address = new Uri("http://localhost:5047");options.ChannelOptionsActions.Add(channelOption =>{channelOption.HttpHandler = new SocketsHttpHandler{EnableMultipleHttp2Connections = true};});//配置请求超时自动取消时间 发起请求到服务端后xxx秒后自动取消//options.CallOptionsActions.Add(callOptionsAction =>//{//    callOptionsAction.CallOptions = new CallOptions(deadline: DateTime.UtcNow.AddSeconds(xx));//});});var app = builder.Build();// Configure the HTTP request pipeline.if (app.Environment.IsDevelopment()){app.UseSwagger();app.UseSwaggerUI();}app.UseAuthorization();app.MapControllers();app.Run();}}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com