WCF基于任务的Xamarin服务消耗

时间:2014-08-19 06:28:10

标签: c# android wcf xamarin xamarin.android

我正在重构WPF项目以添加对android的支持,我有一个用于消费WCF服务的PCL项目。相同的代码适用于WCF,但在Android应用程序中生成错误。

这是服务:

[ServiceContract]
public interface IMessage
{
    [OperationContract]
    Task<string> GetMessages(string msg);
}

及其实施:

public class Message : IMessage
{
    public async Task<string> GetMessages(string msg)
    {
        var task = Task.Factory.StartNew(() =>
        {
            Thread.Sleep(3000);
            return "Return from Server : " + msg;
        });
        return await task.ConfigureAwait(false);
    }
}

创建ClientBase&lt;&gt;的辅助类:

public class Proxy : ClientBase<IMessage>
{
    public Proxy()
    {
    }
    public Proxy(string endpointConfigurationName) :
        base(endpointConfigurationName)
    {
    }
}

这就是服务的消费方式:

var client = new Proxy("BasicHttpBinding_IMessage");
HelloServer = "Waiting for the result";
HelloServer = await client.Channel.GetMessages(Hello);

WPF应用程序没有问题,但Android生成错误:

System.Runtime.Serialization.SerializationException:
Deserializing type 'System.Threading.Tasks.Task1[[System.String, mscorlib,
Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]'.
Expecting state 'EndElement'. Encountered state 'Text' with name '' with namespace ''. (1,141)`

0 个答案:

没有答案