开发者

NullReferenceException using AutoMapper and StructureMap

开发者 https://www.devze.com 2023-04-10 19:21 出处:网络
So, I get a NullReferenceException thrown on a production server. It\'s not reproducible and happens once every ~100 request.

So, I get a NullReferenceException thrown on a production server. It's not reproducible and happens once every ~100 request.

This is the code.

var userInfo = Mapper.Map<UserSubscribedEvent, UserInfo>(userSubscribedEvent);
var subscription = repository.GetActiveSubscriptionForUser(userInfo.UserId);

The exception appears on the second line. So it's either userInfo that is null开发者_StackOverflow社区 or it is the repository.

The repository is injected into the class from the constructor using StructureMap so it really should not be null (since it usually works) and userInfo is created using AutoMapper and should not be null either.

So my question is - Can Mapper.Map return null in AutoMapper, if so, when? If not, have anyone experienced that StructureMap randomly injects null dependencies into the constructor? If so, can it be avoided?

Thanks!


Can Mapper.Map return null in AutoMapper

Yes.

if so, when?

If you pass null as argument. In your case this would be the userSubscribedEvent variable.


SOLVED: The problem was actually on the line below when used a property on subscription (embarrassed), the server ran with a file that had an extra empty line at the top. There where no active subscriptions since the client sent in the request just after it had closed the active subscription. :-/

Thanks again for your help and sorry for not checking my own production code better.

0

精彩评论

暂无评论...
验证码 换一张
取 消