首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WCF RIA服务超时

WCF RIA服务超时
EN

Stack Overflow用户
提问于 2013-05-17 10:04:06
回答 1查看 2.6K关注 0票数 3

我有这样的背景:

代码语言:javascript
复制
[EnableClientAccess()]
public class MyRiaService : LinqToEntitiesDomainService<EntityFrameworkContext>

使用Silverlight客户端,我正在启动大量的数据库操作,需要超过1分钟。因此,我得到了超时异常:

未处理错误:在Silverlight应用程序中发生未处理错误:提交操作失败。对于对https://localhost/MyProject/ClientBin/myservice.svc/binary的HTTP请求,已超过分配的超时。分配给此操作的时间可能是较长超时的一部分。 堆栈跟踪: System.Windows.Ria.OperationBase.Complete(Exception错误) System.Windows.Ria.SubmitOperation.Complete(Exception错误) ( System.Windows.Ria.DomainContext.CompleteSubmitChanges(IAsyncResult asyncResult) ( System.Windows.Ria.DomainContext.<>c_DisplayClassd.b_5(Object )

我会很高兴改变发送超时,但我不知道,怎么做。我试过这个:

代码语言:javascript
复制
((WebDomainClient<LibraryDomainContext.ILibraryDomainServiceContract>)this.DomainClient).ChannelFactory.Endpoint.Binding.SendTimeout = new TimeSpan(0, 5, 0);

但我没有财产DomainClient

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-21 12:04:41

可以在域服务端点的客户端设置连接的超时。但如何才能抓住它呢?通过为域上下文创建扩展方法:

代码语言:javascript
复制
public static class DomainServiceExtensions
{
    /// <summary>
    /// This method changes the send timeout for the specified 
    /// <see cref="DomainContext"/> to the specifified <see cref="TimeSpan"/>.
    /// </summary>
    /// <param name="domainContext">
    /// The <see cref="DomainContext"/> that is to be modified.
    /// </param>
    /// <param name="newTimeout">The new timeout value.</param>
    public static void ChangeTimeout(this DomainContext domainContext, 
                                          TimeSpan newTimeout)
    {
        // Try to get the channel factory property from the domain client 
        // of the domain context. In case that this property does not exist
        // we throw an invalid operation exception.
        var channelFactoryProperty = domainContext.DomainClient.GetType().GetProperty("ChannelFactory");
        if(channelFactoryProperty == null)
        {
            throw new InvalidOperationException("The 'ChannelFactory' property on the DomainClient does not exist.");
        }

        // Now get the channel factory from the domain client and set the
        // new timeout to the binding of the service endpoint.
        var factory = (ChannelFactory)channelFactoryProperty.GetValue(domainContext.DomainClient, null);
        factory.Endpoint.Binding.SendTimeout = newTimeout;
    }
}

有趣的问题是何时将调用此方法。使用终结点后,无法再更改超时。因此,在创建域上下文之后立即设置超时:

DomainContext-class本身是sealed,但幸运的是,该类也被标记为partial --方法OnCreated()也是这样,可以很容易地进行扩展。

代码语言:javascript
复制
public partial class MyDomainContext
{
    partial void OnCreated()
    {
        this.ChangeTimeout(new TimeSpan(0,10,0));
    }
}

Pro :在实现部分类时,类的所有部分的命名空间必须是相同的。这里列出的代码属于客户端项目(例如,使用名称空间RIAServicesExample),但是上面显示的部分类仍然需要驻留在服务器端名称空间中(例如,RIAServicesExample.Web)。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16606387

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档