Author |
Share Topic Topic Search Topic Options
|
pponzano
Senior Member
Joined: 28-Apr-2011
Location: Italy
Posts: 165
|
Post Options
Quote Reply
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
|
|
mgood
IdeaBlade
Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
|
Post Options
Quote Reply
Posted: 03-Oct-2012 at 5:47am |
Yes, you can. You'll have to subclass OperationResult or OperationResult<T> for your web service call.
|
|
pponzano
Senior Member
Joined: 28-Apr-2011
Location: Italy
Posts: 165
|
Post Options
Quote Reply
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?
|
|
mgood
IdeaBlade
Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
|
Post Options
Quote Reply
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 : INotifyCompleted, INotifyCompletedArgs
{
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 == 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; }
}
/// <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 { get; set; }
}
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; }
}
}
|
|
pponzano
Senior Member
Joined: 28-Apr-2011
Location: Italy
Posts: 165
|
Post Options
Quote Reply
Posted: 09-Oct-2012 at 10:05am |
Worked! Thanks a lot!
|
|
pponzano
Senior Member
Joined: 28-Apr-2011
Location: Italy
Posts: 165
|
Post Options
Quote Reply
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
|
|
mgood
IdeaBlade
Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
|
Post Options
Quote Reply
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?
|
|
pponzano
Senior Member
Joined: 28-Apr-2011
Location: Italy
Posts: 165
|
Post Options
Quote Reply
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...
|
|
mgood
IdeaBlade
Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
|
Post Options
Quote Reply
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); } }
|
|
pponzano
Senior Member
Joined: 28-Apr-2011
Location: Italy
Posts: 165
|
Post Options
Quote Reply
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
|
|
mgood
IdeaBlade
Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
|
Post Options
Quote Reply
Posted: 30-Jan-2013 at 6:21am |
public Task<bool> DoWorkAsync()
{
return Task.Factory.StartNew(() =>
{
using (service = new MyServiceClient())
{
return service.DoWork();
}
});
}
|
|
pponzano
Senior Member
Joined: 28-Apr-2011
Location: Italy
Posts: 165
|
Post Options
Quote Reply
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
|
|
mgood
IdeaBlade
Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
|
Post Options
Quote Reply
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
|
|