Where is the single-created instance? [ASP. NET mvc4]

Source: Internet
Author: User

During the architecture process today, I encountered the following problem: the single-built instance is gone!

It was really strange that I thought about countless methods and did n tests all afternoon, but I couldn't find them.

This is generally the case:

In the initial stage of project startup, it is easy to initialize some option values in a single-created instance? Now, implementation is started, and it will be completed in a moment. It is normal to create a console program for testing.

Well, what we did just now is the service part of a website. Now we start to build a website. ASP. NET mvc4. Will it be okay later? Test, renew, and initialize the single-creation instance without all the values?

Dizzy ......

Of course, I used reflection for initialization, so a series of single-creation tasks do not need to be specified, and a brain will be fully automated for initialization.

The following is the core code of implementation:

VaR optionlist = new list <icodersetting> (); // traverses all the options found for instance foreach (type Klass in types) {try {If (Klass. isabstract) continue; If (Klass. fullname! = NULL) {If (! Parmsinfomap. containskey (Klass. fullname) {_ logger. Info (Klass. fullname + "the programmer configuration information is not found. This programmer configuration will not be enabled. "); Continue ;}}// create a programmer to configure var prop = Klass. getproperty (" me ") through the attribute name of the static attribute of a single instance ")?? Klass. getproperty ("instance"); If (prop! = NULL) {var option = (icodersetting) (prop. getvalue (null, null); If (null! = Option) {// fill in the order (SORT) attribute of option type if (Klass. fullname! = NULL) if (parmsinfomap. containskey (Klass. fullname) {xmlelement orderele = parmsinfomap [Klass. fullname]; If (orderele. hasattribute ("order") {int order; If (Int. tryparse (orderele. getattribute ("order"), out order) _ logger. trace (Klass. name + "is defined as" + order); option. order = order ;}}// put a valid option into the optionlist in the set. add (option) ;}} else {_ logger. warn (string. format ("the properties of the single-creation instance configured by the programmer are not found. {0} ", Klass. fullname) ;}} catch (exception e) {_ logger. Error (string. Format (" {0} programmer configuration initialization exception. {1} ", Klass. fullname, E. message), e) ;}}// sort optionlist according to the sorting value defined by each programmer in the configuration file. sort (); // traverses all option instances and calls the initialization method of these options to initialize foreach (icodersetting option in optionlist) {try {type = option. getType (); methodinfo method = type. getmethod (initializes, (bindingflags. nonpublic | (bindingflags. public | bindingflags. instance); // call the initializes (source) method, if (type. fullname! = NULL) if (parmsinfomap. containskey (type. fullname) method. invoke (option, new object [] {parmsinfomap [type. fullname]}); _ logger. info (string. format ("program's {0} programmer configuration type initialization successful. ", Type. name); _ optionmap. add (type. name, option);} catch (exception e) {_ logger. error (string. format ("{0} programmer configuration initialization exception. {1} ", option. GetType (). fullname, E. Message), e );}}

Some code, even if it runs normally, means data is lost and the features of a single instance are lost. But the same code has been used since year 34 (this is a framework I specifically wrote called "Programmer configuration"). It is no problem in winform and WPF applications, the website project becomes invalid.

Although the cause of the fault has been found, it is hard to understand why.

It's easy to solve. In another way, it will take 20 minutes. But what exactly happened ??? Study again when you are free.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.