在调试模式下以编程方式设置WCF超时 [英] Programmatically set WCF timeout in debug mode

查看:98
本文介绍了在调试模式下以编程方式设置WCF超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在服务器和客户端(都是用C#编写)之间的通信中使用WCF。

I'm using WCF in communication between a server and client (both written in C#).

在发布模式下,应将timouts设置为〜20秒,但在调试模式下,我想将其设置为更高的值,以便可以调试/步进在我的代码中不会发生超时。

In release-mode, the timouts should be set to ~20 seconds, but in debug mode I want to set them to a higher value so that I can debug/step in my code without the timeout occurring.

我知道我可以通过修改app.config文件来更改超时。但是,我有两个不同的绑定,每个绑定都有4个超时值,因此我必须在几个地方进行更改,并且它很容易忘记。

I know that I can change the timeouts by modifying the app.config file. However, I've got two different bindings and 4 time out values in each so I would have to change in several places, and its easy to forget.

要解决这个问题,我想在代码中有一个小的#if DEBUG部分,以编程方式将超时值更改为1小时。

To solve this, I would like to have a small #if DEBUG-section in my code which programmatically changes the timeout values to 1 hour.

我尝试使用以下代码进行操作

I tried to use the following code to do this:

Configuration configuration = 
       ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ServiceModelSectionGroup serviceModel = 
       ServiceModelSectionGroup.GetSectionGroup(configuration); 

BindingsSection bindings = serviceModel.Bindings;

foreach (var configuredBinding in bindings.WSHttpBinding.ConfiguredBindings)
{
 configuredBinding.CloseTimeout = new TimeSpan(0, 30, 0);
 configuredBinding.OpenTimeout = new TimeSpan(0, 30, 0);

但是* Timeout属性是只读的,因此我收到编译错误。

but the *Timeout properties are readonly so I get a compilation error.

我不喜欢以编程方式从头开始创建绑定的想法。如果我更改了app.config中的某些属性,则必须记住在代码中进行相同的更改,以确保调试行为与发布行为相似(超时除外)。

I'm not fond of the idea of creating bindings from scratch programmatically. If I change some of the attributes in the app.config, I have to remember to do the same change in the code to make sure that the debug-behavior is similar to the release-behavior (except for the timeouts..)

如何处理?

推荐答案

您可以执行以下操作:


  • 创建绑定,并在代码中使用端点

  • 设置绑定实例的超时时间

  • 然后使用这两个元素创建您的客户端代理

类似于:

BasicHttpBinding myBinding = new BasicHttpBinding("ConfigName");
myBinding.CloseTimeout = .......
myBinding.OpenTimeout = .......
myBinding.ReceiveTimeout = .......
myBinding.SendTimeout = .......

EndpointAddress myEndpoint = new EndpointAddress("http://server:8181/yourservice");

YourServiceClient proxy = new YourServiceClient(myBinding, myEndpoint);

这样,您可以在描述绑定超时时利用基本配置,但可以调整设置想要从中创建客户端代理。

That way, you can leverage the basic config when describing binding timeouts and yet you can tweak the settings you want and create your client proxy from it.

这篇关于在调试模式下以编程方式设置WCF超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆