简体   繁体   English

Lamar AddScoped 未按预期工作网络核心 webapi

[英]Lamar AddScoped not working as expected net core webapi

I have netcore webapi project and i'm using Lamar for DI.我有 netcore webapi 项目,我正在使用 Lamar 进行 DI。 I have a service MyContextService which i register as scoped我有一个服务 MyContextService 我注册为范围

    public class LifetimeRegistry : ServiceRegistry
    {
        public LifetimeRegistry()
        {
            this.AddScoped<MyContextService>();
        }
    }

I use this service in 2 places, firstly in a authorization handler (where i also set the context).我在两个地方使用此服务,首先是在授权处理程序中(我还设置了上下文)。

    public class TokenRequirement : IAuthorizationRequirement { }

    public class TokenRequirementHandler : AuthorizationHandler<TokenRequirement>, IAuthorizationRequirement
    {
        private readonly MyContextService _cxtService;

        public TokenRequirementHandler(MyContextService cxtService)
        {
            _cxtService = _cxtService;
        }

        protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, TokenRequirement requirement)
        {
            // other code..
            _cxtService.InitContext(); // This sets some context that's valid for this http request
        }
    }

And the second place is in a controller第二名是controller

    [Authorize(Policy = "TokenRequired")]
    public class MyController
    {
        private readonly MyContextService _cxtService;

        public MyController(MyContextService cxtService)
        {
            _cxtService = _cxtService;
            // The context is null & service has a different hashcode
            var cxt = _cxtService.GetContext(); 
        }
    }

In this controller, i expect the context to be set but it isn't and i noticed that the 2 instances have different hashcodes.在这个 controller 中,我希望设置上下文,但事实并非如此,我注意到这两个实例具有不同的哈希码。 What am i doing wrong here?我在这里做错了什么?

Answering my own question in hopes that this will help someone else.回答我自己的问题,希望这对其他人有帮助。 The problem is that TokenRequirementHandler is a singleton and MyContextService is scoped.问题是TokenRequirementHandler是 singleton 并且MyContextService是作用域。 This makes a lot of sense when you think about it but it wasn't so obvious at the time.当您考虑它时,这很有意义,但当时并不那么明显。

My solution was to make TokenRequirementHandler scoped.我的解决方案是使TokenRequirementHandler范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 StructureMap -&gt; Lamar .NET Core 3.1 服务创建不起作用 - StructureMap -> Lamar .NET Core 3.1 service creation not working 如何在 ASP.NET Core 3 预览版中使用 Lamar 2? - How to use Lamar 2 with ASP.NET Core 3 preview? 从AddTransient到AddScoped的ASP.NET Core依赖项注入 - ASP.NET Core dependency injection from AddTransient to AddScoped 拍卖计时器在 Asp.net webAPI 中未按预期工作 - Auction Timer is not working as expected in Asp.net webAPI 模型绑定不适用于 ASP.NET Core 2 WebAPI 中的 POST 请求 - Model binding is not working on POST request in ASP.NET Core 2 WebAPI TakeLast 在 .net 核心 3.1 中无法按预期工作 - TakeLast not working as expected in .net core 3.1 .Net Core 3.1 API 路由未按预期工作 - .Net Core 3.1 API Routing not working as expected 无法让 Lamar (IOC) 解决 .NET Core 3.1 中的 API 控制器依赖项 - Can't get Lamar (IOC) to resolve API Controller Dependencies in .NET Core 3.1 在.NET Core内置DI容器中为AddScoped服务实现自定义处理 - Implement custom dispose for AddScoped services in .NET Core built-in DI Container AddTransient 或 AddScoped 用于单独的 controller 在 .net 核心后台应用程序中的每个监听器 - AddTransient or AddScoped for separate controller isntance per lisntener in .net core back ground application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM