简体   繁体   中英

Calling an async WCF method throws an exception

I have a WCF service, that needs to convert a video. I need to call that method from my Xamarin app. If I call the regular method everything works as expected, but if I call the Async method, I get the error below. I already set the IncludeExceptionDetailInFaults on my WCF service to true, to get the details of the error.

WCF:

The interface:

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

The service:

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:

This works:

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

This throws an error:

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

The error:

{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/').}

EDIT: This only happens on Xamarin. I have tried it with WPF and everything works fine there.

your _Client should have _Client.ConvertVideoCompleted . your code should be something like

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM