locked
when implement middleware to validate token result show correct but give me error null connectionstring ? RRS feed

  • Question

  • User696604810 posted

    problem

    when implement middleware to validate token result show correct but give me error null connectionstring ?

    i get errror below in case of using app.UseTokenAuth(); :

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
            {
                 
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
                else
                {
                    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                    app.UseHsts();
                }
              
                
               
                app.UseTokenAuth(); 
    
                app.UseHttpsRedirection();
              
                app.UseMvc();
                app.UseStaticFiles();
                app.UseDefaultFiles();
                
            }

    but without this line no error connection

    full errror show

    ArgumentNullException: Value cannot be null. Parameter name: connectionString
    Microsoft.EntityFrameworkCore.Utilities.Check.NotEmpty(string value, string parameterName)
    Microsoft.EntityFrameworkCore.SqlServerDbContextOptionsExtensions.UseSqlServer(DbContextOptionsBuilder optionsBuilder, string connectionString, Action<SqlServerDbContextOptionsBuilder> sqlServerOptionsAction)
    Security.Installers.DBInstaller+<>c__DisplayClass0_0.<InstallService>b__0(DbContextOptionsBuilder options) in DBInstaller.cs
    +
                options.UseSqlServer(
    Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions+<>c__DisplayClass1_0<TContextService, TContextImplementation>.<AddDbContext>b__0(IServiceProvider p, DbContextOptionsBuilder b)
    Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.DbContextOptionsFactory<TContext>(IServiceProvider applicationServiceProvider, Action<IServiceProvider, DbContextOptionsBuilder> optionsAction)
    Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions+<>c__DisplayClass10_0<TContextImplementation>.<AddCoreServices>b__0(IServiceProvider p)
    Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, ServiceProviderEngineScope scope)
    Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor<TArgument, TResult>.VisitCallSite(IServiceCallSite callSite, TArgument argument)
    Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProviderEngineScope scope)
    Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor<TArgument, TResult>.VisitCallSite(IServiceCallSite callSite, TArgument argument)
    Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProviderEngineScope scope)
    Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor<TArgument, TResult>.VisitCallSite(IServiceCallSite callSite, TArgument argument)
    Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProviderEngineScope scope)
    Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor<TArgument, TResult>.VisitCallSite(IServiceCallSite callSite, TArgument argument)
    Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine+<>c__DisplayClass1_0.<RealizeService>b__0(ServiceProviderEngineScope scope)
    Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
    Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
    Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.GetService(IServiceProvider sp, Type type, Type middleware)
    lambda_method(Closure , object , HttpContext , IServiceProvider )
    Microsoft.AspNetCore.Builder.UseMiddlewareExtensions+<>c__DisplayClass4_1.<UseMiddleware>b__2(HttpContext context)
    Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
    public void ConfigureServices(IServiceCollection services)
            {
              
               
               
                services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    
                services.InstallServicesInAssembly(Configuration);
                UserManager.StartSystem(Providers.SQLServer);
             
            }
    public class DBInstaller : IInstaller
        {
            public void InstallService(IServiceCollection services, IConfiguration confiuration)
            {
    
                services.AddDbContext<Data.DataContext>(options =>
    
                options.UseSqlServer(
                    confiuration.GetConnectionString("DefaultConnection")));
                
                services.AddSingleton<ItockenValidate, tockenValidate>();
                
                
            }
    public interface IInstaller
        {
            void InstallService(Microsoft.Extensions.DependencyInjection.IServiceCollection services, Microsoft.Extensions.Configuration.IConfiguration confiuration);
        }

    so that what is the problem please

    Thursday, September 12, 2019 3:49 PM

All replies

  • User-474980206 posted

    the error says its you sqlserver contection is null:

                services.AddDbContext<Data.DataContext>(options =>
    
                options.UseSqlServer(
                    confiuration.GetConnectionString("DefaultConnection")));
                
       
    Thursday, September 12, 2019 4:16 PM
  • User711641945 posted

    Hi ahmedbarbary,

    As bruce said, please check your DefaultConnection.And could you share a demo that could reproduce your issue.Actually you posted the missing code(e.g. how did you register InstallServicesInAssembly...) ,so I could not reproduce.

    Did you make error if you use the default services like below:

    services.AddDbContext<DataContext>(options =>
                        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

    Best Regards,

    Rena

    Friday, September 13, 2019 6:04 AM