我有这样的背景:
[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 )
我会很高兴改变发送超时,但我不知道,怎么做。我试过这个:
((WebDomainClient<LibraryDomainContext.ILibraryDomainServiceContract>)this.DomainClient).ChannelFactory.Endpoint.Binding.SendTimeout = new TimeSpan(0, 5, 0);但我没有财产DomainClient。
发布于 2013-05-21 12:04:41
可以在域服务端点的客户端设置连接的超时。但如何才能抓住它呢?通过为域上下文创建扩展方法:
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()也是这样,可以很容易地进行扩展。
public partial class MyDomainContext
{
partial void OnCreated()
{
this.ChangeTimeout(new TimeSpan(0,10,0));
}
}Pro :在实现部分类时,类的所有部分的命名空间必须是相同的。这里列出的代码属于客户端项目(例如,使用名称空间RIAServicesExample),但是上面显示的部分类仍然需要驻留在服务器端名称空间中(例如,RIAServicesExample.Web)。
https://stackoverflow.com/questions/16606387
复制相似问题