Leo*_*lva 5 c# asp.net-mvc split azure multicastdelegate
我有一个仅在生产环境中发生的非常奇怪的问题。异常有消息
“委托给实例方法不能有 null 'this'”。
抛出异常的方法非常简单,并且工作了很长时间,所以问题一定是环境中的模糊依赖,或者类似的东西......
我正在使用托管在 Azure 中的 ASP.NET Web API,控制器的操作方法是通过 AJAX 执行的。
这是抛出异常的代码:
public class BlacklistService : IBlacklistService
{
public bool Verify(string blacklist, string message)
{
if (string.IsNullOrEmpty(blacklist)) return true;
var split = blacklist.ToLower().Split(';'); // exception is thrown here
return !split.Any(message.Contains);
}
}
Run Code Online (Sandbox Code Playgroud)
这是堆栈跟踪的相关部分:
at System.MulticastDelegate.ThrowNullThisInDelegateToInstance()
at System.MulticastDelegate.CtorClosed(Object target, IntPtr methodPtr)
at MyApp.Business.Services.BlacklistService.Verify(String blacklist, String message)
at MyApp.Business.Services.ContactMessageFactory.GetVerifiedStatus(String mensagem)
at MyApp.Business.Services.ContactMessageFactory.GetMailMessage(ContactForm contactForm)
at MyApp.Business.ContactEmailService.Send(ContactForm contactForm)
Run Code Online (Sandbox Code Playgroud)
有人可以找出导致此异常的可能原因吗?提前致谢。
问题在于事实message
确实如此null
。你可以很容易地重现这个:
void Main()
{
Verify("hello", null);
}
public bool Verify(string blacklist, string message)
{
if (string.IsNullOrEmpty(blacklist)) return true;
var split = blacklist.ToLower().Split(';'); // exception is thrown here
return !split.Any(message.Contains);
}
Run Code Online (Sandbox Code Playgroud)
发生的情况是通过方法组转换message.Contains
传递给Func<string, bool>
构造函数,如下所示:
Func<string, bool> func = ((string)null).Contains;
return !split.Any(func);
Run Code Online (Sandbox Code Playgroud)
这就是导致MulticastDelegate
发疯的原因。您还可以在生成的 IL 中看到:
IL_0028: ldftn System.String.Contains
IL_002E: newobj System.Func<System.String,System.Boolean>..ctor
IL_0033: call System.Linq.Enumerable.Any
Run Code Online (Sandbox Code Playgroud)
为了避免这种情况发生,请确保您也将检查消息设为空:
public bool Verify(string blacklist, string message)
{
if (string.IsNullOrEmpty(blacklist)) return true;
if (string.IsNullOrEmpty(message)) return false;
var split = blacklist.ToLower().Split(';'); // exception is thrown here
return !split.Any(message.Contains);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
569 次 |
最近记录: |