Skip to content

Service registered with RegisterMany with Reuse.Scoped throwing error while resolving using IServiceProvider.GetService<T>() in .net 8 #688

@programmersgeek-maker

Description

@programmersgeek-maker

I have a class and multiple interface as below:

public interface IWebUser1 { string GerWebUser(); }
public interface IConsumerUser2 { string GetConsumerUser(); }
public interface ISchedulerUser3 { string GetSchedulerUser(); }
public interface IUser : IWebUser1, IConsumerUser2, ISchedulerUser3 { string GetUser(); }
public class User : IUser
{
public string UserName { get; set; }
public string GerWebUser() => $"Executed IWebUser1 , User: {UserName ?? ""}";
public string GetConsumerUser() => $"Executed IConsumerUser2, User: {UserName ?? ""}";
public string GetSchedulerUser() => $"Executed ISchedulerUser3, User: {UserName ?? ""}";
public string GetUser() => $"Executed IUser, User: {UserName ?? ""}";
}

I have registered it using DryIoc provider as below:

// Register services with service key
container.RegisterMany(Reuse.Scoped);

Now when my code execute, it first goes to Middleware where i am resolving IUser using context accessor and able to get its instance and assigning UserName.

Now it goes into my controller where i am executor handler via MediatorR and it goes into my central Executor.

In this Executor class, when i am trying to resolve IUser using IServiceProvider.GetSerivce, i am getting null reference exception.

Above is working in .Net 6 but after upgrading to .Net 8, it is not working as i have got some information about changes related to scope validation strictness when it is registered using RegiserMany with Reuse Scope.

What is the solution around this as i have tried all possible ways like:

  1. Giving servicekey null
  2. Trying to individual register all interface but in that case User service is getting registered as seperate instance for each interface

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions