Silverlight Async Template Design Problem

I am in the middle of a Silverlight application, and I have a function that should call a web service and using the result completes the rest of the function.

My problem is that I usually made a synchronous call to the web service, got the result and using it, which was executed using the function. Since Silverlight does not support synchronous web service calls without additional custom classes to simulate, I suggest that it is best to go with the async stream rather than deal with it. So my question is about what is the best design pattern for working with asynchronous calls in a program stream.

In the following example, I want to use the myFunction TypeId parameter depending on the return value of the web service call. But I do not want to call the web service until this function is called. How can I change my code to allow an asynchronous call?

        string _myPath;

    bool myFunction(Guid TypeId)
    {
        WS_WebService1.WS_WebService1SoapClient proxy = new WS_WebService1.WS_WebService1SoapClient();
        proxy.GetPathByTypeIdCompleted += new System.EventHandler<WS_WebService1.GetPathByTypeIdCompleted>(proxy_GetPathByTypeIdCompleted);
        proxy.GetPathByTypeIdAsync(TypeId);

        // Get return value

        if (myPath == "\\Server1")
        {
            //Use the TypeId parameter in here
        }
    }

    void proxy_GetPathByTypeIdCompleted(object sender, WS_WebService1.GetPathByTypeIdCompletedEventArgs e)
    {
        string server = e.Result.Server;
        myPath = '\\' + server;
    }

Thanks in advance, Mike

+3
source share
3 answers

Given the asynchronous nature of Silverlight, you cannot return values โ€‹โ€‹from myFunction. Instead, you can pass an action that takes place after the service call completes. See sample code below. I'm not sure if this is considered best practice, but I often use this โ€œtemplateโ€ and it always worked well for me.


, .

void DoSomething(Guid TypeId, Action<int, bool> Callback)
{
    WS_WebService1.WS_WebService1SoapClient proxy = new WS_WebService1.WS_WebService1SoapClient();
    proxy.GetPathByTypeIdCompleted += (s, e) =>
        {
            string server = e.Result.Server;
            myPath = '\\' + server;

            //
            if (myPath == "\\Server1")
            {
                Callback(888, true);
            }
            else
            {
                Callback(999, false);
            }
        };
    proxy.GetPathByTypeIdAsync(TypeId);

}

void CallDoSomething()
{
    DoSomething(Guid.NewGuid(), (returnValue1, returnValue2) =>
        {
            //Here you can do stuff with the returned value(s)
        });
}
+2

Reactive Extensions. ( IObservable<string> GetPathByTypeId(string typeId) WS_WebService1SoapClient, :

proxy
    .GetPathByTypeId(TypeId)
    .Subscribe(server => 
        { 
            //Here you can do stuff with the returned value
        });

:)

+3

GetPathByTypeId GetPathByTypeIdCompleted. mypath . mypath INotifyPropertyChanged, Mypath , Mypath .

  • mypath.
  • mypath
  • Mypath GetPathByTypeId
  • Mypath , notification of Observer
  • Mypath
+1

Source: https://habr.com/ru/post/1743947/


All Articles