英文:
How to unit test if an interceptor is registered within the DI?
问题
如何测试我的应用程序是否已在DI中注册了这个拦截器?
英文:
I have my startup like this:
builder.Services.AddGrpc(options =>
{
options.Interceptors.Add<ExceptionGrpcInterceptor>(); // << how to test if this is there
});
Now I want to test if my application has this interceptor registered within the DI.
How to do that?
答案1
得分: 1
从 DI 容器获取选项对象
使用 ConsoleApp1;
使用 Grpc.AspNetCore.Server;
使用 Microsoft.Extensions.DependencyInjection;
使用 Microsoft.Extensions.Options;
IServiceCollection 服务集合 = new ServiceCollection();
services.AddGrpc(options =>
{
options.Interceptors.Add
});
IServiceProvider 服务提供程序 = services.BuildServiceProvider();
var options = 服务提供程序.GetRequiredService<IOptions
bool hasInterceptor = options.Value.Interceptors.Any(x => x.Type == typeof(ExceptionGrpcInterceptor));
Console.ReadKey();
英文:
You can get options object from DI container
using ConsoleApp1;
using Grpc.AspNetCore.Server;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
IServiceCollection services = new ServiceCollection();
services.AddGrpc(options =>
{
options.Interceptors.Add<ExceptionGrpcInterceptor>();
});
IServiceProvider serviceProvider = services.BuildServiceProvider();
var options = serviceProvider.GetRequiredService<IOptions<GrpcServiceOptions>>();
bool hasInterceptor = options.Value.Interceptors.Any(x => x.Type == typeof(ExceptionGrpcInterceptor));
Console.ReadKey();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论