繁体   English   中英

正确处理期望失败(417),并在WCF客户端应用程序中更改Expect100Continue

[英]Correcty handle Expectation failed (417) and change Expect100Continue in WCF client app

我的应用程序是使用WCF服务的C#Windows服务。 当出现第一个“预期失败(417)”错误时,我将ServicePointManager.Expect100ContinueServicePoint.Expect100Continue都更改为false

try
{
    //ServicePointManager.Expect100Continue = false; // If uncomment all work
    var svc = new ServiceClient();
    svc.GetData(); // first error
}
catch (ProtocolException pex)
{
    if (pex.Message.Contains("(417)"))
    {
        ServicePointManager.Expect100Continue = false;
        var sp = ServicePointManager.FindServicePoint(new Uri(@"http://addr.to.service/service.svc"));
        sp.Expect100Continue = false;

        var svc = new ServiceClient();
        svc.GetData(); // second same error
    }
}

但是,第二次调用该服务也失败。 但是,如果在进行任何连接之前将Expect100Continue设置为false ,则与该服务的通信将正常进行。

这样正确地处理Expect100Continue错误吗? 我需要应用程序自动适应而无需用户干预。 我忘了做这项工作是什么?

ServicePointManager上的大多数设置都被视为应用到应用程序生命周期中在该点之后创建的所有新ServicePoints上应用的默认值。 在看到错误后更改设置的情况下,实际上并没有更改现有ServicePoint实例上的任何内容,包括与WCF在这种情况下使用的连接关联的实例。

在示例代码中,您正在调用ServicePointManager.FindServicePoint以尝试找到正确的ServicePoint。 但是, FindServicePoint具有多个重载,并且很容易错误地使用该API。 例如, FindServicePoint将尝试考虑http / https,要连接的主机,代理配置等问题。如果没有为FindServicePoint提供正确的参数,则很容易导致错误的ServicePoint返回。您和您的设置将不会应用于您要更改的ServicePoint

我建议您使用带有IWebProxy objectFindServicePoint重载,以确保获得正确的ServicePoint 在大多数情况下,您应该能够将WebRequest.DefaultWebProxy作为IWebProxy对象传递。

从ServicePointManager.Expect100Continue的MSDN文档中,

更改此属性的值不会影响现有的ServicePoint对象。 仅更改后创建的新ServicePoint对象。 因此,更改现有WCF客户端上的值将无效。 您需要创建一个新的WCF客户端,然后调用GetData()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM