Improve Error Message: Resolving unregistered service from internal service provider #5500

Closed
rowanmiller opened this Issue May 24, 2016 · 0 comments

Projects

None yet

1 participant

@rowanmiller
Member

Currently we assume that resolving an unregistered service means you haven't configured a provider:

No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext.

But it is particularly confusing if you try to resolve a context from the internal service provider (not a super weird thing to assume might work):

public static void MyExtensionMethod<TEntity>(this DbSet<TEntity> set)
{
    var context = set.GetService<DbContext>();
}

We should update the message so say something like "Could not resolve '', this may be because...".

@rowanmiller rowanmiller added this to the 1.0.0 milestone May 26, 2016
@rowanmiller rowanmiller self-assigned this May 26, 2016
@rowanmiller rowanmiller added a commit that referenced this issue May 31, 2016
@rowanmiller rowanmiller 📝 Don't assume failed GetService means no provider
If you call an extension method such as
`DbContext.Database.GetDbConnection()` and we fail to resolve the
connection from DI, then we assumed this was because you did not
register a provider. This is often the case, but it's a misleading error
message if you are just trying to resolve some service that is not
present. Adding some pre-amble to this exception to explain this.

Note we still throw the existing message if we are trying to locate the
configured provider and there is none
(see `DatabaseProviderSelector.SelectServices()`) since we are sure that
is the problem in that case.

Fixes #5500
951fc59
@rowanmiller rowanmiller added a commit that referenced this issue Jun 1, 2016
@rowanmiller rowanmiller 📝 Don't assume failed GetService means no provider
If you call an extension method such as
`DbContext.Database.GetDbConnection()` and we fail to resolve the
connection from DI, then we assumed this was because you did not
register a provider. This is often the case, but it's a misleading error
message if you are just trying to resolve some service that is not
present. Adding some pre-amble to this exception to explain this.

Note we still throw the existing message if we are trying to locate the
configured provider and there is none
(see `DatabaseProviderSelector.SelectServices()`) since we are sure that
is the problem in that case.

Fixes #5500
afac453
@rowanmiller rowanmiller added a commit that closed this issue Jun 1, 2016
@rowanmiller rowanmiller 📝 Don't assume failed GetService means no provider
If you call an extension method such as
`DbContext.Database.GetDbConnection()` and we fail to resolve the
connection from DI, then we assumed this was because you did not
register a provider. This is often the case, but it's a misleading error
message if you are just trying to resolve some service that is not
present. Adding some pre-amble to this exception to explain this.

Note we still throw the existing message if we are trying to locate the
configured provider and there is none
(see `DatabaseProviderSelector.SelectServices()`) since we are sure that
is the problem in that case.

Fixes #5500
fca20d1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment