Home > Net >  Who is a great god for help to reassure, singleton is released the question?
Who is a great god for help to reassure, singleton is released the question?

Time:11-26

In.net 3.0 in the core, through the Program to initialize the singleton, but in the subsequent API request singleton is empty, static members will be released?

 
Public class Program
{
Public static void Main (string [] args)
{
CreateWebHostBuilder (args);
}

Public static void CreateWebHostBuilder (string [] args)
{
Var configuration=new ConfigurationBuilder (). SetBasePath (Environment. CurrentDirectory)
. AddJsonFile (" host. Json ")
The Build ();

IWebHost host=to WebHost. CreateDefaultBuilder (args) UseUrls (configuration [" url "])
. UseStartup (). The Build ();

ApplicationEngine. StartEngine ();

The host. The Run ();

}
}



Public class ApplicationEngine
{

Private static ApplicationEngine _uniqueEngine;

Private ApplicationEngine () {
}

Public static int StartEngine ()
{
If (_uniqueEngine==null)
{
The lock (instanceObjectLock)
{

If (_uniqueEngine==null)
{

_uniqueEngine=new ApplicationEngine ();
}

}

}
return 0;
}
Public static ApplicationEngine Instance
{
The get
{
Return _uniqueEngine;
}
}


Subsequent access to the Instance is empty

CodePudding user response:

The breakpoint with a minute _uniqueEngine be new static should not be released

CodePudding user response:

Well, we can't say no, we just want to say, when you make sure you visit he must have finished the StartEngine??

Since in netcore discussion question, of course, we said that, although you so that we can't say wrong, can only say that if the netcore, his nature writing is

Public class ApplicationEngine: if you want to initialize the first please inheritance Ihostservice
{
}

To WebHost. CreateDefaultBuilder (args)
ConfigureServices (p=& gt;
{
P.A ddSingleton (a);
//if you want to have the initialization, and to ensure the initialization process, combined with
P.A ddHoesedService (p=& gt; P.g etService ())
})


Behind you should be, how to control access to him, you should understand

CodePudding user response:

Not have injected directly into the singleton
Services. AddSingleton<>

CodePudding user response:

InstanceObjectLock? Did not see the statement
Since then use the.net core, the Singleton can be passed di di to statement

CodePudding user response:

Still want to mention a mouth, you all of this with the netcore, also need not di singleton, stem what

CodePudding user response:

How to obtain the integral

CodePudding user response:

Don't understand. Netcore, take a look
  •  Tags:  
  • C#
  • Related