diff --git a/src/OpenIddict.EntityFramework/OpenIddictEntityFrameworkBuilder.cs b/src/OpenIddict.EntityFramework/OpenIddictEntityFrameworkBuilder.cs
index c3a99690..363231c3 100644
--- a/src/OpenIddict.EntityFramework/OpenIddictEntityFrameworkBuilder.cs
+++ b/src/OpenIddict.EntityFramework/OpenIddictEntityFrameworkBuilder.cs
@@ -94,10 +94,10 @@ namespace Microsoft.Extensions.DependencyInjection
///
/// The .
public OpenIddictEntityFrameworkBuilder ReplaceDefaultEntities()
- where TApplication : OpenIddictApplication, new()
- where TAuthorization : OpenIddictAuthorization, new()
- where TScope : OpenIddictScope, new()
- where TToken : OpenIddictToken, new()
+ where TApplication : OpenIddictApplication
+ where TAuthorization : OpenIddictAuthorization
+ where TScope : OpenIddictScope
+ where TToken : OpenIddictToken
where TKey : IEquatable
{
Services.Configure(options =>
diff --git a/src/OpenIddict.EntityFramework/OpenIddictEntityFrameworkExtensions.cs b/src/OpenIddict.EntityFramework/OpenIddictEntityFrameworkExtensions.cs
index f1f63302..c1157418 100644
--- a/src/OpenIddict.EntityFramework/OpenIddictEntityFrameworkExtensions.cs
+++ b/src/OpenIddict.EntityFramework/OpenIddictEntityFrameworkExtensions.cs
@@ -98,10 +98,10 @@ namespace Microsoft.Extensions.DependencyInjection
/// The builder used to configure the Entity Framework context.
/// The Entity Framework context builder.
public static DbModelBuilder UseOpenIddict([NotNull] this DbModelBuilder builder)
- where TApplication : OpenIddictApplication, new()
- where TAuthorization : OpenIddictAuthorization, new()
- where TScope : OpenIddictScope, new()
- where TToken : OpenIddictToken, new()
+ where TApplication : OpenIddictApplication
+ where TAuthorization : OpenIddictAuthorization
+ where TScope : OpenIddictScope
+ where TToken : OpenIddictToken
where TKey : IEquatable
{
if (builder == null)
diff --git a/src/OpenIddict.EntityFramework/Stores/OpenIddictApplicationStore.cs b/src/OpenIddict.EntityFramework/Stores/OpenIddictApplicationStore.cs
index 4b59dc15..a2cdacc5 100644
--- a/src/OpenIddict.EntityFramework/Stores/OpenIddictApplicationStore.cs
+++ b/src/OpenIddict.EntityFramework/Stores/OpenIddictApplicationStore.cs
@@ -52,9 +52,9 @@ namespace OpenIddict.EntityFramework
/// The type of the Entity Framework database context.
/// The type of the entity primary keys.
public class OpenIddictApplicationStore : IOpenIddictApplicationStore
- where TApplication : OpenIddictApplication, new()
- where TAuthorization : OpenIddictAuthorization, new()
- where TToken : OpenIddictToken, new()
+ where TApplication : OpenIddictApplication
+ where TAuthorization : OpenIddictAuthorization
+ where TToken : OpenIddictToken
where TContext : DbContext
where TKey : IEquatable
{
@@ -651,7 +651,22 @@ namespace OpenIddict.EntityFramework
/// whose result returns the instantiated application, that can be persisted in the database.
///
public virtual ValueTask InstantiateAsync(CancellationToken cancellationToken)
- => new ValueTask(new TApplication());
+ {
+ try
+ {
+ return new ValueTask(Activator.CreateInstance());
+ }
+
+ catch (MemberAccessException exception)
+ {
+ return new ValueTask(Task.FromException(
+ new InvalidOperationException(new StringBuilder()
+ .AppendLine("An error occurred while trying to create a new application instance.")
+ .Append("Make sure that the application entity is not abstract and has a public parameterless constructor ")
+ .Append("or create a custom application store that overrides 'InstantiateAsync()' to use a custom factory.")
+ .ToString(), exception)));
+ }
+ }
///
/// Executes the specified query and returns all the corresponding elements.
diff --git a/src/OpenIddict.EntityFramework/Stores/OpenIddictAuthorizationStore.cs b/src/OpenIddict.EntityFramework/Stores/OpenIddictAuthorizationStore.cs
index 0c9f0814..11723500 100644
--- a/src/OpenIddict.EntityFramework/Stores/OpenIddictAuthorizationStore.cs
+++ b/src/OpenIddict.EntityFramework/Stores/OpenIddictAuthorizationStore.cs
@@ -52,9 +52,9 @@ namespace OpenIddict.EntityFramework
/// The type of the Entity Framework database context.
/// The type of the entity primary keys.
public class OpenIddictAuthorizationStore : IOpenIddictAuthorizationStore
- where TAuthorization : OpenIddictAuthorization, new()
- where TApplication : OpenIddictApplication, new()
- where TToken : OpenIddictToken, new()
+ where TAuthorization : OpenIddictAuthorization
+ where TApplication : OpenIddictApplication
+ where TToken : OpenIddictToken
where TContext : DbContext
where TKey : IEquatable
{
@@ -574,7 +574,22 @@ namespace OpenIddict.EntityFramework
/// whose result returns the instantiated authorization, that can be persisted in the database.
///
public virtual ValueTask InstantiateAsync(CancellationToken cancellationToken)
- => new ValueTask(new TAuthorization());
+ {
+ try
+ {
+ return new ValueTask(Activator.CreateInstance());
+ }
+
+ catch (MemberAccessException exception)
+ {
+ return new ValueTask(Task.FromException(
+ new InvalidOperationException(new StringBuilder()
+ .AppendLine("An error occurred while trying to create a new authorization instance.")
+ .Append("Make sure that the authorization entity is not abstract and has a public parameterless constructor ")
+ .Append("or create a custom authorization store that overrides 'InstantiateAsync()' to use a custom factory.")
+ .ToString(), exception)));
+ }
+ }
///
/// Executes the specified query and returns all the corresponding elements.
diff --git a/src/OpenIddict.EntityFramework/Stores/OpenIddictScopeStore.cs b/src/OpenIddict.EntityFramework/Stores/OpenIddictScopeStore.cs
index c3e702c5..34bed152 100644
--- a/src/OpenIddict.EntityFramework/Stores/OpenIddictScopeStore.cs
+++ b/src/OpenIddict.EntityFramework/Stores/OpenIddictScopeStore.cs
@@ -46,7 +46,7 @@ namespace OpenIddict.EntityFramework
/// The type of the Entity Framework database context.
/// The type of the entity primary keys.
public class OpenIddictScopeStore : IOpenIddictScopeStore
- where TScope : OpenIddictScope, new()
+ where TScope : OpenIddictScope
where TContext : DbContext
where TKey : IEquatable
{
@@ -439,7 +439,22 @@ namespace OpenIddict.EntityFramework
/// whose result returns the instantiated scope, that can be persisted in the database.
///
public virtual ValueTask InstantiateAsync(CancellationToken cancellationToken)
- => new ValueTask(new TScope());
+ {
+ try
+ {
+ return new ValueTask(Activator.CreateInstance());
+ }
+
+ catch (MemberAccessException exception)
+ {
+ return new ValueTask(Task.FromException(
+ new InvalidOperationException(new StringBuilder()
+ .AppendLine("An error occurred while trying to create a new scope instance.")
+ .Append("Make sure that the scope entity is not abstract and has a public parameterless constructor ")
+ .Append("or create a custom scope store that overrides 'InstantiateAsync()' to use a custom factory.")
+ .ToString(), exception)));
+ }
+ }
///
/// Executes the specified query and returns all the corresponding elements.
diff --git a/src/OpenIddict.EntityFramework/Stores/OpenIddictTokenStore.cs b/src/OpenIddict.EntityFramework/Stores/OpenIddictTokenStore.cs
index e3855376..11ae518c 100644
--- a/src/OpenIddict.EntityFramework/Stores/OpenIddictTokenStore.cs
+++ b/src/OpenIddict.EntityFramework/Stores/OpenIddictTokenStore.cs
@@ -52,9 +52,9 @@ namespace OpenIddict.EntityFramework
/// The type of the Entity Framework database context.
/// The type of the entity primary keys.
public class OpenIddictTokenStore : IOpenIddictTokenStore
- where TToken : OpenIddictToken, new()
- where TApplication : OpenIddictApplication, new()
- where TAuthorization : OpenIddictAuthorization, new()
+ where TToken : OpenIddictToken
+ where TApplication : OpenIddictApplication
+ where TAuthorization : OpenIddictAuthorization
where TContext : DbContext
where TKey : IEquatable
{
@@ -582,7 +582,22 @@ namespace OpenIddict.EntityFramework
/// whose result returns the instantiated token, that can be persisted in the database.
///
public virtual ValueTask InstantiateAsync(CancellationToken cancellationToken)
- => new ValueTask(new TToken());
+ {
+ try
+ {
+ return new ValueTask(Activator.CreateInstance());
+ }
+
+ catch (MemberAccessException exception)
+ {
+ return new ValueTask(Task.FromException(
+ new InvalidOperationException(new StringBuilder()
+ .AppendLine("An error occurred while trying to create a new token instance.")
+ .Append("Make sure that the token entity is not abstract and has a public parameterless constructor ")
+ .Append("or create a custom token store that overrides 'InstantiateAsync()' to use a custom factory.")
+ .ToString(), exception)));
+ }
+ }
///
/// Executes the specified query and returns all the corresponding elements.
diff --git a/src/OpenIddict.EntityFrameworkCore/OpenIddictEntityFrameworkCoreBuilder.cs b/src/OpenIddict.EntityFrameworkCore/OpenIddictEntityFrameworkCoreBuilder.cs
index 5bd9ff63..051e0ef8 100644
--- a/src/OpenIddict.EntityFrameworkCore/OpenIddictEntityFrameworkCoreBuilder.cs
+++ b/src/OpenIddict.EntityFrameworkCore/OpenIddictEntityFrameworkCoreBuilder.cs
@@ -103,10 +103,10 @@ namespace Microsoft.Extensions.DependencyInjection
///
/// The .
public OpenIddictEntityFrameworkCoreBuilder ReplaceDefaultEntities()
- where TApplication : OpenIddictApplication, new()
- where TAuthorization : OpenIddictAuthorization, new()
- where TScope : OpenIddictScope, new()
- where TToken : OpenIddictToken, new()
+ where TApplication : OpenIddictApplication
+ where TAuthorization : OpenIddictAuthorization
+ where TScope : OpenIddictScope
+ where TToken : OpenIddictToken
where TKey : IEquatable
{
Services.Configure(options =>
diff --git a/src/OpenIddict.EntityFrameworkCore/OpenIddictEntityFrameworkCoreCustomizer.cs b/src/OpenIddict.EntityFrameworkCore/OpenIddictEntityFrameworkCoreCustomizer.cs
index bcbceab6..e254dac5 100644
--- a/src/OpenIddict.EntityFrameworkCore/OpenIddictEntityFrameworkCoreCustomizer.cs
+++ b/src/OpenIddict.EntityFrameworkCore/OpenIddictEntityFrameworkCoreCustomizer.cs
@@ -18,10 +18,10 @@ namespace OpenIddict.EntityFrameworkCore
/// required by the OpenIddict stack in an Entity Framework context.
///
public class OpenIddictEntityFrameworkCoreCustomizer : RelationalModelCustomizer
- where TApplication : OpenIddictApplication, new()
- where TAuthorization : OpenIddictAuthorization, new()
- where TScope : OpenIddictScope, new()
- where TToken : OpenIddictToken, new()
+ where TApplication : OpenIddictApplication
+ where TAuthorization : OpenIddictAuthorization
+ where TScope : OpenIddictScope
+ where TToken : OpenIddictToken
where TKey : IEquatable
{
public OpenIddictEntityFrameworkCoreCustomizer([NotNull] ModelCustomizerDependencies dependencies)
diff --git a/src/OpenIddict.EntityFrameworkCore/OpenIddictEntityFrameworkCoreExtensions.cs b/src/OpenIddict.EntityFrameworkCore/OpenIddictEntityFrameworkCoreExtensions.cs
index 7810b622..ecd86e09 100644
--- a/src/OpenIddict.EntityFrameworkCore/OpenIddictEntityFrameworkCoreExtensions.cs
+++ b/src/OpenIddict.EntityFrameworkCore/OpenIddictEntityFrameworkCoreExtensions.cs
@@ -107,10 +107,10 @@ namespace Microsoft.Extensions.DependencyInjection
/// The builder used to configure the Entity Framework context.
/// The Entity Framework context builder.
public static DbContextOptionsBuilder UseOpenIddict([NotNull] this DbContextOptionsBuilder builder)
- where TApplication : OpenIddictApplication, new()
- where TAuthorization : OpenIddictAuthorization, new()
- where TScope : OpenIddictScope, new()
- where TToken : OpenIddictToken, new()
+ where TApplication : OpenIddictApplication
+ where TAuthorization : OpenIddictAuthorization
+ where TScope : OpenIddictScope
+ where TToken : OpenIddictToken
where TKey : IEquatable
{
if (builder == null)
@@ -153,10 +153,10 @@ namespace Microsoft.Extensions.DependencyInjection
/// The builder used to configure the Entity Framework context.
/// The Entity Framework context builder.
public static ModelBuilder UseOpenIddict([NotNull] this ModelBuilder builder)
- where TApplication : OpenIddictApplication, new()
- where TAuthorization : OpenIddictAuthorization, new()
- where TScope : OpenIddictScope, new()
- where TToken : OpenIddictToken, new()
+ where TApplication : OpenIddictApplication
+ where TAuthorization : OpenIddictAuthorization
+ where TScope : OpenIddictScope
+ where TToken : OpenIddictToken
where TKey : IEquatable
{
if (builder == null)
diff --git a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictApplicationStore.cs b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictApplicationStore.cs
index a956a8d1..b6f40614 100644
--- a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictApplicationStore.cs
+++ b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictApplicationStore.cs
@@ -74,9 +74,9 @@ namespace OpenIddict.EntityFrameworkCore
/// The type of the Entity Framework database context.
/// The type of the entity primary keys.
public class OpenIddictApplicationStore : IOpenIddictApplicationStore
- where TApplication : OpenIddictApplication, new()
- where TAuthorization : OpenIddictAuthorization, new()
- where TToken : OpenIddictToken, new()
+ where TApplication : OpenIddictApplication
+ where TAuthorization : OpenIddictAuthorization
+ where TToken : OpenIddictToken
where TContext : DbContext
where TKey : IEquatable
{
@@ -718,7 +718,22 @@ namespace OpenIddict.EntityFrameworkCore
/// whose result returns the instantiated application, that can be persisted in the database.
///
public virtual ValueTask InstantiateAsync(CancellationToken cancellationToken)
- => new ValueTask(new TApplication());
+ {
+ try
+ {
+ return new ValueTask(Activator.CreateInstance());
+ }
+
+ catch (MemberAccessException exception)
+ {
+ return new ValueTask(Task.FromException(
+ new InvalidOperationException(new StringBuilder()
+ .AppendLine("An error occurred while trying to create a new application instance.")
+ .Append("Make sure that the application entity is not abstract and has a public parameterless constructor ")
+ .Append("or create a custom application store that overrides 'InstantiateAsync()' to use a custom factory.")
+ .ToString(), exception)));
+ }
+ }
///
/// Executes the specified query and returns all the corresponding elements.
diff --git a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictAuthorizationStore.cs b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictAuthorizationStore.cs
index a10c67b5..2d9d826a 100644
--- a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictAuthorizationStore.cs
+++ b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictAuthorizationStore.cs
@@ -74,9 +74,9 @@ namespace OpenIddict.EntityFrameworkCore
/// The type of the Entity Framework database context.
/// The type of the entity primary keys.
public class OpenIddictAuthorizationStore : IOpenIddictAuthorizationStore
- where TAuthorization : OpenIddictAuthorization, new()
- where TApplication : OpenIddictApplication, new()
- where TToken : OpenIddictToken, new()
+ where TAuthorization : OpenIddictAuthorization
+ where TApplication : OpenIddictApplication
+ where TToken : OpenIddictToken
where TContext : DbContext
where TKey : IEquatable
{
@@ -662,7 +662,22 @@ namespace OpenIddict.EntityFrameworkCore
/// whose result returns the instantiated authorization, that can be persisted in the database.
///
public virtual ValueTask InstantiateAsync(CancellationToken cancellationToken)
- => new ValueTask(new TAuthorization());
+ {
+ try
+ {
+ return new ValueTask(Activator.CreateInstance());
+ }
+
+ catch (MemberAccessException exception)
+ {
+ return new ValueTask(Task.FromException(
+ new InvalidOperationException(new StringBuilder()
+ .AppendLine("An error occurred while trying to create a new authorization instance.")
+ .Append("Make sure that the authorization entity is not abstract and has a public parameterless constructor ")
+ .Append("or create a custom authorization store that overrides 'InstantiateAsync()' to use a custom factory.")
+ .ToString(), exception)));
+ }
+ }
///
/// Executes the specified query and returns all the corresponding elements.
diff --git a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictScopeStore.cs b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictScopeStore.cs
index e18045d5..a8364dd7 100644
--- a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictScopeStore.cs
+++ b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictScopeStore.cs
@@ -64,7 +64,7 @@ namespace OpenIddict.EntityFrameworkCore
/// The type of the Entity Framework database context.
/// The type of the entity primary keys.
public class OpenIddictScopeStore : IOpenIddictScopeStore
- where TScope : OpenIddictScope, new()
+ where TScope : OpenIddictScope
where TContext : DbContext
where TKey : IEquatable
{
@@ -480,7 +480,22 @@ namespace OpenIddict.EntityFrameworkCore
/// whose result returns the instantiated scope, that can be persisted in the database.
///
public virtual ValueTask InstantiateAsync(CancellationToken cancellationToken)
- => new ValueTask(new TScope());
+ {
+ try
+ {
+ return new ValueTask(Activator.CreateInstance());
+ }
+
+ catch (MemberAccessException exception)
+ {
+ return new ValueTask(Task.FromException(
+ new InvalidOperationException(new StringBuilder()
+ .AppendLine("An error occurred while trying to create a new scope instance.")
+ .Append("Make sure that the scope entity is not abstract and has a public parameterless constructor ")
+ .Append("or create a custom scope store that overrides 'InstantiateAsync()' to use a custom factory.")
+ .ToString(), exception)));
+ }
+ }
///
/// Executes the specified query and returns all the corresponding elements.
diff --git a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictTokenStore.cs b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictTokenStore.cs
index e031e744..59c5590b 100644
--- a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictTokenStore.cs
+++ b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictTokenStore.cs
@@ -74,9 +74,9 @@ namespace OpenIddict.EntityFrameworkCore
/// The type of the Entity Framework database context.
/// The type of the entity primary keys.
public class OpenIddictTokenStore : IOpenIddictTokenStore
- where TToken : OpenIddictToken, new()
- where TApplication : OpenIddictApplication, new()
- where TAuthorization : OpenIddictAuthorization, new()
+ where TToken : OpenIddictToken
+ where TApplication : OpenIddictApplication
+ where TAuthorization : OpenIddictAuthorization
where TContext : DbContext
where TKey : IEquatable
{
@@ -660,7 +660,22 @@ namespace OpenIddict.EntityFrameworkCore
/// whose result returns the instantiated token, that can be persisted in the database.
///
public virtual ValueTask InstantiateAsync(CancellationToken cancellationToken)
- => new ValueTask(new TToken());
+ {
+ try
+ {
+ return new ValueTask(Activator.CreateInstance());
+ }
+
+ catch (MemberAccessException exception)
+ {
+ return new ValueTask(Task.FromException(
+ new InvalidOperationException(new StringBuilder()
+ .AppendLine("An error occurred while trying to create a new token instance.")
+ .Append("Make sure that the token entity is not abstract and has a public parameterless constructor ")
+ .Append("or create a custom token store that overrides 'InstantiateAsync()' to use a custom factory.")
+ .ToString(), exception)));
+ }
+ }
///
/// Executes the specified query and returns all the corresponding elements.
diff --git a/src/OpenIddict.MongoDb/OpenIddictMongoDbBuilder.cs b/src/OpenIddict.MongoDb/OpenIddictMongoDbBuilder.cs
index 7a6a41a6..a5834add 100644
--- a/src/OpenIddict.MongoDb/OpenIddictMongoDbBuilder.cs
+++ b/src/OpenIddict.MongoDb/OpenIddictMongoDbBuilder.cs
@@ -78,7 +78,7 @@ namespace Microsoft.Extensions.DependencyInjection
///
/// The .
public OpenIddictMongoDbBuilder ReplaceDefaultApplicationEntity()
- where TApplication : OpenIddictApplication, new()
+ where TApplication : OpenIddictApplication
{
Services.Configure(options => options.DefaultApplicationType = typeof(TApplication));
@@ -90,7 +90,7 @@ namespace Microsoft.Extensions.DependencyInjection
///
/// The .
public OpenIddictMongoDbBuilder ReplaceDefaultAuthorizationEntity()
- where TAuthorization : OpenIddictAuthorization, new()
+ where TAuthorization : OpenIddictAuthorization
{
Services.Configure(options => options.DefaultAuthorizationType = typeof(TAuthorization));
@@ -102,7 +102,7 @@ namespace Microsoft.Extensions.DependencyInjection
///
/// The .
public OpenIddictMongoDbBuilder ReplaceDefaultScopeEntity()
- where TScope : OpenIddictScope, new()
+ where TScope : OpenIddictScope
{
Services.Configure(options => options.DefaultScopeType = typeof(TScope));
@@ -114,7 +114,7 @@ namespace Microsoft.Extensions.DependencyInjection
///
/// The .
public OpenIddictMongoDbBuilder ReplaceDefaultTokenEntity()
- where TToken : OpenIddictToken, new()
+ where TToken : OpenIddictToken
{
Services.Configure(options => options.DefaultTokenType = typeof(TToken));
diff --git a/src/OpenIddict.MongoDb/Stores/OpenIddictApplicationStore.cs b/src/OpenIddict.MongoDb/Stores/OpenIddictApplicationStore.cs
index cbde5016..76cea3b6 100644
--- a/src/OpenIddict.MongoDb/Stores/OpenIddictApplicationStore.cs
+++ b/src/OpenIddict.MongoDb/Stores/OpenIddictApplicationStore.cs
@@ -28,7 +28,7 @@ namespace OpenIddict.MongoDb
///
/// The type of the Application entity.
public class OpenIddictApplicationStore : IOpenIddictApplicationStore
- where TApplication : OpenIddictApplication, new()
+ where TApplication : OpenIddictApplication
{
public OpenIddictApplicationStore(
[NotNull] IMemoryCache cache,
@@ -490,7 +490,22 @@ namespace OpenIddict.MongoDb
/// whose result returns the instantiated application, that can be persisted in the database.
///
public virtual ValueTask InstantiateAsync(CancellationToken cancellationToken)
- => new ValueTask(new TApplication());
+ {
+ try
+ {
+ return new ValueTask(Activator.CreateInstance());
+ }
+
+ catch (MemberAccessException exception)
+ {
+ return new ValueTask(Task.FromException(
+ new InvalidOperationException(new StringBuilder()
+ .AppendLine("An error occurred while trying to create a new application instance.")
+ .Append("Make sure that the application entity is not abstract and has a public parameterless constructor ")
+ .Append("or create a custom application store that overrides 'InstantiateAsync()' to use a custom factory.")
+ .ToString(), exception)));
+ }
+ }
///
/// Executes the specified query and returns all the corresponding elements.
diff --git a/src/OpenIddict.MongoDb/Stores/OpenIddictAuthorizationStore.cs b/src/OpenIddict.MongoDb/Stores/OpenIddictAuthorizationStore.cs
index 537a97ad..cdb0002c 100644
--- a/src/OpenIddict.MongoDb/Stores/OpenIddictAuthorizationStore.cs
+++ b/src/OpenIddict.MongoDb/Stores/OpenIddictAuthorizationStore.cs
@@ -28,7 +28,7 @@ namespace OpenIddict.MongoDb
///
/// The type of the Authorization entity.
public class OpenIddictAuthorizationStore : IOpenIddictAuthorizationStore
- where TAuthorization : OpenIddictAuthorization, new()
+ where TAuthorization : OpenIddictAuthorization
{
public OpenIddictAuthorizationStore(
[NotNull] IMemoryCache cache,
@@ -489,7 +489,22 @@ namespace OpenIddict.MongoDb
/// whose result returns the instantiated authorization, that can be persisted in the database.
///
public virtual ValueTask InstantiateAsync(CancellationToken cancellationToken)
- => new ValueTask(new TAuthorization());
+ {
+ try
+ {
+ return new ValueTask(Activator.CreateInstance());
+ }
+
+ catch (MemberAccessException exception)
+ {
+ return new ValueTask(Task.FromException(
+ new InvalidOperationException(new StringBuilder()
+ .AppendLine("An error occurred while trying to create a new authorization instance.")
+ .Append("Make sure that the authorization entity is not abstract and has a public parameterless constructor ")
+ .Append("or create a custom authorization store that overrides 'InstantiateAsync()' to use a custom factory.")
+ .ToString(), exception)));
+ }
+ }
///
/// Executes the specified query and returns all the corresponding elements.
diff --git a/src/OpenIddict.MongoDb/Stores/OpenIddictScopeStore.cs b/src/OpenIddict.MongoDb/Stores/OpenIddictScopeStore.cs
index 40646907..6f67aa56 100644
--- a/src/OpenIddict.MongoDb/Stores/OpenIddictScopeStore.cs
+++ b/src/OpenIddict.MongoDb/Stores/OpenIddictScopeStore.cs
@@ -28,7 +28,7 @@ namespace OpenIddict.MongoDb
///
/// The type of the Scope entity.
public class OpenIddictScopeStore : IOpenIddictScopeStore
- where TScope : OpenIddictScope, new()
+ where TScope : OpenIddictScope
{
public OpenIddictScopeStore(
[NotNull] IMemoryCache cache,
@@ -394,7 +394,22 @@ namespace OpenIddict.MongoDb
/// whose result returns the instantiated scope, that can be persisted in the database.
///
public virtual ValueTask InstantiateAsync(CancellationToken cancellationToken)
- => new ValueTask(new TScope());
+ {
+ try
+ {
+ return new ValueTask(Activator.CreateInstance());
+ }
+
+ catch (MemberAccessException exception)
+ {
+ return new ValueTask(Task.FromException(
+ new InvalidOperationException(new StringBuilder()
+ .AppendLine("An error occurred while trying to create a new scope instance.")
+ .Append("Make sure that the scope entity is not abstract and has a public parameterless constructor ")
+ .Append("or create a custom scope store that overrides 'InstantiateAsync()' to use a custom factory.")
+ .ToString(), exception)));
+ }
+ }
///
/// Executes the specified query and returns all the corresponding elements.
diff --git a/src/OpenIddict.MongoDb/Stores/OpenIddictTokenStore.cs b/src/OpenIddict.MongoDb/Stores/OpenIddictTokenStore.cs
index 0fbf9f4a..f41f0542 100644
--- a/src/OpenIddict.MongoDb/Stores/OpenIddictTokenStore.cs
+++ b/src/OpenIddict.MongoDb/Stores/OpenIddictTokenStore.cs
@@ -28,7 +28,7 @@ namespace OpenIddict.MongoDb
///
/// The type of the Token entity.
public class OpenIddictTokenStore : IOpenIddictTokenStore
- where TToken : OpenIddictToken, new()
+ where TToken : OpenIddictToken
{
public OpenIddictTokenStore(
[NotNull] IMemoryCache cache,
@@ -507,7 +507,22 @@ namespace OpenIddict.MongoDb
/// whose result returns the instantiated token, that can be persisted in the database.
///
public virtual ValueTask InstantiateAsync(CancellationToken cancellationToken)
- => new ValueTask(new TToken());
+ {
+ try
+ {
+ return new ValueTask(Activator.CreateInstance());
+ }
+
+ catch (MemberAccessException exception)
+ {
+ return new ValueTask(Task.FromException(
+ new InvalidOperationException(new StringBuilder()
+ .AppendLine("An error occurred while trying to create a new token instance.")
+ .Append("Make sure that the token entity is not abstract and has a public parameterless constructor ")
+ .Append("or create a custom token store that overrides 'InstantiateAsync()' to use a custom factory.")
+ .ToString(), exception)));
+ }
+ }
///
/// Executes the specified query and returns all the corresponding elements.