What needs to be done for a class to be used inside a using block

To enable a class to be used under a using() block, the class must implement the IDisposable interface with its own implementation of the Dispose() method.

public class MyClass : IDisposable {
    public void Dispose() {
      // implementation of Dispose method
    }
}

// in some other method
using(var myclass = new MyClass()) {
    // myclass instance is alive inside 
    // this block only
}

Dispose is never called by the .NET Framework; you must call it manually - preferably by wrapping its creation in a using() block.

How do you handle errors Globally in ASP.NET Core?

We can make use of the built-in UseExceptionHandler() middleware for catching Global Errors in ASP.NET Core.

app.UseExceptionHandler(err =>
   {
        err.Run(async ctx =>
        {
            context.Response.StatusCode = 500;
            context.Response.ContentType = "application/json";
            // gets error detail
            var ex =  context.Features.Get<IExceptionHandlerPathFeature>();
            var error = new MyResponseModel() { Content = ex.Error.Message };
            await context.Response.WriteAsync(JsonConvert.SerializeObject(error));
        });
    });

How do you throw custom errors in SQL

We can use raiseError to throw custom exceptions in an P-SQL block.

Example:

RAISERROR (N'Error Raised: %s %d.', -- Message text.  
           10, -- Severity,  
           1, -- State,  
           N'number', -- First argument from the 
           5); -- Second argument.  

Returns Error Message: Error Raised: number 5.

How do you design a strongly-typed class for a configuration?

To create a strongly-typed class for binding to a configuration section:

  • The property names and their types match the key names and their value types "exactly" in the configuration.
  • The classes used must be non-abstract with parameterless constructors
  • Only the public accessors (properties) are bound to configuration data but not fields

How can you bind a configuration section to an object?

A Configuration section can be bound to a strictly-typed class object in two ways:

  • use Configuration.Bind() by passing the configuration section to bind and the object to which the values are mapped.
  • use services.Configure() to bind a section to a type and access the type by means of IOptions, IOptionsSnapshot or IOptionMonitor interface.