Why don't async methods in identity services in ASP.NET Core have a parameter for CancellationToken?

Hossein Babamoradi

My question is why async methods like userManager.FindByNameAsync(username) in identity Services in Asp.Net Core do not have a parameter for CancellationToken? I know it has a property with CancellationToken.None value and pass to Entityframework methods but it never sets a value and it`s read only and I cannot set a value for that What are the solutions for this problem? This is Identity UserManage service

When I want to override CancellationToken in My custom service which inherited from userManager

iherited

SilentTremor

Short answer is by design because value cannot change. Related to the needs, custom implementation of UserManager.cs or at least specific methods needed may be a solution, if not HttpContext.RequestAborted is the only cancellation token stopping UserManager async invocations.

Long answer, value of an immutable property won't change, the only way in can change the value is in the constructor of base/abstract class. Overwritten value of a virtual property is in the driven class same rule apply, meaning all the time there are two values, one in base one in driven. Even if property is mutable, referring from driven at base.whatever when explicitly assigned something will change base value.

Example:

using Xunit;

namespace Tests
{
    public class MyBase
    {
        public MyBase()
        {
        }

        public MyBase(int immutableInt)
        {
            ImmutableInt = immutableInt;
        }

        protected virtual int ImmutableInt { get; } = -1;
        protected virtual int MutableInt { get; set; } = -1;
    }


    public class MyDriven : MyBase
    {
        public MyDriven()
        {
        }

        public MyDriven(int value)
        {
            ImmutableInt = value;
            MutableInt = value;
        }

        public MyDriven(int immutableInt, int mutableInt1): base(immutableInt)
        {
            ImmutableInt = immutableInt;
            MutableInt = mutableInt1;
        }


        protected override int ImmutableInt { get; }
        protected override int MutableInt => base.MutableInt;

        public int ImmutableIntReadonly => ImmutableInt;
        public int BaseImmutableIntReadonly => base.ImmutableInt;

        public int MutableIntReadonly => MutableInt;
        public int BaseMutableIntReadonly => base.MutableInt;

    }

    public class ImmutableTests
    {
        [Fact]
        public void TestImutableOverride()
        {
            MyDriven imutable = new MyDriven(1);
            Assert.Equal(1, imutable.ImmutableIntReadonly);
            Assert.Equal(-1, imutable.BaseImmutableIntReadonly);
            Assert.Equal(1, imutable.MutableIntReadonly);
            Assert.Equal(1, imutable.BaseMutableIntReadonly);


            MyDriven imutableCtor = new MyDriven(1, 1);
            Assert.Equal(1, imutableCtor.ImmutableIntReadonly);
            Assert.Equal(1, imutableCtor.BaseImmutableIntReadonly);
            Assert.Equal(1, imutableCtor.MutableIntReadonly);
            Assert.Equal(1, imutableCtor.BaseMutableIntReadonly);
        }
    }

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why don't methods of structs have to be declared in C++?

CancellationToken with async Dapper methods?

ASP.NET Identity, add another user to role instantly (they don't have to log out and in again)

default template parameter - don't have to be from right? Why it works?

Why don't the images don't show up on my ASP.Net core website?

F# syntax for async controller methods in ASP.NET Core

Can't add a claim with ASP.NET Core and identity

Allowing parameter name to have "[" in ASP.Net Core WEB API

ASP.NET Core Identity Authorization using Parameter for Team Membership

ASP.NET Core 2.1 Identity couldn't find ApplicationUser

Why Asp.Net Core 2.1 Identity Use Razor Pages?

What are services and why add them in ASP.NET Core?

ASP .NET Core Identity SignInManager

Atlassian.NET SDK Async Methods don't throw Exceptions

Extension methods for Identity Controllers in ASP.Net Core 2.2 App

ASP.NET Core ajax and Async C# methods

CancellationToken not working in asp.net core

Why don't my unauthorized controllers return 401 ASP.Net Core?

ASP.NET Core WebAPI crashes because I don't have StaticFileMiddleware?

How to add Identity Services in ASP.NET Core 5

Why variables have to be final in anonymous methods and class fields don't

IActionFilter - why don't to have to implement all methods?

Why can't I have multiple get methods in asp.net webapi

Is CancellationToken available in ASP.NET Core ActionFilter?

Do the methods in the services also have to be async when the controller is already using async in ASP .NET core?

Why does asp .net core not log exceptions in async methods?

Why editing bootstrap.css files don't have any effect on ASP.NET Core Web App?

Why don't any of these methods work for installing .Net Core SDK & runtime on 22.04 (Jammy Jellyfish)?

Why in extension method I don't have access to some other IServiceCollection extensions in .net Core?