Я пытаюсь добавить основные миграции для моего проекта asp. net core api. К сожалению, миграциям приходится сталкиваться с некоторыми проблемами при добавлении миграции, потому что я внедряю IHttpContextAccessor в одного из моих провайдеров: /
Я звоню services.AddHttpContextAccessor();
и services.AddDbContext<SCContext>(builder => builder.UseSqlServer(connectionstring));
в моем файле startup.cs.
Если я запускаю> dotnet run
и выполняю несколько запросов, все работает нормально. Но если я пытаюсь запустить >dotnet ef migrations add InitialCreate
, отображается следующий журнал (обе команды выполняются по пути каталога проекта api):
Build started...
Build succeeded.
System.NullReferenceException: Object reference not set to an instance of an object.
at SocietyCloud.Services.Provider.CredentialsProvider..ctor(IHttpContextAccessor httpContextAccessor) in E:\Bibliotheken\Projects\societycloud\Application\SocietyCloud.Services\Provider\CredentialsProvider.cs:line 15
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite singletonCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite singletonCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.<>c__DisplayClass1_0.<RealizeService>b__0(ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetServiceOrCreateInstance(IServiceProvider provider, Type type)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass13_2.<FindContextTypes>b__11()
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func`1 factory)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Object reference not set to an instance of an object.
Кто-нибудь сталкивался с такой же проблемой?
Запуск .cs
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddDefaultPolicy(builder =>
builder.SetIsOriginAllowed(_ => true)
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader());
});
services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "TEST", Version = "v1" });
});
AutoMapperConfig.Configure(services);
OptionsConfig.Configure(services, Configuration);
AuthenticationConfig.Configure(services, Configuration);
DependencyConfig.Configure(services, Configuration);
}
DependencyConfig.cs
public static void Configure(IServiceCollection services, IConfiguration configuration)
{
// Register provider
services.AddHttpContextAccessor();
var openibanurl = configuration[$"{nameof(ExternalApisOptions)}:{nameof(ExternalApisOptions.OpenIbanUrl)}"];
services.AddHttpClient<IOpenIbanProvider, OpenIbanProvider>(configure => configure.BaseAddress = new Uri(openibanurl));
// Register DBContext
var connectionstring = configuration[$"{nameof(AzureOptions)}:{nameof(AzureOptions.SqlConnectionstring)}"];
services.AddDbContext<SCContext>(builder => builder.UseSqlServer(connectionstring));
// Register services
foreach (var (Contract, Implementation) in typeof(MembershipsService).Assembly.GetTypesWithImplementations<IService>())
services.AddScoped(Contract, Implementation);
// Register validator
services.AddScoped<IViewModelValidator<VMAddMembership>, VMAddMembershipValidator>();
}
CredentialsProvider.cs
public class CredentialsProvider : ICredentialsProvider
{
public Guid? AccountId { get; private set; }
public Guid? SocietyId { get; private set; }
public CredentialsProvider(IHttpContextAccessor httpContextAccessor)
{
if (httpContextAccessor.HttpContext.User.Identity.IsAuthenticated)
{
AccountId = GetGuid(httpContextAccessor.HttpContext, SCClaims.AccountId);
SocietyId = GetGuid(httpContextAccessor.HttpContext, SCClaims.SocietyId);
}
}
private Guid? GetGuid(HttpContext context, SCClaims claim)
{
if (Guid.TryParse(context.User.Claims?.FirstOrDefault(x => x.Type == Enum.GetName(typeof(SCClaims), claim))?.Value, out var societyid))
return societyid;
return null;
}
}