Unable to create an object of type ‘ApplicationDbContext’. For the different patterns supported at design time

I found the cause of this error could be multiple things in your code. For me at least, the best way was to add verbose in command. With that will be able to understand what is the problem. the verbose will display all steps of the execution. In visual studio use: add-migration Added_something -verbose For … Read more

System.Text.Json: How do I specify a custom name for an enum value?

This is not currently supported out of the box in .net-core-3.0, .net-5, .net-6.0 or .net-7.0. There is currently an issue Support for EnumMemberAttribute in JsonConverterEnum #31081[1] requesting this functionality. In the interim, you will need to create your own JsonConverterFactory that serializes enums with custom value names specified by attributes. If you need to round-trip … Read more

Get Current User in a Blazor component

There are three possibilities for getting the user in a component (a page is a component): Inject IHttpContextAccessor and from it access HttpContext and then User; need to register IHttpContextAccessor in Startup.ConfigureServices, normally using AddHttpContextAccessor. Edit: according to the Microsoft docs you must not do this for security reasons. Inject an AuthenticationStateProvider property, call GetAuthenticationStateAsync … Read more

HTTP Error 500.31 – ANCM Failed to Find Native Dependencies in IIS

I have received the same error after upgrading my ASP.NET Core project from .NET Core 3.0 to 3.1 and installing Microsoft .NET Core 3.1.0 – Windows Server Hosting. Quick (but bad) fix changed the web.config handler from AspNetCoreModuleV2 to AspNetCoreModule and it worked ok. Good fix Find the underlying cause by inspecting Event Viewer. There … Read more

Ignore property when null using the new Net Core 3.0 Json

I’m looking at .Net Core 3.1, where this should ignore null values services.AddControllers().AddJsonOptions(options => { options.JsonSerializerOptions.IgnoreNullValues = true; }); While in .NET 5 and later, set JsonSerializerOptions.DefaultIgnoreCondition to JsonIgnoreCondition.WhenWritingNull: services.AddControllers().AddJsonOptions(options => { options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull; }); Note, the above isn’t per property/attribute, although there is an attribute which may be helpful JsonIgnoreAttribute. An alternative, to … Read more

JWT Authentication and Swagger with .NET Core 3.0

After some research, I eventually found the answer here Before seeing this page, I knew that I should use AddSecurityRequirement after AddSecurityDefinition because of many samples, but it was a problem that the function parameters have changed on .NET Core 3.0. By the way, the final answer is as below: services.AddSwaggerGen(c => { c.SwaggerDoc(“v1”, new … Read more

How to setup app settings in a .Net Core 3 Worker Service

If for example the worker class needed access to some data stored in your appsettings public class Worker : BackgroundService { private readonly ILogger<Worker> logger; private readonly WorkerOptions options; public Worker(ILogger<Worker> logger, WorkerOptions options) { this.logger = logger; this.options = options; } protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { //do something that … Read more

ASP.NET MVC Core API Serialize Enums to String

New System.Text.Json serialization ASP.NET MVC Core 3.0 uses built-in JSON serialization. Use System.Text.Json.Serialization.JsonStringEnumConverter (with “Json” prefix): services .AddMvc() // Or .AddControllers(…) .AddJsonOptions(opts => { var enumConverter = new JsonStringEnumConverter(); opts.JsonSerializerOptions.Converters.Add(enumConverter); }) More info here. The documentation can be found here. If you prefer Newtonsoft.Json You can also use “traditional” Newtonsoft.Json serialization: Install-Package Microsoft.AspNetCore.Mvc.NewtonsoftJson And then: … Read more

Can WPF applications be run in Linux or Mac with .Net Core 3?

No, they have clearly stated that these are windows only. In one of the .NET Core 3.0 discussions, they have also clarified that they do not intend to make these features cross-platform in the future since the whole concept is derived from windows specific features. They talked about thinking of a whole new idea for … Read more

ASP.NET Core 3.0 not showing on Visual Studio 2019

There is an option in Tools -> Options that enables preview versions of the .NET Core SDK. In the VS Preview shipping channel, it is on (by default, and not settable). In the VS Release channel, it defaults to off and you can opt-in. (Note: it’s disabled in the screenshot because I have a Preview … Read more