AutoMapper. Mapper. CreateMap reports "System. NullReferenceException: the object reference is not set to the instance of the object ." Recurrence of exceptions,

Source: Internet
Author: User

AutoMapper. Mapper. CreateMap reports "System. NullReferenceException: the object reference is not set to the instance of the object ." Recurrence of exceptions,

Navigation:

> I. problems during the National Day holiday

> II. An exception occurs again on weekends.

> III. troubleshooting

> IV. Abnormal Replay

> V. Post-repair monitoring

> VI. End

I. TOP problems during the National Day holiday

During the National Day holiday-July 22, October 5-the payment center frequently reported an exception "System. NullReferenceException: the instance of the object is not referenced .", By analyzing the exception stack information, the code appears in the QRCodeService's GetQRCode method, as shown in the following 8th lines of code:

1 /// <summary> 2 // get the Payment Code by scanning the QR code 3 /// </summary> 4 /// <param name = "reqModel"> </param> 5 // <returns> </returns> 6 public ResponseModelBase GetQRCode (QRCodeRequestModel reqModel) 7 {8 AutoMapper. mapper. createMap <QRCodeRequestModel, QRCodeRequestDTO> (); 9 var reqDto = AutoMapper. mapper. map <QRCodeRequestDTO> (reqModel); 10 if (reqDto. valid_minutes = 0) 11 {12 reqDto. valid_minutes = 20; // The default value is 20 minutes 13} 14 15 if (string. isNullOrEmpty (reqModel. order_no) 16 {17 throw new ResponseErrorException ("Order ID not obtained, please re-check order information"); 18} 19 if (reqModel. pay_money <= 0) 20 {21 throw new ResponseErrorException ("Incorrect order price, please verify this order again"); 22} 23 if (string. isNullOrEmpty (reqModel. goods_name) 24 ...... 25 ...... 26}

 

Exception log:

1 usd/10/5 14:03:40 [Renewal] request payment center parameter: {"biz_system": "5", "goods_name": "w1774251005140403733142/qdrpayment3577421317952512T/", "merchant_id ": "102573307097-102125439412", "policy_url ":" http://papi1.shenbianhui.cn//OrderPayBack/BackResult "," Order_no ":" dd1_100500470030 "," order_time ":" 20171005140340 "," pay_channel ":" 12 "," pay_money ":" 30000 "," remark ": "", "return_url ":" http://120.55.16.195/pay/returnYimeiCallBack.do "," Sign ":" signature "," third_pay_platform ":" 61 "," valid_minutes ":" 10 "} 2 Jun /10/5 14:03:40 [GetQRCode_140340241_DCB85] The payment center has been signed. 3 usd/10/5 14:03:40 [getqrcode_14034024108dcb85] Get the QR code to go to 4 usd/10/5 14:03:40 [getqrcode_14034024366dcb85] System exception: System. NullReferenceException. 5 In AutoMapper. typeMapFactory. <> c _ DisplayClass3_0. <MapDestinationPropertyToSource> B _ 0 (IMemberConfiguration _) 6 in System. linq. enumerable. any [TSource] (IEnumerable '1 source, func' 2 predicate) 7 in AutoMapper. typeMapFactory. createTypeMap (Type sourceType, Type destinationType, IProfileConfiguration options, MemberList memberList) 8 in AutoMapper. configurationStore. <> c _ DisplayClass80_0. <CreateTypeMap> B _ 0 (TypePair tp) 9 in System. collections. concurrent. concurrentDictionary '2. getOrAdd (TKey key, Func '2 valueFactory) 10 in AutoMapper. configurationStore. createMap [TSource, TDestination] (String profileName, MemberList memberList) 11 in PaymentService. QRCodeService. getQRCode (QRCodeRequestModel reqModel) 12 in PaymentPlatform. QRCode. getQRCode. myBiz (String requestJson) 13 in PaymentPlatform. QRCode. handlerBase. processRequest (HttpContext context)

There is a ConcurrentDictionary from the first row. Is it hard to be caused by concurrency? At that time, the system TPS concurrency was above 50.

The system has been running for several months and has never encountered such a problem before.

 

Further troubleshooting: After this exception occurs, all subsequent requests to this interface will report this exception. In addition, only 9177 of the three load nodes report this exception.

Intuitively, with the release of an online version once or twice a week, will the AutoMapper. DLL versions of the three nodes be inconsistent. In order to minimize the impact, contact the O & M personnel urgently to delete the site files of the 9177 nodes and copy them from other nodes. After the O & M notification is completed, the problem does not recur.

In the next few days, no similar accidents have occurred online. Therefore, I think the problem is caused by the file version, so I will not pay attention to it.

Ii. TOP again after an exception on weekends

Who knows, in the last two days of the weekend, this exception appeared again, but it still appeared at 9177 nodes.

This time, we cannot understand it. The file version should be consistent.

Contact O & M quickly to help recycle the application pool of the site.

Who knows the problem persists after recovery.

Consultation on O & M, why is this exception reported only on this node? O & M disclosed that the file was not copied on July 15, October 5, but iis was restarted.

So we had to make the O & M restart iis. The incident has been put over for now.

 

Iii. TOP troubleshooting

In order to stop the day for the next weekend, I decided to solve this problem today.

According to our preliminary analysis, AutoMapper. Mapper. CreateMap is a static method and may cause problems when multithreading.

I have always known that AutoMapper recommends initializing the ing relationship at one time when the program starts, instead of initializing the object each time it is converted.

I did not pay attention to this point when I was using AutoMapper. It is written as creating a ing and then converting the object each time.

 

The overdue payment is always paid back. The system has not encountered such an exception before. It can only be said that the time is not enough.

As Murphy's theorem says, what happens will always happen. No, it's not too early or too late. It appears on the National Day and last weekend holidays.

 

Iv. TOP abnormal Replay

It is necessary to reproduce the exception. Of course, the small probability problem is not easy to test.

Fortunately, I have JMeter.

The AutoMapperTest. ashx file is created in the project. The ProcessRequest method is as follows:

public void ProcessRequest(HttpContext context){    context.Response.ContentType = "text/plain";        LogHelperUtil logHelper = new LogHelperUtil("", LogType.HFAgentPayService);    LogHelper.Write("[AutoMapperTest]");        Thread.Sleep(new Random().Next(1, 100));        try    {        QRCodeRequestModel reqModel = new QRCodeRequestModel();        AutoMapper.Mapper.CreateMap<QRCodeRequestModel, QRCodeRequestDTO>();        var reqDto = AutoMapper.Mapper.Map<QRCodeRequestDTO>(reqModel);        Thread.Sleep(new Random().Next(100, 1000));        context.Response.Write(JsonConvert.SerializeObject(reqDto));    }    catch (Exception ex)    {        logHelper.Write("[AutoMapperTestException]" + ex.ToString());        context.Response.Write(ex.ToString());    }}

 

Release to test environment.

Next, create a JMeter test plan and simulate the number of 1000 threads to test the ashx.

It lasted 20 minutes.

In process,

  • After overwriting the files on the site again, the AutoMapper. Mapper. CreateMap statement encountered the following exception, 160 times at 11:08:08.
11:08:08 [AutoMapperTestException] System. InvalidOperationException: The set has been modified. enumeration may not be performed. In System. collections. generic. list '1. enumerator. moveNextRare () in System. linq. enumerable. any [TSource] (IEnumerable '1 source, func' 2 predicate) in AutoMapper. typeMapFactory. createTypeMap (Type sourceType, Type destinationType, IProfileConfiguration options, MemberList memberList) in AutoMapper. configurationStore. <> c _ DisplayClass80_0. <CreateTypeMap> B _ 0 (TypePair tp) in System. collections. concurrent. concurrentDictionary '2. getOrAdd (TKey key, Func '2 valueFactory) in AutoMapper. configurationStore. createMap [TSource, TDestination] (String profileName, MemberList memberList) in PaymentPlatform. test. autoMapperTest. processRequest (HttpContext context)

 

  • At noon, the application pool was manually reclaimed from the O & M personnel. The AutoMapper. Mapper. CreateMap statement encountered the following exception, which is the same as the exception on holidays. This exception occurs between 12:02:04 and ~ It appears 43351 times between 12:03:17.
12:02:04 [AutoMapperTestException] System. NullReferenceException: the object reference is not set to the instance of the object. In AutoMapper. typeMapFactory. <> c _ DisplayClass3_0. <MapDestinationPropertyToSource> B _ 0 (IMemberConfiguration _) in System. linq. enumerable. any [TSource] (IEnumerable '1 source, func' 2 predicate) in AutoMapper. typeMapFactory. createTypeMap (Type sourceType, Type destinationType, IProfileConfiguration options, MemberList memberList) in AutoMapper. configurationStore. <> c _ DisplayClass80_0. <CreateTypeMap> B _ 0 (TypePair tp) in System. collections. concurrent. concurrentDictionary '2. getOrAdd (TKey key, Func '2 valueFactory) in AutoMapper. configurationStore. createMap [TSource, TDestination] (String profileName, MemberList memberList) in PaymentPlatform. test. autoMapperTest. processRequest (HttpContext context)

 

Attached to the JMeter pressure test:

 

V. Post-repair monitoring TOP

As mentioned in section III. troubleshooting, the solution is to define all AutoMapper type mappings in the global Application_Start. This ensures the one-time initialization of the ing relationship. Subsequent Code does not need to be defined. You only need to focus on Object conversion.

Release to test environment.

Start the JMeter test plan again. Within 14 minutes, no exception occurred to the AutoMapper creation type ing caused by concurrency, whether the file is updated during the stress test or the application pool at the recycle bin point.

Vi. End TOP

Again, you should try to manage the autoing definition of AutoMapper in a unified manner and only perform one-time initialization.

In an AutoMapper "AutoMapper best practices" article in the blog:

Although AutoMapper does not require all configurations to be provided at one time when the program starts, it has the following benefits:
A) All configurations can be strictly verified when the program is started (detailed later ).
B) You can uniformly specify the general behavior of DTO entiing to Entity (described later ).
C) logical cohesion: when adding a configuration, it is easy to imitate the previously written configuration. It is easy to intuitively grasp the total number of DTO in the project and their ing relationship with the object.

 

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.