简体   繁体   中英

WCF for windows phone 8.1 silverlight

I'm having problem using wcf with wp8.1 silverlight. I countinously getting the error The contract 'IPhoneService' contains synchronous operations, which are not supported in Silverlight. Split the operations into "Begin" and "End" parts and set the AsyncPattern property on the OperationContractAttribute to 'true'. Note that you do not have to make the same change on the server. After I changed my syncronous method to async I'm still getting the same error (I updated the service reference.). Out of curiousity I tried to use it on a console app, and it works perfectly. Previously I did get an another error that might have something to do with it. Adding a service reference generated an app.config file, but the app needed a ServiceReferences.ClientConfig, so I simply renamed it.

For now I changed back the WCF method to syncronous:

public int GetData()
        {
            return 12;
        }

and on my MainViewModel (I'm using MVVMLight toolkit):

 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();
        }

and i implemeneted before the async method like this, getting the same error anyway:

 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
        }

The problem was VS2013 RC2 version. The reference wasn't generated correctly. An update solved the problem

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