我已经从 NuGet 为Json.Net v5.06 编写了一个小型异步库包装器,在更新了一些 Telerik 包后,我的 UnitTests 开始失败并出现MissingMethodException。
我创建了一个有效的虚拟非异步方法,所以我很困惑错误在哪里。
项目目标平台
.Net 4.5
x86 中央处理器
异步操作
public class JsonSerialiser : ISerialiser
{
[InjectionConstructor]
public JsonSerialiser(IStringCompression streamCompressor, ILog logger)
{
if (streamCompressor == null) throw new ArgumentNullException("streamCompressor");
if (logger == null) throw new ArgumentNullException("logger");
XmlConfigurator.Configure();
this.streamCompressor = streamCompressor;
this.logger = logger;
}
public async Task<string> SerialiseAsync<T>(T serialseObject) where T : class
{
if (serialseObject == null) throw new ArgumentNullException("serialseObject");
try
{
return await JsonConvert.SerializeObjectAsync(serialseObject);
}
catch (JsonSerializationException ex)
{
logger.Error(ex);
throw new SerialisationException("Could Not Serialse The Object", ex);
}
}
}
异步示例
现在将这段代码放在一起只是为了测试基本的序列化,我在类构造函数中跳过了空检查。
private async void button1_Click(object sender, EventArgs e)
{
List<Part> parts = new List<Part> { new Part() { AbstractType = typeof(IOpcController), ConcreteType = typeof(OpcController) },
new Part() { AbstractType = typeof(ISerialiser), ConcreteType = typeof(JsonSerialiser) },
new Part() { AbstractType = typeof(IStringCompression), ConcreteType = typeof(StringGZipCompression)}};
string serialisedResult = string.Empty;
JsonSerialiser serialiser = new JsonSerialiser(null, null);
serialisedResult = await serialiser.SerialiseAsync<List<Part>>(parts);
}
异步结果
这一代一个 MissingMethodException
Method not found: 'System.Threading.Tasks.Task`1<System.String> Newtonsoft.Json.JsonConvert.SerializeObjectAsync(System.Object)'.
at Helper.Core.Serialisation.Json.JsonSerialiser.<SerialiseAsync>d__0`1.MoveNext()
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine)
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.Start[TStateMachine](TStateMachine& stateMachine)
at Helper.Core.Serialisation.Json.JsonSerialiser.SerialiseAsync[T](T serialseObject)
at OpcTester.Form1.<button1_Click>d__9.MoveNext() in c:\Users\phil.murray\Desktop\tmp\OpcTester\Form1.cs:line 44
无 异步操作
只是为了检查是否是导致问题的方法的异步部分,我编写了一个非异步实现。
public string Serialise<T>(T serialseObject) where T : class
{
if (serialseObject == null) throw new ArgumentNullException("serialseObject");
try
{
return JsonConvert.SerializeObject(serialseObject);
}
catch (JsonSerializationException ex)
{
logger.Error(ex);
throw new SerialisationException("Could Not Serialse The Object", ex);
}
}
无 异步实现
private async void button1_Click(object sender, EventArgs e)
{
List<Part> parts = new List<Part> { new Part() { AbstractType = typeof(IOpcController), ConcreteType = typeof(OpcController) },
new Part() { AbstractType = typeof(ISerialiser), ConcreteType = typeof(JsonSerialiser) },
new Part() { AbstractType = typeof(IStringCompression), ConcreteType = typeof(StringGZipCompression)}};
string serialisedResult = string.Empty;
JsonSerialiser serialiser = new JsonSerialiser(null, null);
serialisedResult = serialiser.Serialise<List<Part>>(parts);
}
无 异步结果
该方法完成并将 List 序列化为字符串。
失败的测试示例
Test Name: SerialiserSerialiseObjectExists
Test FullName: Helper.Tests.SerialiserTests.SerialiserSerialiseObjectExists
Test Source: c:\Perforce\Development\SharedAPIs\Helper.Core\Helper.Tests\SerialiserTests.cs : line 38
Test Outcome: Failed
Test Duration: 0:00:00.0116216
Result Message:
Test method Helper.Tests.SerialiserTests.SerialiserSerialiseObjectExists threw exception:
System.MissingMethodException: Method not found: 'System.Threading.Tasks.Task`1<System.String> Newtonsoft.Json.JsonConvert.SerializeObjectAsync(System.Object)'.
Result StackTrace:
at Helper.Core.Serialisation.Json.JsonSerialiser.<SerialiseAsync>d__0`1.MoveNext()
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine)
a
t System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.Start[TStateMachine](TStateMachine& stateMachine)
at Helper.Core.Serialisation.Json.JsonSerialiser.SerialiseAsync[T](T serialseObject)
at Helper.Tests.SerialiserTests.<SerialiserSerialiseObjectExists>d__3.MoveNext() in c:\Perforce\Development\SharedAPIs\Helper.Core\Helper.Tests\SerialiserTests.cs:line 40
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
没有 Json.Net 的异步测试
只是为了争论,我用一个虚拟任务替换了 Json.Net 调用,它工作了,所以看起来问题出在使用 Await\Async 调用 Json.net 上。奇怪,因为它正在工作并且版本尚未更新。
public async Task<string> SerialiseAsync<T>(T serialseObject) where T : class
{
if (serialseObject == null) throw new ArgumentNullException("serialseObject");
try
{
//return await JsonConvert.SerializeObjectAsync(serialseObject);
return await Task.Run(() => string.Empty);
}
catch (JsonSerializationException ex)
{
logger.Error(ex);
throw new SerialisationException("Could Not Serialse The Object", ex);
}
}
问题
现在,在我更新 Telerik 控件套件之前,异步方法 UnitTests 以前可以工作,并且我确实在许多真实世界的实例中测试了该操作。我并不是说 Telerik 更新导致了问题,因为这可能是巧合。在测试其他类(与 Json.Net 无关)时,会通过许多其他异步测试用例。
知道 Async 方法有什么问题以及如何解决问题吗?
可能的解决方案
当我继续调查这个问题时,我发现问题可能出在 Json.Net 库中的异步调用中,所以我将无异步调用包装在一个任务中,如下所示,它有效
public async Task<string> SerialiseAsync<T>(T serialseObject) where T : class
{
if (serialseObject == null) throw new ArgumentNullException("serialseObject");
try
{
//return await JsonConvert.SerializeObjectAsync(serialseObject);
return await Task.Run<string>(() => JsonConvert.SerializeObject(serialseObject));
}
catch (JsonSerializationException ex)
{
logger.Error(ex);
throw new SerialisationException("Could Not Serialse The Object", ex);
}
}
出于兴趣,我下载了 Json.Net 的源代码并检查了 JsonConvert.SerializeObjectAsync 调用,它正在做同样的事情,所以我再次不确定根本问题。
public static Task<string> SerializeObjectAsync(object value, Formatting formatting, JsonSerializerSettings settings)
{
return Task.Factory.StartNew(() => SerializeObject(value, formatting, settings));
}