【问题标题】:Configuring the timeout for a WCF RIA Services call from a Silverlight 3 client为 Silverlight 3 客户端的 WCF RIA 服务调用配置超时
【发布时间】:2009-12-16 06:44:04
【问题描述】:

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

有没有一种简单的方法来控制这个和其他 WCF 设置?

我的第一个想法是尝试在 RIA 服务概述 pdf 文件中提到的DomainContext OnCreated 挂钩点,该文件在 RIA 服务进入测试版之前可用。 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
        }
    }
}

【问题讨论】:

    标签: wcf silverlight wcf-ria-services


    【解决方案1】:

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

    【讨论】:

    • 我不再在这个代码库上工作,但很高兴知道他们最终暴露了这个。当时我正在使用 Beta 版。
    • 顺便说一句,这不适用于 RTM,或者至少我无法弄清楚如何。
    • 这里是使用 silverlight 4 的更新:blogs.msdn.com/b/kylemc/archive/2010/11/03/…
    【解决方案2】:

    作为参考,下面的代码几乎可以工作,但您无法在 Silverlight 中使用反射访问私有成员。无论如何,我不会对这个 hack 感到满意。有趣的是,有一个 WebDomainClient 构造函数采用 Binding 参数private WebDomainClient(Uri serviceUri, bool usesHttps, Binding binding),但它的 XML 注释声明 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);
        }
    }
    

    【讨论】:

    • 如何通过 web.config 进行配置?
    猜你喜欢
    • 1970-01-01
    • 2011-06-21
    • 2012-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-14
    • 2013-05-12
    • 1970-01-01
    相关资源
    最近更新 更多