调用异步 WCF 方法会引发异常



我有一个WCF服务,需要转换视频。我需要从我的 Xamarin 应用调用该方法。如果我调用常规方法,一切都按预期工作,但是如果我调用异步方法,则会出现以下错误。 我已经将 WCF 服务上的 IncludeExceptionDetailInFaults 设置为 true,以获取错误的详细信息。

周转基金:

界面:

[ServiceContract]
public interface IConvert
{
[OperationContract]
bool ConvertVideo(string path);
}

服务内容:

public class ConvertService : IConvert
{
public bool ConvertVideo(string path)
{
Console.WriteLine("Converting video... wait 3 sec");
Thread.Sleep(3000);
Console.WriteLine("Video converted!");
Console.WriteLine(path);
return true;
}
}

Xamarin:

这有效:

try
{
_Client.ConvertVideo(GetFullFtpPath());
}
catch (Exception e) { }

这将引发错误:

try
{
_Client.ConvertVideoAsync(GetFullFtpPath());
}
catch (Exception e) { }

错误:

{System.ServiceModel.FaultException`1[System.ServiceModel.ExceptionDetail]: 
Error in deserializing body of request message for operation 'ConvertVideo'. 
OperationFormatter encountered an invalid Message body. 
Expected to find node type 'Element' with name 'ConvertVideo' and namespace 'http://tempuri.org/'. 
Found node type 'Element' with name 'ConvertVideoAsync' and namespace 'http://tempuri.org/' 
(Fault Detail is equal to Error in deserializing body of request message for operation 'ConvertVideo'. 
OperationFormatter encountered an invalid Message body. 
Expected to find node type 'Element' with name 'ConvertVideo' and namespace 'http://tempuri.org/'. 
Found node type 'Element' with name 'ConvertVideoAsync' and namespace 'http://tempuri.org/').}

编辑:这只发生在Xamarin上。我已经用WPF尝试过,那里一切正常。

你的_Client应该有_Client.ConvertVideoCompleted。 你的代码应该是这样的

try
{
_Client.ConvertVideoCompleted+= yourHandler;
_Client.ConvertVideo(GetFullFtpPath());
}
catch (Exception e) { }

请参阅 https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-call-wcf-service-operations-asynchronously

最新更新