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
serviceinprogram.cs.
"If possible, then how?"
Here is the
Program.csexample for you onasp.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.2orasp.net 5which we used to do in startup.cs file. Same thing need to do inHope above steps guided you accordingly. If you still need
more details you can have a look our official documentCodePudding user response:
The new .Net 6 is a simplified version of the previous .Net 5.
You can do dependency injection within the
Program.csfile 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.


