How to read AppSettings values from a .json file in ASP.NET Core

I have set up my AppSettings data in file appsettings/Config .json like this: { “AppSettings”: { “token”: “1234” } } I have searched online on how to read AppSettings values from .json file, but I could not get anything useful. I tried: var configuration = new Configuration(); var appSettings = configuration.Get(“AppSettings”); // null var token … Read more

Command dotnet ef not found

I’m following the docs in order to create an initial migration. When I execute dotnet, I get the help section, meaning that the PATH works properly. Then I try to execute the command below from the docs in console window: dotnet ef migrations add InitialCreate I get the following error: Could not execute because the … Read more

ASP.NET Core Dependency Injection error: Unable to resolve service for type while attempting to activate

I created an .NET Core MVC application and use Dependency Injection and Repository Pattern to inject a repository to my controller. However, I am getting an error: InvalidOperationException: Unable to resolve service for type ‘WebApplication1.Data.BloggerRepository’ while attempting to activate ‘WebApplication1.Controllers.BlogController’. Model (Blog.cs) namespace WebApplication1.Models { public class Blog { public int BlogId { get; set; … Read more

ASP.NET Core Web API exception handling

I am using ASP.NET Core for my new REST API project after using regular ASP.NET Web API for many years. I don’t see any good way to handle exceptions in ASP.NET Core Web API. I tried to implement an exception handling filter/attribute: public class ErrorHandlingFilter : ExceptionFilterAttribute { public override void OnException(ExceptionContext context) { HandleExceptionAsync(context); … Read more

How to determine if .NET Core is installed

I know that for older versions of .NET, you can determine if a given version is installed by following https://support.microsoft.com/en-us/kb/318785 Is there an official method of determining if .NET Core is installed? (And I don’t mean the SDK, I want to check a server without the SDK, to determine if it has DotNetCore.1.0.0-WindowsHosting.exe installed on … Read more

Resolving instances with ASP.NET Core DI from within ConfigureServices

How do I manually resolve a type using the ASP.NET Core MVC built-in dependency injection framework? Setting up the container is easy enough: public void ConfigureServices(IServiceCollection services) { // … services.AddTransient<ISomeService, SomeConcreteService>(); } But how can I resolve ISomeService without performing injection? For example, I want to do this: ISomeService service = services.Resolve<ISomeService>(); There are … Read more

How do you create a custom AuthorizeAttribute in ASP.NET Core?

I’m trying to make a custom authorization attribute in ASP.NET Core. In previous versions it was possible to override bool AuthorizeCore(HttpContextBase httpContext). But this no longer exists in AuthorizeAttribute. What is the current approach to make a custom AuthorizeAttribute? What I am trying to accomplish: I am receiving a session ID in the Header Authorization. … Read more

AddTransient, AddScoped and AddSingleton Services Differences

I want to implement dependency injection (DI) in ASP.NET Core. So after adding this code to ConfigureServices method, both ways work. What is the difference between the services.AddTransient and service.AddScoped methods in ASP.NET Core? public void ConfigureServices(IServiceCollection services) { // Add framework services. // Add application services. services.AddTransient<IEmailSender, AuthMessageSender>(); services.AddScoped<IEmailSender, AuthMessageSender>(); } 10 TL;DR Transient … Read more