Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
274 views
in Technique[技术] by (71.8m points)

.net core - WCF: The remote server returned an error: "(417) Expectation Failed"

We're trying to connect from a Backendservice made with NET Core 3.1 to a legacy WCF Service. For this we use the two NuGet packages "System.ServiceModel.Http" and "System.ServiceModel.Primitives" (latest version 4.8.1).

For the connection to the endpoint we create the binding/serviceclient manual as follows

var binding = new BasicHttpsBinding();

binding.Security.Mode = BasicHttpsSecurityMode.Transport;
binding.Security.Transport = new HttpTransportSecurity() { ClientCredentialType = HttpClientCredentialType.Certificate, ProxyCredentialType = HttpProxyCredentialType.Basic };
binding.CloseTimeout = new TimeSpan(0, 12, 0, 0, 0);
binding.OpenTimeout = new TimeSpan(0, 12, 0, 0, 0);
binding.SendTimeout = new TimeSpan(0, 12, 0, 0, 0);
binding.MaxReceivedMessageSize = 2147483646;
binding.MaxBufferSize = 2147483646;

var channelfactory = new SearchServiceClient(binding, endpoint);
channelfactory.ClientCredentials.ClientCertificate.SetCertificate(System.Security.Cryptography.X509Certificates.StoreLocation.CurrentUser, System.Security.Cryptography.X509Certificates.StoreName.My, System.Security.Cryptography.X509Certificates.X509FindType.FindBySubjectName, "CERTIFICATENAME");

var response = await channelfactory.existsAsync(...);

In order to establish the connection we need to use an SSL client certificate, not a username/password.

With each request we receive the following error message: "The remote server returned an error: (417) Expectation Failed".

I did some research and found out that all you need to do is set the "ExpectContinue" flag to false.

Unfortunately this does not work via the ServicePointManager (not even in web.config).

ServicePointManager.Expect100Continue = false;

if I downgrade the nuget package version of "System.ServiceModel.Http" and "System.ServiceModel.Primitives" to 4.4.4, it works.

Does anyone know how I can do this with the latest version ?

question from:https://stackoverflow.com/questions/65940536/wcf-the-remote-server-returned-an-error-417-expectation-failed

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

After some weeks of research I could now find a solution.

  1. create a custom MessageHandlerBehavior and DelegatingHandler (please look at https://justsimplycode.com/2019/11/02/disable-header-100-continue-in-net-core-wcf-client/)

  2. use the custom MessageHandlerBehavior and set ExpectContinue to false:

    var handlerFactoryBehavior = new HttpMessageHandlerBehavior();
    handlerFactoryBehavior.OnSending = (message, token) =>
    {
       message.Headers.ExpectContinue = false;
       return null;
    };
    
    var channelfactory = new SearchServiceClient(binding, endpoint);
    channelfactory.ClientCredentials.ClientCertificate.SetCertificate(...);
    channelfactory.Endpoint.EndpointBehaviors.Add(handlerFactoryBehavior);
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...