IdentityServer / IdentityServer3.Admin.EntityFramework

Apache License 2.0
12 stars 30 forks source link

Resolve custom dependencies #27

Closed lexon0011 closed 7 years ago

lexon0011 commented 7 years ago

I use Unity in my project to register and resolve dependency. To tell AutoFac how to resolve a custom dependency a write the following working code for my IdentityServerServiceFactory

IdentityManagerServiceFactory factory;
factory.Register(new Registration<RoleManager<PlatosRole, Guid>>(res => (RoleManager<PlatosRole, Guid>)ServiceLocator.Current.GetInstance<IPlatosRoleManager>()));

I couldn't resolve my custom dependency for the IdentityAdminServiceFactory, because the Register method has only a parameter for an optional string.

IdentityAdminServiceFactory factory;
factory.IdentityAdminService = new Registration<IIdentityAdminService, IdentityAdminManagerService>();

I need a way to tell autofac, how it should resolve my dependencies, because in the IdentityAdminManagerServiceI need my IDatabaseConfiguration, which could not resolved at the moment:

public IdentityAdminManagerService(IDatabaseConfiguration databaseConfiguration)
      : base("IdentityDbContext")
{
   // databaseConfiguration could not resolve here!!
}

What is the correct way to resolve my dependency in the Register method of IdentityAdminServiceFactory ??

Thanks!

madhavabhyankar commented 7 years ago

This may be because you don't have a connection string named IdentityDbContext

Sent from my iPhone

On Oct 5, 2016, at 11:20 AM, lexon0011 notifications@github.com wrote:

IdentityDbContext

lexon0011 commented 7 years ago

No, because the following code works:

public IdentityAdminManagerService()
      : base("IdentityDbContext")
{
}

But my goal is to write the following code:

public IdentityAdminManagerService(IDatabaseConfiguration databaseConfiguration)
      : base(databaseConfiguration.ConnectionString)
{
}

The IDatabaseConfiguration was registered in my Unity container. As the IdentityServer3 use Autofac for Dependency Injection, my registered dependencies could not resolved.

madhavabhyankar commented 7 years ago

You could use the dependency resolver overload like this:

var dbConfiguration = new DatabaseConfiguration(); // either resolve it using DI or create an isntance of IDatabaseConfiguration
IdentityAdminServiceFactory factory;
factory.IdentityAdminService = new Registration<IIdentityAdminService>(resolver=> new IdentityAdminManagerService(dbConfiguration));
lexon0011 commented 7 years ago

Thank you, That was the answer I was looking for đź‘Ť

madhavabhyankar commented 7 years ago

Please close this issue