0

With Microsoft.Practices.Unity, the GetService method of IDependencyResolver in Asp.Net webAPI is called for each request, where i can do the below(user specific resolving of container)

public UnityDependencyResolver(IUnityContainer container)
    {
string userKey = GetUserFromContext();

        if (_container != null)
        {
            if (_container.ContainsKey(userKey))
                _container[userKey] = container;
            else
                _container.Add(userKey, container);
        }
        else
        {
            _container = new Dictionary<string, IUnityContainer>();
            _container.Add(userKey, container);
        }

}

public object GetService(Type serviceType) {

            string userKey = GetUserKey();
            return _container[userKey].Resolve(serviceType);
       
    }

, i want to achieve the same in Core API. as i need this to show container data specific to user. now two user across global can see data of each other

Note: Cannot use default DI of Core API

Kindly help Thanks

tried below but no luck Custom unity Unity

RayK
  • 41
  • 2
  • 10
  • My apologies, but I did not get what is the advantage you are getting having service registrations per user? What if you have huge user base like 100000 then what's going to happen? would that be good design? My suggestion here that you should look for dependency injection based on singleton, scope or transient types lifetime. Check here --https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-5.0 – user1672994 Jan 07 '21 at 07:13
  • there are some scenarios where in i get data from other external service and i need to hold(not save in DB) data for specific user, which i achieve it using [ContainerRegistration(LifetimeManagement = ContainerLifetimeManagement.ContainerControlled)] on viewmodel. and application user base is not more than 5 users – RayK Jan 07 '21 at 07:42
  • tried this https://stackoverflow.com/questions/52821647/using-unity-dependency-injection-in-multi-user-web-application-second-user-to-l no luck – RayK Jan 11 '21 at 08:56

0 Answers0