在调试模式下以编程方式设置 WCF 超时

发布于 2024-08-13 10:09:06 字数 924 浏览 1 评论 0原文

我在服务器和客户端(均用 C# 编写)之间进行通信时使用 WCF。

在发布模式下,超时应设置为 ~20 秒,但在调试模式下,我想将它们设置为更高的值,以便我可以调试/单步执行代码而不会发生超时。

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

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

我尝试使用以下代码来执行此操作:

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 属性是只读的,因此出现编译错误。

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

)处理这个?

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

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.

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.

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

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

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..)

How to handle this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逆流 2024-08-20 10:09:06

您可以执行以下操作:

  • 在代码中创建绑定和端点,
  • 在绑定实例上设置超时
  • ,然后使用这两个元素创建客户端代理

类似:

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

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

You could do the following:

  • create the binding and the endpoint in code
  • set the timeouts on the binding instance
  • then create your client proxy using those two elements

Something like:

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.

裂开嘴轻声笑有多痛 2024-08-20 10:09:06

您可以在 web.config 中创建第二个绑定并设置更长的 sendTimeout。

        if (debug)
        {
            proxy =  new MyClient("WSHttpBinding_MyLocal");
        }
        else
        {
            proxy = new MyClient("WSHttpBinding_MyDev");
        }

        <wsHttpBinding>
            <binding name="WSHttpBinding_MyLocal" closeTimeout="00:01:00"
                openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:20:00"

...

You can create a second binding in the web.config and set a longer sendTimeout.

        if (debug)
        {
            proxy =  new MyClient("WSHttpBinding_MyLocal");
        }
        else
        {
            proxy = new MyClient("WSHttpBinding_MyDev");
        }

        <wsHttpBinding>
            <binding name="WSHttpBinding_MyLocal" closeTimeout="00:01:00"
                openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:20:00"

...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文