从Silverlight 3客户端配置WCF RIA Services调用的超时

时间:2009-12-16 06:44:04

标签: wcf silverlight wcf-ria-services

我正在使用Silverlight 3.0的WCF RIA Services Beta,我希望能够从客户端配置超时。我知道底层技术是WCF,默认超时似乎是我所期望的60秒。

是否有一种简单的方法来控制此WCF和其他WCF设置?

我首先想到的是尝试在RIA服务之前提供的 RIA服务概述 pdf文件中提到的DomainContext OnCreated 挂钩点公测。 DomainContext对象的MSDN文档不再提及该方法,尽管它仍然存在?我不确定这是文件落后的情况还是我不应该使用这个扩展点的指示。

namespace Example.UI.Web.Services
{
    public sealed partial class CustomDomainContext
    {
        partial void OnCreated()
        {
            // Try and get hold of the WCF config from here
        }
    }
}

2 个答案:

答案 0 :(得分:3)

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

创建域上下文后的任一行:

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

或部分类

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);
   }
}

答案 1 :(得分:1)

作为参考, near 下面的代码可以使用,但是您无法使用Silverlight中的反射访问私有成员。尽管如此,不会对这个 hack 感到满意。有趣的是,有一个WebDomainClient构造函数接受Binding参数private WebDomainClient(Uri serviceUri, bool usesHttps, Binding binding),但此状态的XML Comment Private构造函数。一旦我们在WCF 之上有一个端到端的可扩展性故事,就应该公开。看起来我必须等待一段时间才能向我们公开这种配置。

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);
    }
}
相关问题