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

时间:2017-08-17 13:29:40

标签: c# wcf xamarin service

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

WCF:

界面:

[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尝试过,一切都很好。

1 个答案:

答案 0 :(得分:0)

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

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

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