Best way to catch a WCF exception in Silverlight?

前端 未结 9 1092
悲哀的现实
悲哀的现实 2020-12-31 06:43

I have a Silverlight 2 application that is consuming a WCF service. As such, it uses asynchronous callbacks for all the calls to the methods of the service. If the service

9条回答
  •  遥遥无期
    2020-12-31 07:12

    I'm not a plumber, so I decided to create my own WCF service class that overrides some of the functionality of the class file "reference.cs" that is automatically generated by Visual Studio, I then added my own try/catch blocks to catch communication errors.

    The class I created looks something like this:

    public class myWCFService : MyWCFServiceClient
    {
    
        protected override MyController.MyService.IMyWCFService CreateChannel()
        {
            return new MyWCFServiceClientChannel(this);
        }
    
    }
    
    private class MyWCFServiceClientChannel : ChannelBase, MyController.MyService.IMyWCFService
    {
        /// 
        /// Channel Constructor
        /// 
        /// 
        public MyWCFServiceClientChannel(System.ServiceModel.ClientBase client) :
        base(client)
        {
        }
        /// 
        /// Begin Call To RegisterUser
        /// 
        /// 
        /// 
        /// 
        /// 
        public System.IAsyncResult BeginRegisterUser(MyDataEntities.MembershipInformation memberInformation, System.AsyncCallback callback, object asyncState)
        {               
            object[] _args = new object[1];
            _args[0] = memberInformation;
            System.IAsyncResult _result = base.BeginInvoke("RegisterUser", _args, callback, asyncState);
            return _result;               
        }
        /// 
        /// Result from RegisterUser
        /// 
        /// 
        /// 
        public MyDataEntities.MembershipInformation EndRegisterUser(System.IAsyncResult result)
        {
            try
            {
                object[] _args = new object[0];
                MyDataEntities.MembershipInformation _result = ((MyDataEntities.MembershipInformation)(base.EndInvoke("RegisterUser", _args, result)));
                return _result;
            }
             catch (Exception ex)
            {
                MyDataEntities.MembershipInformation _result = new MyDataEntities.MembershipInformation();
                _result.ValidationInformation.HasErrors = true;
                _result.ValidationInformation.Message = ex.Message;
                return _result;
            }
        }
    }
    

提交回复
热议问题