Use IHttpClientFactory to implement resilient HTTP requests

Benefits of using IHttpClientFactory

  • Provides a central location for naming and configuring logical HttpClient objects. For example, you may configure a client (Service Agent) that’s pre-configured to access a specific microservice.
  • Codify the concept of outgoing middleware via delegating handlers in HttpClient and implementing Polly-based middleware to take advantage of Polly’s policies for resiliency.
  • HttpClient already has the concept of delegating handlers that could be linked together for outgoing HTTP requests. You can register HTTP clients into the factory and you can use a Polly handler to use Polly policies for Retry, CircuitBreakers, and so on.
  • Manage the lifetime of xref:System.Net.Http.HttpMessageHandler to avoid the mentioned problems/issues that can occur when managing HttpClient lifetimes yourself.

Multiple ways to use IHttpClientFactory

There are several ways that you can use IHttpClientFactory in your application:

  • Basic usage
  • Use Named Clients
  • Use Typed Clients
  • Use Generated Clients

source

C# Auto-Implemented Properties

In C# 3.0 and later, auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors. They also enable client code to create objects. When you declare a property as shown in the following example, the compiler creates a private, anonymous backing field that can only be accessed through the property’s get and set accessors. In C# 9 and later, init accessors can also be declared as auto-implemented properties.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// This class is mutable. Its data can be modified from
// outside the class.
class Customer
{
// Auto-implemented properties for trivial get and set
public double TotalPurchases { get; set; }
public string Name { get; set; }
public int CustomerId { get; set; }

// Constructor
public Customer(double purchases, string name, int id)
{
TotalPurchases = purchases;
Name = name;
CustomerId = id;
}

// Methods
public string GetContactInfo() { return "ContactInfo"; }
public string GetTransactionHistory() { return "History"; }

// .. Additional methods, events, etc.
}

class Program
{
static void Main()
{
// Intialize a new object.
Customer cust1 = new Customer(4987.63, "Northwind", 90108);

// Modify a property.
cust1.TotalPurchases += 499.99;
}
}

source

C# throw vs throw ex

sample
source

Quality rule: CA2200: Rethrow to preserve stack details

Once an exception is thrown, part of the information it carries is the stack trace. The stack trace is a list of the method call hierarchy that starts with the method that throws the exception and ends with the method that catches the exception. If an exception is re-thrown by specifying the exception in the throw statement, the stack trace is restarted at the current method and the list of method calls between the original method that threw the exception and the current method is lost. To keep the original stack trace information with the exception, use the throw statement without specifying the exception

Distintos formatos para módulos en JavaScript

Existen distintos formatos ya que JavaScript originalmente no incluía el concepto de modulos.

Asynchronous Module Definition (AMD)

Es usado por los navegadores.
Utiliza define como palabra reservada.

// suma.js
define(function() {
  return suma = function(r) {
    return r + r;
  }
});


// index.js
define(function(require) {
  require('./suma');
  suma(4); // = 8
}

CommonJS (CJS)

Se usa en npm y Node.js.
Utiliza require y module.exports para definir las dependencias de los modulos.

// utils.js
function suma(r){
  return r + r;
}
exports.suma = suma;


// index.js
var utils = require('./utils.js');
utils.suma(4); // = 8

ES Module (ESM).

Es parte del lenguaje nativo a partir de ECMAScript 6.
Utiliza import y export para definir las dependencias de los modulos.
La keyword System.register se diseño para dar soporte a los modulos en ECMAScript 5.

// suma.js
 export function suma(r) {
   return r + r;
 }


 // index.js
 import suma from "./suma";
 suma(4); // = 8

Universal Module Definition (UMD)

Se puede usar en el navegador y en Node.js.
https://github.com/umdjs/umd/tree/master/templates