开发者

Properway to encapsulate Asynchronous Web Service Communications

开发者 https://www.devze.com 2023-01-08 20:17 出处:网络
I usually try to encapsulate my web service calls in my client side apps. Rather than doing this: public Guid FindUserIDSelected(string userName)

I usually try to encapsulate my web service calls in my client side apps.

Rather than doing this:

public Guid FindUserIDSelected(string userName)
{
    MyWebServiceReference service = new MyWebServiceReference(GetEndpointBasedOnEnv(Env));
    return service.GetUserIDFromName(userName);
}

I have a static class that encapsulates the communication with the web services. It handles the endpoint resolution via determining the environment (and other such things).

So the above code changes to look like this:

public Guid FindUserIDSelected(string userName)
{
    return Communication.GetUserIDFromName(userName);
}

But now I am having an issue. Silverlight only supports Asynchronous calls (at least as far as I am seeing). So calling a web se开发者_JS百科rvice and then returning the value in an encapsulated call does not work.

The best I can come up with is passing in a delegate that is used in the Communication class for the completed event:

private Guid foundUserID;

public void FindUserIDSelected(string userName)
{
    Communication.GetUserIDFromName(userName, GetUserIDCompleted);
}

private void QuestionRecieved(object sender, GetUserIDFromNameCompletedEventArgs e)
{
    foundUserID= e.Result();
}

This has several problems (in my opinion).

  1. I now have elements of the web services that have broken encapsulation (the completed call is really the web service return. I don't want the rest of my classes to have to care about the services).
  2. I have had to expose my result (foundUserID) at the class level.

Am I being too rigid? Is that good enough? Is there a better way?

Am I the only one who has this issue?


In my opinion, it'd better to use eventing from your communication class, especially if you have some thing like [EventAggregator]1, so you can filter an event based on your specific argument

Below is the code snippet, this may be helpful for you.

public static class Communication {

    public static event EventHandler<MyEventArgs> ServiceCallComplete;

    public static void InvokeMyAcionComplete(MyEventArgs e)
    {
        EventHandler<MyEventArgs> handler = ServiceCallComplete;
        if (handler != null) handler(null, e);
    }

    public static void CallService ()
    {
        //Performed async call  

        // Fire the event to notify listeners
        OnServiceCalled();
    }

    private static void OnServiceCalled ()
    {

        InvokeMyAcionComplete(new MyEventArgs());

    }



}

public class  ClientCode
{
       public void CallService()
       {
           Communication.CallService();

           //Subscribe to the event and get notified when the call is complete
           Communication.ServiceCallComplete += OnServiceCalled;
       }

    private void OnServiceCalled(object sender, MyEventArgs e)
    {
            //MyEventArgs is your customized event argument

            //Do something with the result  
    }
}

Hope this help

0

精彩评论

暂无评论...
验证码 换一张
取 消