Pergunta

I'm a little new to Silverlight, and I want to know how to deal with the Faulted/Disposing of a WCF service.

I'm used to something like this (wcf abort/close pattern) where you call the service in a try/catch (making sure you close or abort). (which works well in a stateless application)

looking into Silverlight, where do we apply the abort/close pattern? as the service call is async and the application is state full.

At the moment the only thing I can think of is some sort of dynamic proxy (using something like Castle DP) accompanied with the ChannelFactoryManager from the n-tier app, about 1/2 way down the page example. where the proxy will ensure there is always an open channel and the ChannelFactoryManager will handle the faults

Foi útil?

Solução 2

Solution using Castle DP with the ChannelFactoryManager implemented and detailed here:

http://www.codeproject.com/Articles/502121/WCF-in-a-stateful-application-WPF-Silverlight

Outras dicas

Because of the asynchronous nature of the Silverlight networking environment I recommend you to build more testable ServiceAgents - long-living singleton wrappers around silverlight's client proxies with callbacks for service methods. You can check real-proxy state (& recreate if needed) before calling service methods or use channel Faulted event. For ex:

public void GetOptionsAsync(Action<GetOptionsCompletedEventArgs> callback)
{
    try
    {
        CheckProxy();

        EventHandler<GetOptionsCompletedEventArgs> handler = null;

        handler = (sender, args) =>
        {
           Proxy.GetOptionsCompleted -= handler;
           if (args.Error != null)
           {
               //...
           }
           if (callback != null)
           {
               callback(args);
           }
        };

        Proxy.GetOptionsCompleted += handler;

        Proxy.GetOptionsAsync();
    }
    catch (Exception unknownException)
    {
       //...
       throw;
    }
}

public override void ResetProxy() //AbortProxy/CloseProxy
{
    if (Proxy != null)
    {
        try
        {
            Proxy.CloseProxy(); //extension method to handle exception while closing
        }
        catch (Exception unknownException) //CommunicationObjectFaultedException
        {
            //...
            Proxy.Abort();
        }               
    }

    CreateProxy();          
}

public override void CheckProxy()
{
    if (Proxy == null || (Proxy.State != CommunicationState.Opened && Proxy.State != CommunicationState.Created))
    {               
        ResetProxy();
    }
}

public override void CreateProxy() //RecreateProxy
{           
     Proxy = new WcfClient();

     Proxy.InnerChannel.Faulted += OnChannelFaulted;
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top