Talaviya Bhavdip

Talaviya Bhavdip

  • 461
  • 2.8k
  • 1.4m

Enabling ADO.net in ASP.NET Core using appsettings.json

May 26 2018 5:49 AM
Enabling ADO.net in ASP.NET Core 2.1 MVC, Read Custom Property from appsettings.json and display the record
 
i have make code perfect but i dont know where i am wrong., i am trying to make angular project with repository pattern.
 
Error Show like this
-------------------------------------- 
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type serviceType, Type implementationType, CallSiteChain callSiteChain, ParameterInfo[] parameters, bool throwIfCallSiteNotFound) 
 
 
 appsettings.json code
  1. {  
  2.   "DbSettings": {  
  3.     "ConnectionString": {  
  4.         
  5.       "ERPDbContext""Data Source=.;Initial Catalog=Angular6_Demo;integrated security=True;"  
  6.     }  
  7.   },  
  8.     "AppSettings": {  
  9.         "Secret""REPLACE THIS WITH YOUR OWN SECRET, IT CAN BE ANY STRING"  
  10.     },  
  11.   "Logging": {  
  12.     "IncludeScopes"false,  
  13.     "Debug": {  
  14.       "LogLevel": {  
  15.         "Default""Warning"  
  16.       }  
  17.     },  
  18.     "Console": {  
  19.       "LogLevel": {  
  20.         "Default""Warning"  
  21.       }  
  22.     }  
  23.   }  
  24. }  
My Home APIController 
  1. using System.Threading.Tasks;  
  2. using Angular6_Demo_BusinessAccessLayer.Interface;  
  3. using Angular6_Demo_BusinessObjects.Common;  
  4. using Microsoft.AspNetCore.Http;  
  5. using Microsoft.AspNetCore.Mvc;  
  6.   
  7. namespace Angular6_Demo.Controllers  
  8. {  
  9.     [Produces("application/json")]  
  10.     [Route("api/[controller]")]  
  11.     public class HomeController : Controller  
  12.     {  
  13.           
  14.         private readonly ICommonManager _commonManager;  
  15.         public HomeController(ICommonManager commonManager)  
  16.         {  
  17.             _commonManager = commonManager;  
  18.         }  
  19.         [HttpGet]  
  20.         public async Task<List<GetDemoDataList>> GetAll()  
  21.         {  
  22.             return await _commonManager.GetDemoDataList();  
  23.         }  
  24.   
  25.     }  
  26. }  
ICommonManager
 
  1. using Angular6_Demo_BusinessObjects.Common;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace Angular6_Demo_BusinessAccessLayer.Interface  
  8. {  
  9.     public interface ICommonManager  
  10.     {  
  11.         Task<List<GetDemoDataList>> GetDemoDataList();  
  12.   
  13.         }  
  14. }  
CommonManager
 
  1. using Angular6_Demo_BusinessAccessLayer.Interface;  
  2. using Angular6_Demo_BusinessObjects.Common;  
  3. using Angular6_Demo_DataAccessLayer.Interface;  
  4. using System;  
  5. using System.Collections.Generic;  
  6. using System.Text;  
  7. using System.Threading.Tasks;  
  8.   
  9. namespace Angular6_Demo_BusinessAccessLayer  
  10. {  
  11.    public class CommonManager:ICommonManager  
  12.     {  
  13.         private readonly ICommonRepository _commonRepository;  
  14.         public CommonManager(ICommonRepository commonRepository)  
  15.         {  
  16.             _commonRepository = commonRepository;  
  17.         }  
  18.         public async Task<List<GetDemoDataList>> GetDemoDataList()  
  19.         {  
  20.             return await _commonRepository.GetDemoDataList();  
  21.         }  
  22.     }  
  23. }  
CommonRepository
 
  1. using Dapper;  
  2. using Angular6_Demo_DataAccessLayer.Interface;  
  3. using System;  
  4. using System.Collections.Generic;  
  5. using Microsoft.Extensions.Options;  
  6. using Angular6_Demo_BusinessObjects.Common;  
  7. using Angular6_Demo_BusinessObjects.Settings;  
  8. using System.Linq;  
  9. using System.Threading.Tasks;  
  10. using System.Data.SqlClient;  
  11.   
  12. namespace Angular6_Demo_DataAccessLayer  
  13. {  
  14.     public class CommonRepository : ICommonRepository  
  15.     {  
  16.         private readonly DbSettings _settings;  
  17.         public CommonRepository(IOptions<DbSettings> options)  
  18.         {  
  19.             _settings = options.Value;  
  20.         }  
  21.         public async Task<List<GetDemoDataList>> GetDemoDataList()  
  22.         {  
  23.             try  
  24.             {  
  25.                 using (var dbConnection = new SqlConnection(_settings.ConnectionString[DbConnections.DbContext.ToString()]))  
  26.               {  
  27.                     //dbConnection.Open();  
  28.                     return (await dbConnection.QueryAsync<GetDemoDataList>("SELECT [Id],[Name] FROM [dbo].[Demo]", null)).ToList();  
  29.                 }  
  30.             }  
  31.             catch (Exception ex)  
  32.             {  
  33.                 return null;  
  34.             }  
  35.         }  
  36.     }  
  37. }  
ICommonRepository
 
  1. using Angular6_Demo_BusinessObjects.Common;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace Angular6_Demo_DataAccessLayer.Interface  
  8. {  
  9.   public interface ICommonRepository  
  10.     {  
  11.         Task<List<GetDemoDataList>> GetDemoDataList();  
  12.     }  
  13. }  
Startup.cs
 
  1. using Angular6_Demo_BusinessAccessLayer;  
  2. using Angular6_Demo_BusinessAccessLayer.Interface;  
  3. using Angular6_Demo_BusinessObjects.Settings;  
  4. using Microsoft.AspNetCore.Builder;  
  5. using Microsoft.AspNetCore.Hosting;  
  6. using Microsoft.AspNetCore.Mvc;  
  7. using Microsoft.AspNetCore.SpaServices.AngularCli;  
  8. using Microsoft.Extensions.Configuration;  
  9. using Microsoft.Extensions.DependencyInjection;  
  10.   
  11. namespace Angular6_Demo  
  12. {  
  13.     public class Startup  
  14.     {  
  15.         public Startup(IConfiguration configuration)  
  16.         {  
  17.             Configuration = configuration;  
  18.         }  
  19.   
  20.         public IConfiguration Configuration { get; }  
  21.   
  22.         // This method gets called by the runtime. Use this method to add services to the container.  
  23.         public void ConfigureServices(IServiceCollection services)  
  24.         {  
  25.             services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);  
  26.   
  27.             // In production, the Angular files will be served from this directory  
  28.             services.AddSpaStaticFiles(configuration =>  
  29.             {  
  30.                 configuration.RootPath = "ClientApp/dist";  
  31.             });  
  32.             // Configure ConnectionStrings using config  
  33.             services.Configure<DbSettings>(Configuration.GetSection("ConnectionString"));  
  34.   
  35.   
  36.             services.AddSingleton<ICommonManager, CommonManager>();  
  37.         }  
  38.   
  39.         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.  
  40.         public void Configure(IApplicationBuilder app, IHostingEnvironment env)  
  41.         {  
  42.             if (env.IsDevelopment())  
  43.             {  
  44.                 app.UseDeveloperExceptionPage();  
  45.             }  
  46.             else  
  47.             {  
  48.                 app.UseExceptionHandler("/Home/Error");  
  49.                 app.UseHsts();  
  50.             }  
  51.   
  52.             app.UseHttpsRedirection();  
  53.             app.UseStaticFiles();  
  54.             app.UseSpaStaticFiles();  
  55.   
  56.             app.UseMvc(routes =>  
  57.             {  
  58.                 routes.MapRoute(  
  59.                                     name: "default",  
  60.                                     template"{controller}/{action=Index}/{id?}");  
  61.             });  
  62.   
  63.             app.UseSpa(spa =>  
  64.             {  
  65.                             // To learn more about options for serving an Angular SPA from ASP.NET Core,  
  66.                             // see https://go.microsoft.com/fwlink/?linkid=864501  
  67.   
  68.                             spa.Options.SourcePath = "ClientApp";  
  69.   
  70.                 if (env.IsDevelopment())  
  71.                 {  
  72.                     spa.UseAngularCliServer(npmScript: "start");  
  73.                 }  
  74.             });  
  75.         }  
  76.     }  
  77. }  
 
 
 
 
 

Answers (1)