Search This Blog

Thursday, November 5, 2020

Q6-Q10

Q6. How to implement CORS in .net core?
Q7. What are different types of services lifetime in microservices. Explain each of them?
Q8. How we can inject service dependency into controller?
Q9. Difference between inprocess and OutOfProcess hosting in .net core?
Q10. When we can use kestral server alone why we need reverse proxy server?
-----------------------------------------------------------------------------------------------------------------------------
Q6. How to implement CORS in dotnet 8?

Answer:
Full form of CORS is cross origin resource sharing. 
Implementing CORS in middleware is the most common practice in dotnet 8 applications

MiddleWare CORS

Step 1:
>> Install Nuget package "Microsoft.AspNetCore.Cors"

Step 2:
>> Add CORS service in program.cs

Step 3:
>> Use CORS middleware


-----------------------------------------------------------------------------------------------------------------------------
Q7. What are different types of services lifetime in microservices. Explain each of them?

Answer:
In .NET microservices, we configure service lifetimes in the Dependency Injection (DI) container, usually inside the Program.cs (in .NET 6+) or Startup.cs (older versions).
We use methods like:

services.AddSingleton<IMyService, MyService>();
services.AddScoped<IMyService, MyService>();
services.AddTransient<IMyService, MyService>();

Types of Service Lifetimes

Singleton 🟢

  • Created only once for the entire application lifetime.
  • Same instance is shared across all requests and users.
  • Example: Caching service, configuration reader.

Scoped 🟡
  • Created once per request (scope).
  • The same instance is reused within a single request but a new one is created for each new request.
  • Example: Database context (DbContext) in web APIs.

Transient 🔵
  • Created every time it’s requested.
  • Best for lightweight, stateless services.
  • Example: Utility services like email formatters, logging helpers.
-----------------------------------------------------------------------------------------------------------------------------
Q8. How we can inject service dependency into controller.

Answer:

Step 1: Create the Interface and the service itself

 public interface IHelloWorldService
 {
  string SaysHello();
 }
 
 public class HelloWorldService: IHelloWorldService
 {
  public string SaysHello()
  { 
   return "Hello ";
  }
 }
Step 2: Register the Service in DI Container

var builder = Webapplication.CreateBuilder(args);

//Register the service
builder.services.AddTransient<IHelloWorldService, HelloWorldService>();

Step 3: Inject the service into Controller

[ApiController]
[Route("api/controller")]

public class HomeController: ControllerBase
{
   private readonly IHelloWorldService _helloWorldService;

    public HomeController(IHelloWorldService helloWorldService)
    {
        _helloWorldService = helloWorldService;
    }
}


-----------------------------------------------------------------------------------------------------------------------------
Q9. Difference between inprocess and OutOfProcess hosting in .net core?

Answer:
In dotnet 8 InProcess and OutOfProcess are two hosting models for applications. 

InProcess Hosting:
>> InProcess hosting runs the ASP.NET Core application within the same process as the IIS worker process (w3wp.exe or iisexpress.exe).
>> This model provides improved performance because it avoids the overhead of proxying requests between IIS and the Kestrel server. Requests are handled directly within the IIS process.
>> Visual studio (not .net core CLI) Default hosting is InProcess. 
>> To configure InProcess hosting, set the <AspNetCoreHostingModel> property to InProcess in the project file (.csproj)

Outofprocess
>> OutOfProcess hosting runs the ASP.NET Core application in a separate process from the IIS worker process. The application runs on the Kestrel server, and IIS acts as a reverse proxy.
>> The internal web server is Kestrel, the external web server (also called reverse proxy server) can be IIS, Nginx or Apache.
>> To configure OutOfProcess hosting, set the <AspNetCoreHostingModel> property to OutOfProcess in the project file (.csproj):

-----------------------------------------------------------------------------------------------------------------------------
Q10. Why cannot we use Kestral server alone? Why we need reverse proxy server?

Answer:

The Kestrel Web Server can also be used with the combination of a reverse proxy server such as IIS, Apache, or Nginx. Now the question that should come to your mind is, If Kestrel can be used by itself as a web server which can directly handle and process the incoming HTTP Request, then why do we need a reverse proxy server?

This is because the reverse proxy server provides an additional layer of configuration and security which is not available with the Kestrel Server. It also maintains the load balancing. So, it is a good choice to use Kestrel Server along with a reverse proxy server

-----------------------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment

Q36-Q40(EF)

Q36.  What is the difference between Include and Join in entity framework? Q37. What will be your approach in case you want to call a manual...