InternetException

Posts

ASP.NET Core global exception handling

· 14 words · ~1 min read · in .NET, interview

#ASP.NET

To add global exception handling in ASP.NET Core just use configuration method: app.UseExceptionHandler https://learn.microsoft.com/en-us/aspnet/core/fundamentals/error-handling?view=aspnetcore-6.0#exception-handler-lambda

What ASP.NET Middleware can do?

· 42 words · ~1 min read · updated · in .NET, interview

#ASP.NET MVC #ASP.NET

Pretty much everything. Default ASP.NET Core request handling pipeline is consisting of bunch of middlewares: Authentication Authorization CORS HTTP Logging MVC Static Files other Whole list of built-in middlewares can be found here: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-7.0#built-in-middleware There is possibility to write custom middleware too.

Middleware in OWIN

· 141 words · ~1 min read · in .NET, interview

#delegates #async #ASP.NET

There are 2 ways of writing OWIN middleware in ASP.NET Core.

  • Pure OWIN - usefull if you want to integrate with OWIN compatible module but not written in C#
  • ASP.NET Core OWIN middleware

ASP.NET Core Middleware:

This type of middleware can be:

  • delegate of Func<HttpContext, Func<Task>, Task>
  • class with IMiddleware imlementation
app.Use(async (context, next) =>
{
    // Do work that can write to the Response.
    await next.Invoke();
    // Do logging or other work that doesn't write to the Response.
});

Pure OWIN Middleware:

This rather would be used to integrate with OWIN middlewares written in different language, so that ASP.NET Core implementation directly is not possible.

Read the whole thing →

Owin model and Kestrell request handling

· 89 words · ~1 min read · updated · in .NET, ASP.NET, interview

  • OWIN - Open Web Interface for .NET
  • allows web apps to be decoupled from web servers
  • defines standard way for middleware to be used in a pipeline to handle requests and provide responses
  • ASP.NET Core applications can interoperate with OWIN-based applications, servers and middleware

Microsoft.AspNetCore.Owin package provides two adapter implementations:

  • ASP.NET Core to OWIN
  • OWIN to ASP.NET Core

This allows ASP.NET Core to be hosted on top of an OWIN compatible server/host or for other OWIN compatible components to be run on top of ASP.NET Core.

Read the whole thing →

Expression Trees

· 81 words · ~1 min read · in .NET, interview

  • ’tree like’ data structure
  • each node is expression (method call, equality comparison, binary operation etc.) representing a piece of code
  • you can compile expression tree and run code it represent
  • enables dynamic code generation at runtime
  • LINQ queries on DBs are implemented with Expression Trees
  • used in the dynamic language runtime (DLR)
  • anonymous lambda expression can be converted to ExpressionTree via compiler (i.e. x => x*2 can be represented as tree instead of delegate or anonymous lambda function)

Further read:

Read the whole thing →

Linq

· 98 words · ~1 min read · in .NET, interview

#LINQ

consistent model for working with data from various kinds of sources and formats (XML, SQL, ADO.NET, Datasets, .NET collections etc) 3 stages of working with LINQ: obtain data source create the query execute the query data sources implements IEnumerable&lt;&gt; and are called queryable types IQueryable&lt;&gt; interface inherits from IEnumerable&lt;&gt; generally it is better to use LINQ via IQueryable&lt;&gt; which produces deferred query instead of producing result immediately queries can be executed multiple times queries can be forced immediate execution via Count(), Max(), ToList() or ToArray() etc. LINQ to DB libraries (like EntityFramework) usually are implemented via Expression …

Read the whole thing →

Entity Framework: Change Tracking

· 31 words · ~1 min read · in .NET, Entity Framework, interview

tracked entities are saved during SaveChanges() call entities being tracked when they are: returned from DB attached to context added as subentity tracking can be disabled (AsNoTracking() i.e.) Further read: https://learn.microsoft.com/en-us/ef/core/change-tracking/

Entity Framework: eager and lazy loading

· 71 words · ~1 min read · in .NET, Entity Framework, interview

  • lazy loading:

    • delaying load of data until we specifically request for it
    • i.e. accessing customer.Address loads Address of a customer in the background via separate SQL query
    • property needs to be public, virtual
    • context.Configuration.ProxyCreationEnabled should be true
    • context.Configuration.LazyLoadingEnabled should be true
    • to disable lazy loading completely Context.Configuration.LazyLoadingEnabled = false;
  • eager loading

    • opposite of lazy loading
    • loads related entities as part of the query
    • done via .Include() query

Further read:

Read the whole thing →

Pattern matching

· 103 words · ~1 min read · in .NET, interview

#design patterns #SOLID #C#

Pattern matching is used for: testing expression to check if some value have certain characteristics safer compare because &lsquo;==&rsquo; and &lsquo;!=&rsquo; can be overloaded Examples: a is int number sequence is IList&lt;T&gt; list switch statement: command switch { Operation.SystemTest =&gt; RunDiagnostics(), Operation.Start =&gt; StartSystem(), Operation.Stop =&gt; StopSystem(), Operation.Reset =&gt; ResetToReady(), _ =&gt; throw new ArgumentException(&#34;Invalid enum value for command&#34;, nameof(command)), }; switch with ranges: tempInFahrenheit switch { (&gt; 32) and (&lt; 212) =&gt; &#34;liquid&#34;, &lt; 32 =&gt; &#34;solid&#34;, &gt; 212 =&gt; &#34;gas&#34;, 32 =&gt; …

Read the whole thing →

Reflection

· 20 words · ~1 min read · in .NET, interview

#reflection

obtain information about types and members at runtime create instances, invoke or access them dynamic code generation Further read: https://learn.microsoft.com/en-us/dotnet/framework/reflection-and-codedom/reflection

Deployment models

· 35 words · ~1 min read · updated · in .NET, interview

Type Command framework-dependent executable for the current platform. dotnet publish framework-dependent executable for a specific platform. dotnet publish -r &lt;RID&gt; --self-contained false framework-dependent binary. dotnet publish self-contained executable. dotnet publish -r &lt;RID&gt; Further read: https://learn.microsoft.com/en-us/dotnet/core/deploying/#publish-framework-dependent

.NET AOT

· 46 words · ~1 min read · in .NET, interview

Ahead of the time compilation to machine code produces self-contained application which: starts quicker uses less memory does not require .NET runtime installed it is better for workloads with high deployment instances (clouds) makes runtime code generation impossible not all libraries are compatible Further read: https://learn.microsoft.com/en-us/dotnet/core/deploying/native-aot/

.NET Jit

· 47 words · ~1 min read · in .NET, interview

Just - in - time compiler. part of CLR for all .net programs for all .net languages converts MSIL to native machine code compiles code when it is necessary at runtime also it is possible to pre-compile to native code at compile time instead Further read: https://www.geeksforgeeks.org/what-is-just-in-time-jit-compiler-in-dot-net/

Records

· 64 words · ~1 min read · in .NET, interview

can be class or struct creates methods overrides for enforcing value equality instead of reference equality: Object.Equals Object.GetHashCode operators == != implements IEquatable&lt;&gt; parameter implements ToString supports deconstruction supports with expression meant to be immutable Positional records is a more concise syntax for record: primary constructor parameters match the positional parameters of the record declaration public init-only properties for each parameter Further read: https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/tutorials/records#characteristics-of-records

Is it possible to block GC?

· 26 words · ~1 min read · updated · in .NET, interview

We can. for some critical part of application code using GC.TryStartNoGCRegion method. using CER (Constrained Execution Regions) (obsolete) with GC LatencyModes Further read: https://learn.microsoft.com/en-us/dotnet/api/system.gc.trystartnogcregion?redirectedfrom=MSDN&view=net-7.0#overloads https://learn.microsoft.com/en-us/dotnet/framework/performance/constrained-execution-regions?redirectedfrom=MSDN https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/latency