简体   繁体   English

WCF / wsHttpBinding / 消息安全 - BadTokenRequest

[英]WCF / wsHttpBinding / Message Security - BadTokenRequest

So got an older WCF service / client I'm working on.所以有了我正在处理的旧 WCF 服务/客户端。 Added a new (static) logging system to it, actually and now doing some load testing.添加了一个新的(静态)日志系统,实际上现在正在做一些负载测试。

Getting some really annoying sporadic issues now - claiming "Secure channel cannot be opened because security negotiation with the remote endpoint has failed".现在遇到一些非常烦人的零星问题 - 声称“无法打开安全通道,因为与远程端点的安全协商失败”。 I noticed I get a CommunicationException with a fault name of Sender and subcode of BadContextToken.我注意到我收到了一个故障名称为 Sender 和子代码为 BadContextToken 的 CommunicationException。

Weird thing is, I'll get 2-4 correct responses, then a flurry of these exceptions, then start getting good responses again.奇怪的是,我会得到 2-4 个正确的回答,然后是一连串这些异常,然后又开始得到好的回答。

This is my first real foray into WCF, and not loving it so far :)这是我第一次真正涉足 WCF,到目前为止还不喜欢它:)

Service web.config:服务 web.config:

<system.serviceModel>
  <bindings>
    <wsHttpBinding>
      <security mode="Message">
        <message clientCredentialType="UserName" />
      </security>
    </wsHttpBinding>
  </bindings>
  <services>
    <service behaviorConfiguration="ServiceBehavior" name="MyNamespace.MyService">
      <endpoint address="" binding="wsHttpBinding" contract="MyNamespace.IMyService" bindingConfiguration="wsMessage">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>
    </service>
  </services>
  <behaviors>
    <serviceBehaviors>
      <behavior name="ServiceBehavior">
        <serviceMetadata httpGetEnabled="false" />
        <serviceCredentials>
          <serviceCertificate findValue="MyValue" storeLocation="LocalMachine" storeName="My" x509FindType="FindBySubjectName" />
          <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="MyNamespace.UserNamePassValidator, MyNamespace" />
        </serviceCredentials>
      </behavior>
    </serviceBehaviors>
  </behaviors>
</system.serviceModel>

And on the client side, the client is instantiated as such:在客户端,客户端被实例化如下:

var binding = new WSHttpBinding();
binding.Name = "WSHttpBinding_IMyService";
binding.Security.Mode = SecurityMode.Message;
binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;

var client = new MyService(binding, "http://myserver:8080/myapp/service.svc");

var endpointIdentity = new DnsEndpointIdentity("MyValue"); // Match the certificate name used by the server

client.Endpoint.Address = new EndpointAddress(new Uri("http://myserver:8080/myapp/service.svc"), endpointIdentity, client.Endpoint.Address.Headers);

var creds = client.ClientCredentials;

creds.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
creds.UserName.UserName = "myuser";
creds.UserName.Password = "mypassword";

string retVal = client.SendRequest(); // SendRequest == one of the methods on my IMyService, returns a string.  This is also where I sporadically see my error when load testing.

I would appreciate any pointers to help me out with this WCF setup!我很感激任何能帮助我解决这个 WCF 设置的指针!

These might be useful additions to your web.config:这些可能对您的 web.config 有用:

<behaviors> 
    <serviceBehaviors> 
        <behavior name="CalculatorServiceBehavior">
            <serviceDebug includeExceptionDetailInFaults="False" /> 
            <serviceMetadata httpGetEnabled="True"/> 
            <serviceThrottling maxConcurrentCalls="20" maxConcurrentInstances="100"/> 
        </behavior>
     </serviceBehaviors>
</behaviors>

<binding name="basicHttp" allowCookies="true" maxReceivedMessageSize="1048576" maxBufferSize="1048576" maxBufferPoolSize="1048576">
    <readerQuotas maxDepth="32" maxArrayLength="1048576" maxStringContentLength="1048576"/>
</binding>

Usually this kind of "random" behaviour might depend on:通常这种“随机”行为可能取决于:

  1. Timeouts (probably not your case, since you'd get a different exception)超时(可能不是你的情况,因为你会得到一个不同的例外)
  2. Too many connections: if you client opens too many connections (and "forgets" to close them), you'll exceed the default allowed maximum (depending on context, it might be 10 connections).连接太多:如果您的客户端打开太多连接(并且“忘记”关闭它们),您将超过默认允许的最大值(取决于上下文,可能是 10 个连接)。 You can act on this if you alter your web.config, editing maxConcurrentCalls and maxConcurrentInstances如果您更改 web.config,编辑maxConcurrentCallsmaxConcurrentInstances ,您可以对此采取行动
  3. Perhaps those errors are not random, but specific to some message;也许这些错误不是随机的,而是特定于某些消息的; if so, that might be due to its size (ie it's too large): again, alter your web.config setting maxReceivedMessageSize , maxBufferSize , maxBufferPoolSize and readerQuotas如果是这样,那可能是由于它的大小(即它太大):再次更改您的 web.config 设置maxReceivedMessageSizemaxBufferSizemaxBufferPoolSizereaderQuotas

Of course you will get more info if you turn on WCF tracing .当然,如果您打开WCF 跟踪,您将获得更多信息。

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

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