2. application_start and application_end methods in the global. asax file of the oixtesite Project
View the global. asax file of the oxitesite project and find its implementationCodeIn the oxiteapplication class of the oxite project. Oxitesite is initialized in the application_start method.
What did the application_start method do?
First, set the dependency injection container and store it to the application.ProgramStatus (httpapplicationstate)
The second is to load the dependency injection container used in the oxite module according to the configuration (for details, see Enterprise Library 4.0 or later ).
The setupcontainer method sets the injection container and returns an unitycontainer object, which is saved as application ["Container"]. This method will be carefully analyzed later.
Application ["bootstrappersloaded"] is used to identify whether Initialization is complete.
Load () method call static method load (httpcontextbase context ). The function is to load the specified module according to the configuration ).
The application_end method calls the unload () method. At the end of the application, some modules are cleaned up.
Iii. setupcontainer Method
Prerequisites: Unity (IOC/di) and custom web. config configuration nodes
The method name of the setupcontainer method is used to set the dependency injection container.
In the setupcontainer method, first define an iunitycontainer variable parentcontainer: first, register several basic objects as a singleton: parentcontainer. registerinstance (oxiteconfigurationsection) configurationmanager. getsection ("oxite ")). registerinstance (New receivettingshelper (configurationmanager. appsettings )). registerinstance (routetable. routes ). registerinstance (system. web. MVC. modelbinders. binders ). registerinstance (viewengines. engines ). registerinstance (hostingenvironment. virtualpathprovider );
Oxiteconfigurationsection class, custom configuration node. Its definition bit is in the namespace of oxite. configuration. Is a modular configuration file in oxite. The configured nodes are separately placed in the oxite. config file under the oxitesite project.
The deleettingshelper class wraps configurationmanager. deleetmanager and provides several reading methods, such as getint32 and getstring, to read values under the deleettings node in the web. config file. In fact, you can add these read methods to the namevaluecollectionextensions class (under the oxite. Extensions namespace ). However, I thought about it later. The purpose of this class can also be clarified by naming ettingshelper.
The routetable. routes static property returns a routecollection static object. The routecollection class is defined in the system. Web. routing program. Used to save URL routing settings. The purpose of container injection is to perform unit testing.
The static attribute of modelbinders. Binders returns a static modelbinderdictionary object. It is used to process data binding operations (obtain forms, query data, convert data, and generate URL paths ).
The viewengines. Engines static property returns a viewenginecollection static object. Used for view engine.
Hostingenvironment. virtualpathprovider returns a virtualpathprovider static object. Personal guesses may be used in custom viewengine, but the oxite version does not seem to be available yet, and an error is reported if it is commented out.
Routetable. routes, modelbinders. binders, and viewengines. Engines are basic attributes or objects of ASP. net mvc. It is worth the time to study independently.
Then, register connectionstrings under connectionstrings in Web. config and the custom node "oxite" (oxite. config file) as a single piece. Foreach (connectionstringsettings connectionstring in configurationmanager. connectionstrings) parentcontainer. registerinstance (connectionstring. name, connectionstring. connectionstring); foreach (connectionstringsettings connectionstring in parentcontainer. resolve <oxiteconfigurationsection> (). connectionstrings) parentcontainer. registerinstance (connectionstring. name, connectionstring. connectionstring );
Question 1: If the oxonstrings at the oxite node changes while the site is not restarted, how can I update it to the dependency injection container? The processing here seems inadequate. Later, I went to oxite.codeplex.com to ask, and erikporter OF THE oxite project team said that the site had to be restarted. We hope they can fix it as soon as possible, otherwise the so-called hot swapping of modules will be greatly reduced.
Next, let's take a look at the setupcontainer method: The loadmodules class and the loadbackgroundservices class are located in the oxite. bootstrappertasks namespace. From the perspective of class naming, one is related to the loading module, and the other is related to the loading of background services. The specific content will be detailed later. Both implement the ibootstrappertask interface in the oxite. Infrastructure namespace. Ibootstrappertask has two methods: Execute and clearup. I think ibootstrappertask can be literally translated as a Bootstrap program interface, and its instance can be called a Bootstrap program.
Here we only need to know that the modules instance and the loadbackgroundsercies instance are registered as single pieces respectively.
Next, let's take a look at the setupcontainer method to register some types into the dependency injection container. Apart from several custom life cycle types, all others are simple ing, which is not mentioned here.
Before setupcontainer ends the return value, the Unity configuration node in the web. config file is registered into the container. If the configuration node is the same as the setting in hard encoding, the configuration in hard encoding will be overwritten. This feature is very useful. It allows us to use the default configuration of the program and provide an interface for us to replace. For details, you can view information about unity.