Skip to main content

Posts

Showing posts with the label .Net Core

ActionFilterAttribute: When to use OnActionExecuting vs. OnActionExecutingAsync?

Difference between OnActionExecting and OnActionExecutingAsync Both are ActionFilters can be added as attributes on Controller methods. OnActionExecting and OnActionExecutingAsync are both called before the controller action method is executed. The difference is OnActionExecuting runs SYNCHRONOUSLY and OnActionExecutingAsync should be used when you want to run some ASYNCHRONOUS calls. OnActionExecuting Implementation public class ActionFilter : Attribute, IActionFilter { private ILoggerEntry _loggerEntry; public ActionFilter(ILoggerEntry loggerEntry) { _loggerEntry = loggerEntry ?? throw new ArgumentNullException("ILogger cannot be null"); } public void OnActionExecuted(ActionExecutedContext actionContext) { _loggerEntry.AddToLog("Called after action method executes"); } public void OnActionExecuting(ActionExecutingContext actionContext) { _loggerEntry.A...

Calling 'BuildServiceProvider' from application code results in copy of Singleton warning in .Net Core

You will get this issue when you try to call the BuildServiceProvider() from the Startup.ConfigureServices method. The recommended solution is to use the IApplicationBuilder ApplicationServices to get the registered services in IServiceCollection. Calling BuildServiceProvider anywhere in your code is bad design as this might create additional singleton instances of services. Each Service provider will have its own cache of singleton instances. If multiple service builders are created in code, it will cause the singleton instances to be created more than once, which contradicts the singleton property which is only once the instance is present throughout the lifetime of the application. Please find the alternative solution below, when you want to get the registered services for dependency injection. First, create a static class with a one property IServiceProvider type public void ConfigureServices(IServiceCollection services) { services.AddScoped<ILoggerEntry, LoggerEntry>(); ...

How to dependency inject to static class

.Net core supports dependency injection. There are many ways that you can inject services like constructor injection, action method injection, property injection. But there will be scenarios where you need to inject dependency services to static classes. For example, injecting services to extension methods. First, create a static class with a one property IServiceProvider type public void ConfigureServices(IServiceCollection services) { services.AddScoped<ILoggerEntry, LoggerEntry>(); services.AddTransient<IMongoRepository, MongoRepository>(); } Second, configure your services in ConfigureServices() method in Startup.cs and define the lifetime of the service instance using either Transient, Scoped or Singleton types. public void ConfigureServices(IServiceCollection services) { services.AddScoped<ILoggerEntry, LoggerEntry>(); services.AddTransient<IMongoRepository, MongoRepository>(); } For the next step to configure the Static class provider proper...

Error NU1605 - Detected package downgrade. Reference the package directly from the project to select a different version.

Error NU1605 - Detected package downgrade This error occurs when a dependency package has a version higher than an existing package version in the project solution. Solution: Add the following in .csproj file < PackageReference > < NoWarn >$( NoWarn ); NU1605 </ NoWarn > </ PackageReference > Another way to do this is to right-click on the solution and  click  Properties . Click  Build  and under  Errors and warnings  add 1605 to the  SuppressWarnings  text box. You can also add multiple error codes that you want to suppress by adding each separated by a comma. P.S. The below screenshot is in VS2019 Mac Version

Senior .Net Developer Interview questions

Senior .Net Developer Interview questions I have listed down some of the common .Net interview questions for Senior developer positions which will be helpful for someone who wants to brush up quickly before interviews. The topics that are covered are below, C#, OOPS, LINQ, ENTITY FRAMEWORK, SQL, .NET, DESIGN PATTERNS, ASP.NET, ASP.NET MVC, WCF, WEB API, REST, .NET CORE, JAVASCRIPT, JQUERY, ADO.NET, ANGULAR, ANGULAR JS, MONGO DB Explain SOLID Design principles in C#? Explain the 12-factor app methodology? Could you explain OWASP top 10 security risks and vulnerabilities? What are extension methods and how to define it? What is Dependency Injection(DI) and tools used for DI? Explain CLR in .Net and its functions? What is a .Net Web Service? What are generics and generic collections in C# and why we use it? What data types are applicable or can be passed in generics? What are dynamic types in C#? Please describe the use case? What is a strong-named assembly in .NET and what is t...

Working with MongoDB in .Net Core (Part 5) - Mongo Queries

Mongo Aggregation pipeline Queries //Pipeline Query use Collection; db.getCollection(collectionName).aggregate( [ { " $match " : { " {field1} " : { " $eq " : " string " }, " {field2} " : { " $eq " : " string " } }, }, { " $group " : { " _id " : " $field " , " Count " : { " $sum " : 1.0 }, " Results " : { " $addToSet " : " $field " } } }, { " $sort " : { " Count " : -1.0 } }, { " $project " : { " count " : 1.0, " _id " : 1.0, " AccountId " : 1.0 } } ], ...

SwashBuckle - Swagger Open API Documentation

Swashbuckle Open API Swagger is Open API documentation used to understand the various methods and the request/responses of various endpoints. It generates the Web API SDK for clients. Swashbuckle .AspNetCore is an open-source project for generating Swagger documents for ASP.Net Core Web APIs.  Another open-source project is  NSwag  for generating swagger documents. Enable Swagger in .NET Core Apps -  Note - Install the  Swashbuckle.AspNetCore NuGet package  into your solution.Don't install Swashbuckle.AspNetCore.Swagger package) Add the below to the Configure() Method -  app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "AppName"); }); using Swashbuckle.AspNetCore.Examples ; using Swashbuckle.AspNetCore.Swagger ; public void ConfigureServices (IServiceCollection services) { // Register the Swagger generator, defining 1 or more Swagger documents services.AddSwaggerGen(c => { ...