New Posts New Posts RSS Feed: using cocktail for calling a svc service
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

using cocktail for calling a svc service

 Post Reply Post Reply
Author
pponzano View Drop Down
Senior Member
Senior Member
Avatar

Joined: 28-Apr-2011
Location: Italy
Posts: 165
Post Options Post Options   Quote pponzano Quote  Post ReplyReply Direct Link To This Post Topic: using cocktail for calling a svc service
    Posted: 03-Oct-2012 at 12:24am
Hello,
I need to call from my SL application a webservice (.svc) can I do it using a repository and OperationResult or it's just for retrieving entities from an ideablade's edmx?

Thanks
Back to Top
mgood View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
Post Options Post Options   Quote mgood Quote  Post ReplyReply Direct Link To This Post Posted: 03-Oct-2012 at 5:47am
Yes, you can. You'll have to subclass OperationResult or OperationResult<T> for your web service call.
Back to Top
pponzano View Drop Down
Senior Member
Senior Member
Avatar

Joined: 28-Apr-2011
Location: Italy
Posts: 165
Post Options Post Options   Quote pponzano Quote  Post ReplyReply Direct Link To This Post Posted: 03-Oct-2012 at 5:50am
Can you please explain me where to start looking at? in the repository I'll make a call to my svc service?
Back to Top
mgood View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
Post Options Post Options   Quote mgood Quote  Post ReplyReply Direct Link To This Post Posted: 03-Oct-2012 at 7:15am
Here's an untested example for how you would call the following service method.
    [ServiceContract]
    public interface IMyService
    {
        [OperationContract]
        bool DoWork();
    }
 
-----------
    public class Repository
    {
        public OperationResult<bool> DoWork()
        {
            var wrapper = new ServiceWrapper();
            wrapper.DoWork();
            return new ServiceOperationResult(wrapper);
        }
    }
 
    public class ServiceWrapper : INotifyCompletedINotifyCompletedArgs
    {
        private DoWorkCompletedEventArgs _args;
        private Action<INotifyCompletedArgs> _notifyActions;
        private readonly MyServiceClient _service;
 
        public ServiceWrapper()
        {
            _service = new MyServiceClient();    
        }
 
        /// <summary>
        /// Action to be performed when the asynchronous operation completes.
        /// </summary>
        /// <param name="completedAction"/>
        public void WhenCompleted(Action<INotifyCompletedArgs> completedAction)
        {
            if (completedAction == nullreturn;
            if (_args != null)

            {
                completedAction(this);
                return;
            }
            _notifyActions = (Action<INotifyCompletedArgs>)Delegate.Combine(_notifyActions, completedAction);
        }
 
        public void DoWork()
        {
            _service.DoWorkCompleted += ServiceOnDoWorkCompleted;
            _service.DoWorkAsync();
        }
 
        private void ServiceOnDoWorkCompleted(object sender, DoWorkCompletedEventArgs args)
        {
            _args = args;
            _service.DoWorkCompleted -= ServiceOnDoWorkCompleted;
            _service.CloseAsync();
 
            // Notify subscribers
            var actions = _notifyActions;
            _notifyActions = null;
            if (actions == nullreturn;
            actions(this);
        }
 
        public bool Result
        {
            get { return _args != null && _args.Result; }
        }
 
        /// <summary>
        /// The exception if the action failed.
        /// </summary>
        public Exception Error { get { return _args != null ? _args.Error : null; } }
 
        /// <summary>
        /// Whether the action was cancelled.
        /// </summary>
        public bool Cancelled { get { return _args != null && _args.Cancelled; } }
 
        /// <summary>
        /// Whether the error was handled.
        /// </summary>
        public bool IsErrorHandled { getset; }
    }
 
    public class ServiceOperationResult : OperationResult<bool>
    {
        private readonly ServiceWrapper _wrapper;
 
        public ServiceOperationResult(ServiceWrapper wrapper)
            : base(wrapper)
        {
            _wrapper = wrapper;
        }
 
        /// <summary>
        /// The result value of the operation.
        /// </summary>
        public override bool Result
        {
            get { return _wrapper.Result; }
        }
    }
Back to Top
pponzano View Drop Down
Senior Member
Senior Member
Avatar

Joined: 28-Apr-2011
Location: Italy
Posts: 165
Post Options Post Options   Quote pponzano Quote  Post ReplyReply Direct Link To This Post Posted: 09-Oct-2012 at 10:05am
Worked! Thanks a lot!
Back to Top
pponzano View Drop Down
Senior Member
Senior Member
Avatar

Joined: 28-Apr-2011
Location: Italy
Posts: 165
Post Options Post Options   Quote pponzano Quote  Post ReplyReply Direct Link To This Post Posted: 17-Oct-2012 at 5:30am
Marcel,
please excuse me in your example you refer to a specific

   private DoWorkCompletedEventArgs _args;

I've tested with

System.ComponentModel.AsyncCompletedEventArgs _args;

and it works (I've also replaced the

   public bool Result
        {
            get { return _args != null && _args.Result; }
        }

with

public object Result...

in that way I've to cast the operation.Result to the specific type I need, is there a way for returning the type?
I mean using the generics for the public T Result


Back to Top
mgood View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
Post Options Post Options   Quote mgood Quote  Post ReplyReply Direct Link To This Post Posted: 17-Oct-2012 at 9:04am
I'm not sure about what you are trying to do. In my example I wrap a service method that returns a bool, so therefore I use the corresponding completed event that gives me the typed result. 

Are you trying to write a generic wrapper that can call any service method?
Back to Top
pponzano View Drop Down
Senior Member
Senior Member
Avatar

Joined: 28-Apr-2011
Location: Italy
Posts: 165
Post Options Post Options   Quote pponzano Quote  Post ReplyReply Direct Link To This Post Posted: 18-Oct-2012 at 12:12am
If possible yes... since If I've just 3 different methods that returns different types I've to write Result1,2,3...
Back to Top
mgood View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
Post Options Post Options   Quote mgood Quote  Post ReplyReply Direct Link To This Post Posted: 18-Oct-2012 at 12:31am

Sure you can turn the ServiceWrapper into ServiceWrapper<T> and then the Result property would look something like this.

 
        public T Result
        {
            get { return _args != null ? (T)_args.Result : default(T); }
        }
Back to Top
pponzano View Drop Down
Senior Member
Senior Member
Avatar

Joined: 28-Apr-2011
Location: Italy
Posts: 165
Post Options Post Options   Quote pponzano Quote  Post ReplyReply Direct Link To This Post Posted: 30-Jan-2013 at 3:28am
Excuse me Marcel,
how do I have to write this using Tasks? I don't have to inherits from 
OperationResult anymore ? right??

Thanks
Back to Top
mgood View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
Post Options Post Options   Quote mgood Quote  Post ReplyReply Direct Link To This Post Posted: 30-Jan-2013 at 6:21am
public Task<bool> DoWorkAsync()
{
      return Task.Factory.StartNew(() =>
             {
                    using (service = new MyServiceClient())
                    {
                          return service.DoWork();
                    }
             });
}
Back to Top
pponzano View Drop Down
Senior Member
Senior Member
Avatar

Joined: 28-Apr-2011
Location: Italy
Posts: 165
Post Options Post Options   Quote pponzano Quote  Post ReplyReply Direct Link To This Post Posted: 31-Jan-2013 at 7:49am
Hello Marcel,
the sample you provided to me is for the repository...
how should I write the wrapper in order to subsitute the code of the Wrapper for the DoWork?

I mean

   public void WhenCompleted(Action<INotifyCompletedArgs> completedAction)
       {
           if (completedAction == null) return;
           if (_args != null)
           {
               completedAction(this);
               return;
           }
           _notifyActions = (Action<INotifyCompletedArgs>)Delegate.Combine(_notifyActions, completedAction);
       }

       public void DoWork()
       {
           _service.DoWorkCompleted += ServiceOnDoWorkCompleted;
           _service.DoWorkAsync();
       }

       private void ServiceOnDoWorkCompleted(object sender, DoWorkCompletedEventArgs args)
       {
           _args = args;
           _service.DoWorkCompleted -= ServiceOnDoWorkCompleted;
           _service.CloseAsync();

           // Notify subscribers
           var actions = _notifyActions;
           _notifyActions = null;
           if (actions == null) return;
           actions(this);
       }

       public bool Result
       {
           get { return _args != null && _args.Result; }
       }

Thanks
Paolo
Back to Top
mgood View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
Post Options Post Options   Quote mgood Quote  Post ReplyReply Direct Link To This Post Posted: 31-Jan-2013 at 10:02am
My bad. Ignore what I gave you. I had WPF in my head at the time. In Silverlight you can use TaskCompletionSource in place of the wrapper.

http://msdn.microsoft.com/en-us/library/dd449174(v=vs.95).aspx
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down