属性httpRequest'不存在

bre*_*iba 3 c# wcf

我在Visual Studio外部运行构建时收到此错误:

"名称为'httpRequest'的属性不存在"

如果我在Visual Studio中运行SAME代码,它可以工作.有没有人知道我做错了什么?

我的app.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
  </startup>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="CacheServiceEndpoint" />
        <binding name="consultaWebServicePortBinding" maxBufferPoolSize="524288" maxBufferSize="10485760"
  maxReceivedMessageSize="10485760">
          <readerQuotas maxDepth="32" maxStringContentLength="10485760"
            maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <security mode="Transport" />
        </binding>
        <binding name="consultaWebServicePortBinding1" />
      </basicHttpBinding>
      <customBinding>
        <binding name="CustomBinding_ICacheService">
          <binaryMessageEncoding />
          <httpTransport />
        </binding>
      </customBinding>
    </bindings>
    <client>
      <endpoint address="http://....svc"
          binding="basicHttpBinding" bindingConfiguration="CacheServiceEndpoint"
          contract="Cache.ICacheService" name="CacheServiceEndpoint" />
      <endpoint address="http://.../binary"
          binding="customBinding" bindingConfiguration="CustomBinding_ICacheService"
          contract="Cache.ICacheService" name="CustomBinding_ICacheService" />
      <endpoint address="https://..."
          binding="basicHttpBinding" bindingConfiguration="consultaWebServicePortBinding"
          contract="ABC.consultaWebService" name="consultaWebServicePort" />
    </client>
  </system.serviceModel>
</configuration>
Run Code Online (Sandbox Code Playgroud)

我正在做什么打电话:

svc = new consultaWebServiceClient ("consultaWebServicePort");                    
svc.Endpoint.Behaviors.Add (new CustomEndpointBehavior (user, psw));
Run Code Online (Sandbox Code Playgroud)

谢谢!

小智 9

我遇到了完全相同的问题.我在MSDN论坛上找到了解决方案.我也在这里发布它,因为很难找到解决方案,我首先检查了这个Stack Overflow问题.

显然,当您在调试器中运行时,它会初始化HttpRequestMessageProperty,但是当您在常规运行时环境中运行时,此Message属性不会被初始化.我不知道其他Message属性可能会丢失,但也可能是其他属性.

因此,您可以编写一些代码来创建新代码,并将其添加到Message属性(如果它尚不存在).这似乎是安全的.我的EndpointBehavior添加的标题确实被添加并发送到服务器.

private HttpRequestMessageProperty GetHttpRequestMessageProperty(Message request)
{
    if (!request.Properties.ContainsKey(HttpRequestMessageProperty.Name))
    {
        request.Properties.Add(HttpRequestMessageProperty.Name, new HttpRequestMessageProperty());
    }

    return request.Properties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
}
Run Code Online (Sandbox Code Playgroud)

感谢SteveSteveSteveP在MSDN论坛上提供的解决方案!