Home > Software design >  Where can I register unit of work service in .NET 6?
Where can I register unit of work service in .NET 6?

Time:02-24

I wonder how I can register unitofwork service in .NET 6.

In previous versions of .NET Core, it was possible to register a unitofwork service in startup.cs file. In .NET 6, startup.cs and program.cs files are merged.

Is it possible to register the service in program.cs? If possible, then how?

CodePudding user response:

Let's begin with your questions

"Is it possible to register the service in program.cs?"

Yes its Absolutely possible to register service in program.cs.

"If possible, then how?"

Here is the Program.cs example for you on asp.net 6:

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddScoped<IUnitOfWork, UnitOfWork>();

Note:

It’s almost similar to asp.net core 3.2 or asp.net 5 which we used to do in startup.cs file. Same thing need to do in enter image description here

enter image description here

Hope above steps guided you accordingly. If you still need more details you can have a look our official document

CodePudding user response:

The new .Net 6 is a simplified version of the previous .Net 5.

You can do dependency injection within the Program.cs file in your project root directory.

Example:

You should have this line

var builder = WebApplication.CreateBuilder(args);

If not already declared, create the following variable:

var app = builder.Build();

You can then interact with services by using "app.Services" property.

app.Services.GetServices(typeof(MyService));

Or you can use your own middleware

app.MyMiddleware();

Refer to this Microsoft Documentation page for migration examples.

  • Related