Configuring the timeout for a WCF RIA Services call from a Silverlight 3 client

10,167

Solution 1

http://blogs.objectsharp.com/CS/blogs/dan/archive/2010/03/22/changing-timeouts-in-wcf-ria-services-rc.aspx

Either one line after domain context creation:

((WebDomainClient<LibraryDomainContext.ILibraryDomainServiceContract>)this.DomainClient).ChannelFactory.Endpoint.Binding.SendTimeout = new TimeSpan(0, 5, 0);

or a partial class

public partial class LibraryDomainContext
{
   partial void OnCreated()
   {
      if(DesignerProperties.GetIsInDesignMode(App.Current.RootVisual))
         ((WebDomainClient<LibraryDomainContext.ILibraryDomainServiceContract>)this.DomainClient).ChannelFactory.Endpoint.Binding.SendTimeout = new TimeSpan(0, 5, 0);
   }
}

Solution 2

For reference the code below nearly works but you can't access a private member using reflection in Silverlight. Wouldn't have been happy with this hack though anyway. Interesting to note that there is a WebDomainClient contructor that takes a Binding parameter private WebDomainClient(Uri serviceUri, bool usesHttps, Binding binding) but the XML Comment for this states Private constructor. Should be made public once we have an end-to-end extensibility story on top of WCF. Looks like I'll have to wait a while before they get to exposing this kind of configuration to us.

public sealed partial class AppDomainContext
{
    partial void OnCreated()
    {
        var webDomainClient = ((WebDomainClient<AppDomainContext.IAppDomainServiceContract>)this.DomainClient);
        // Can I use reflection here to get hold of the Binding
        var bindingField = webDomainClient.GetType().GetField("_binding", BindingFlags.NonPublic | BindingFlags.Instance);

        // In Silverlight, the value of a private field cannot be access by using reflection so the GetValue call throws an exception
        // http://msdn.microsoft.com/en-us/library/4ek9c21e%28VS.95%29.aspx
        var binding = bindingField.GetValue(webDomainClient) as System.ServiceModel.Channels.Binding;

        // So near yet so far!!
        binding.SendTimeout = new TimeSpan(0,0,1);
    }
}
Share:
10,167
ANISH S NAIR
Author by

ANISH S NAIR

Full stack (code and infrastructure) Tech Lead

Updated on June 05, 2022

Comments

  • ANISH S NAIR
    ANISH S NAIR almost 2 years

    I'm using the WCF RIA Services Beta with Silverlight 3.0 and I want to be able to configure the timeout from the client. I know that the underlying technology is WCF and the default timeout seems to be 60 seconds as I would expect.

    Is there an easy way to control this and other WCF settings?

    My first thought is to try the DomainContext OnCreated hook point which was mentioned in the RIA Services Overview pdf file that was available prior to RIA Services going beta. The MSDN documentation for the DomainContext object no longer mentions the method although it is still there? I'm not sure if this is a case of the documentation lagging behind or an indication that I shouldn't use this extensibility point.

    namespace Example.UI.Web.Services
    {
        public sealed partial class CustomDomainContext
        {
            partial void OnCreated()
            {
                // Try and get hold of the WCF config from here
            }
        }
    }
    
  • ANISH S NAIR
    ANISH S NAIR almost 14 years
    I'm not working on this code base any longer but it's good to know they exposed this eventually. I was working with the Beta at the time.
  • Rodney S. Foley
    Rodney S. Foley over 13 years
    BTW This doesn't work with RTM, or at least I couldn't figure out how.
  • Jonx
    Jonx over 13 years
    Here is an update working with silverlight 4: blogs.msdn.com/b/kylemc/archive/2010/11/03/…
  • NoWar
    NoWar over 11 years
    How do we can configure it via web.config?