views:

2074

answers:

2

When using a Silverlight-enabled WCF service, where is the best place to instantiate the service and to call the CloseAsync() method?

Should you say, instantiate an instance each time you need to make a call to the service, or is it better to just instantiate an instance as a variable of the UserControl that will be making the calls?

Then, where is it better to call the CloseAsync method? Should you call it in each of the "someServiceCall_completed" event methods? Or, if created as a variable of the UserControl class, is there a single place to call it? Like a Dispose method, or something equivalent for the UserControl class.

Thanks,

Jeff

+2  A: 

You're better off just having an instance variable for the service. Creating and destroying the service for each call creates a lot of unnecessary overhead. Just create the variable and call the methods, no need to open it since this will be done automatically as of beta 2 (see section #5).

As for close, whether you try to close it for clean up probably depends on how your app is structured. If when the UserControl is closed the whole app is shutting down (the user closed the browser) then you probably don't need to explicitly close it since everything will get cleaned up when the Silverlight host closes. However, if you're creating lots of these user control and closing them while keeping the app open then you might want to create some kind of close method on your control that would clean up by calling CloseAsync.

If all the user controls use the same service, then you could just create a single service wrapper class that is used by all the controls that would handle calling the service. This would keep you from having to close the services when the controls unload as well.

Bryant
What about when 2 controls in seperate parts of your silverlight application, let's say seperate tabs, both subscribe to the same CompletedEvent and one control invokes the ....Async() call which results in BOTH handlers being invoked, even though only one was meant to be invoked?
TJB
A: 

In the case of 2 parallel event handlers in your SL client, you can do the following approach to make sure only one gets invoked: Assume, we have a global client variable, App.Client, which is being used by everything in the app. Now, control 1 needs to react on MyOperationCompleted, as does control 2. Each control uses the eventhandler like this:

...
{
    App.Client.MyOperationCompleted += Client_MyOperationCompleted;
    App.Client.MyOperationAsync(...);
}

void Client_MyOperationCompleted(object sender, MyOperationCompletedEventArgs e)
{
    App.Client.MyOperationCompleted -= Client_MyOperationCompleted;
}

So if you subscribe to the event in one case, as soon as it returns, you remove the subscription to this event. If if you always stick to this, it's quite unlikely (however not impossible) that other controls react to the event. Note that this approach is not 100% concurrency safe. I'm still trying to come up with a really safe method for doing this. But it sure helps.

RoastedBattleSquirrel