TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Talaviya Bhavdip
492
2.8k
1.5m
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
{
"DbSettings"
: {
"ConnectionString"
: {
"ERPDbContext"
:
"Data Source=.;Initial Catalog=Angular6_Demo;integrated security=True;"
}
},
"AppSettings"
: {
"Secret"
:
"REPLACE THIS WITH YOUR OWN SECRET, IT CAN BE ANY STRING"
},
"Logging"
: {
"IncludeScopes"
:
false
,
"Debug"
: {
"LogLevel"
: {
"Default"
:
"Warning"
}
},
"Console"
: {
"LogLevel"
: {
"Default"
:
"Warning"
}
}
}
}
My Home APIController
using
System.Threading.Tasks;
using
Angular6_Demo_BusinessAccessLayer.Interface;
using
Angular6_Demo_BusinessObjects.Common;
using
Microsoft.AspNetCore.Http;
using
Microsoft.AspNetCore.Mvc;
namespace
Angular6_Demo.Controllers
{
[Produces(
"application/json"
)]
[Route(
"api/[controller]"
)]
public
class
HomeController : Controller
{
private
readonly ICommonManager _commonManager;
public
HomeController(ICommonManager commonManager)
{
_commonManager = commonManager;
}
[HttpGet]
public
async Task<List<GetDemoDataList>> GetAll()
{
return
await _commonManager.GetDemoDataList();
}
}
}
ICommonManager
using
Angular6_Demo_BusinessObjects.Common;
using
System;
using
System.Collections.Generic;
using
System.Text;
using
System.Threading.Tasks;
namespace
Angular6_Demo_BusinessAccessLayer.Interface
{
public
interface ICommonManager
{
Task<List<GetDemoDataList>> GetDemoDataList();
}
}
CommonManager
using
Angular6_Demo_BusinessAccessLayer.Interface;
using
Angular6_Demo_BusinessObjects.Common;
using
Angular6_Demo_DataAccessLayer.Interface;
using
System;
using
System.Collections.Generic;
using
System.Text;
using
System.Threading.Tasks;
namespace
Angular6_Demo_BusinessAccessLayer
{
public
class
CommonManager:ICommonManager
{
private
readonly ICommonRepository _commonRepository;
public
CommonManager(ICommonRepository commonRepository)
{
_commonRepository = commonRepository;
}
public
async Task<List<GetDemoDataList>> GetDemoDataList()
{
return
await _commonRepository.GetDemoDataList();
}
}
}
CommonRepository
using
Dapper;
using
Angular6_Demo_DataAccessLayer.Interface;
using
System;
using
System.Collections.Generic;
using
Microsoft.Extensions.Options;
using
Angular6_Demo_BusinessObjects.Common;
using
Angular6_Demo_BusinessObjects.Settings;
using
System.Linq;
using
System.Threading.Tasks;
using
System.Data.SqlClient;
namespace
Angular6_Demo_DataAccessLayer
{
public
class
CommonRepository : ICommonRepository
{
private
readonly DbSettings _settings;
public
CommonRepository(IOptions<DbSettings> options)
{
_settings = options.Value;
}
public
async Task<List<GetDemoDataList>> GetDemoDataList()
{
try
{
using
(var dbConnection =
new
SqlConnection(_settings.ConnectionString[DbConnections.DbContext.ToString()]))
{
//dbConnection.Open();
return
(await dbConnection.QueryAsync<GetDemoDataList>(
"SELECT [Id],[Name] FROM [dbo].[Demo]"
, null)).ToList();
}
}
catch
(Exception ex)
{
return
null;
}
}
}
}
ICommonRepository
using
Angular6_Demo_BusinessObjects.Common;
using
System;
using
System.Collections.Generic;
using
System.Text;
using
System.Threading.Tasks;
namespace
Angular6_Demo_DataAccessLayer.Interface
{
public
interface ICommonRepository
{
Task<List<GetDemoDataList>> GetDemoDataList();
}
}
Startup.cs
using
Angular6_Demo_BusinessAccessLayer;
using
Angular6_Demo_BusinessAccessLayer.Interface;
using
Angular6_Demo_BusinessObjects.Settings;
using
Microsoft.AspNetCore.Builder;
using
Microsoft.AspNetCore.Hosting;
using
Microsoft.AspNetCore.Mvc;
using
Microsoft.AspNetCore.SpaServices.AngularCli;
using
Microsoft.Extensions.Configuration;
using
Microsoft.Extensions.DependencyInjection;
namespace
Angular6_Demo
{
public
class
Startup
{
public
Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public
IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public
void
ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
// In production, the Angular files will be served from this directory
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath =
"ClientApp/dist"
;
});
// Configure ConnectionStrings using config
services.Configure<DbSettings>(Configuration.GetSection(
"ConnectionString"
));
services.AddSingleton<ICommonManager, CommonManager>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public
void
Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if
(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler(
"/Home/Error"
);
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseSpaStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name:
"default"
,
template
:
"{controller}/{action=Index}/{id?}"
);
});
app.UseSpa(spa =>
{
// To learn more about options for serving an Angular SPA from ASP.NET Core,
// see https://go.microsoft.com/fwlink/?linkid=864501
spa.Options.SourcePath =
"ClientApp"
;
if
(env.IsDevelopment())
{
spa.UseAngularCliServer(npmScript:
"start"
);
}
});
}
}
}
Reply
Answers (
1
)
HOW TO GET RESULT in sql server LIKE THIS .please see image
Object Initialization in ASP.Net webForm