'how to migrate global.asax event 'Application_Start' to program.cs on net.6?
I try to migrate an old projet with a console app and a webservice ( both on .net framework 4.6) to a .net 6 console app (I want to suppress the ws and just have a service). On the old webservice I have a global.asax with a usefull event 'Application_Start' : it let me initialise a static class used for data access (code is in vb.net but I move to c#)
Sub Application_Start(sender As Object, e As EventArgs)
OldDLL.Initialisation(BmyEnum.Mode, My.Settings.mySCHEMAName)
End Sub
I find various topics as (Migrate Global.asax to Startup.cs) on how to migrate from global.asax to Startup.cs but there is no more Startup.cs on my project generate by visualstudio2022, just a program.cs. Googleling a little bit I read that program.cs could replace startup.cs, using middleware. But I'm very new and it's a lot confusing. How I could in a simple way call OldDLL.Initialisation on program.cs ? Could be possible ( and how?) to embed my OldDll static on a service and inject it ? Thank you for reading, thank you for your suggestions.
Solution 1:[1]
in fact my projet is a vs2022 ASP.net core with react js projet. I found how to do: I create a service class to embed my dll:
public class OldDLLService:IOldDllService
{
public OldDllService()
{
oldDLL.Initialisation();
}
public void DoSome()
{
//..
}
}
And the interface playing with :
public interface IOldDllService
{
void DoSome();
}
On programm.cs I injected the service :
builder.Services.AddSingleton<IOldDllService, OldDllService>();
var app = builder.Build();
And after can easy use for example in a controller:
public DummyController(ILogger<DummyController> logger, IOldDllService oldDllService)
{
_logger = logger;
_oldDllService = oldDllService;
public void doSome()
{
_oldDllService.DoSome();
}}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
