WCF for windows phone 8.1 silverlight

本文关键字:silverlight phone for windows WCF | 更新日期: 2023-09-27 18:09:51

我使用wcf与wp8.1 silverlight有问题。合同"IPhoneService"包含同步操作,这在Silverlight中不支持。将操作拆分为"Begin"answers"End"部分,并将OperationContractAttribute上的AsyncPattern属性设置为"true"。注意,您不必在服务器上进行相同的更改。在我将同步方法更改为async之后,我仍然得到相同的错误(我更新了服务引用)。出于好奇,我试着在一个主机应用上使用它,效果非常好。以前我确实得到了另一个可能与此有关的错误。添加一个服务引用会生成一个app.config文件,但是这个应用需要一个ServiceReferences。

现在我把WCF方法改回了synchronized:

public int GetData()
        {
            return 12;
        }

和在我的MainViewModel(我使用MVVMLight工具包):

 public void Load()
        {
            var client = new ServiceReference1.PhoneServiceClient();
            client.GetDataCompleted += client_GetDataCompleted;
            client.GetDataAsync();
        }
        void client_GetDataCompleted(object sender, ServiceReference1.GetDataCompletedEventArgs e)
        {
            Title = e.Result.ToString();
        }

和我之前实现的异步方法像这样,得到相同的错误:

 public IAsyncResult BeginGetData(AsyncCallback callback, object asyncState)
        {
            var msg = 12;
            return new CompletedAsyncResult<int>(msg);
        }
        public int EndGetData(IAsyncResult r)
        {
            CompletedAsyncResult<int> result = r as CompletedAsyncResult<int>;            
            return result.Data;
        }
        class CompletedAsyncResult<T> : IAsyncResult
        {
            T data;
            public CompletedAsyncResult(T data)
            { this.data = data; }
            public T Data
            { get { return data; } }
            #region IAsyncResult Members
            public object AsyncState
            { get { return (object)data; } }
            public WaitHandle AsyncWaitHandle
            { get { throw new Exception("The method or operation is not implemented."); } }
            public bool CompletedSynchronously
            { get { return true; } }
            public bool IsCompleted
            { get { return true; } }
            #endregion
        }

WCF for windows phone 8.1 silverlight

问题是VS2013 RC2版本。引用没有正确生成。更新解决了这个问题