使用webHttpBinding和安全模式将服务引用添加到WCF服务传输会导致配置文件不正确

时间:2010-02-18 03:41:43

标签: wcf rest webhttpbinding

我搜索过并找不到解决办法。似乎通过https运行这个... WCF REST服务是相对常见的。当我添加服务引用时,所有代理类都是正确的,我可以创建服务中定义的对象,调用服务等。但是,我无法使用代理的客户端类创建已配置的客户端。我必须明确地创建绑定,行为和端点,并将它们添加到客户端。举个例子,根据我的所有研究,我应该做的是:

ServiceClient = new ServiceClient();

然后开始跑步。但是,我要做的是:

WebHttpBinding serviceBinding = new WebHttpBinding(WebHttpSecurityMode.Transport);
serviceBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
EndpointAddress endPointAddress
            = new EndpointAddress(<endpointaddress>);

// Create Service Client 
ServiceClient = new ServiceClient(serviceBinding, endPointAddress);

// Add Web Behavior to the EndPoint
WebHttpBehavior webHttpBehavior = new WebHttpBehavior();
ServiceClient.Endpoint.Behaviors.Add(webHttpBehavior);

// Set up the request to POST with a wrapped request
WebInvokeAttribute postAttribute = new WebInvokeAttribute();
postAttribute.Method = "POST";
postAttribute.BodyStyle = WebMessageBodyStyle.WrappedRequest;
ServiceClient.Endpoint.Contract.Operations.Find(<operationname>).Behaviors.Add(postAttribute);

为了模仿服务配置:

<behaviors>
      <endpointBehaviors>
        <behavior name="AspNetAjaxBehaviorXml">
          <webHttp />
        </behavior>
      </endpointBehaviors>
       <serviceBehaviors>
          <behavior name="AuthenticationServicesBehavior">
            <serviceMetadata httpsGetEnabled="true" policyVersion="Policy15" />
            <serviceDebug includeExceptionDetailInFaults="true" />
          </behavior>
          <behavior name="LoggingServicesBehavior">
             <serviceMetadata httpsGetEnabled="true" policyVersion="Policy15" />
             <serviceDebug includeExceptionDetailInFaults="true" />
          </behavior>
       </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="True" />
    <bindings>
      <webHttpBinding>
        <binding name="WebSslBinding">
          <security mode="Transport">
            <transport clientCredentialType="None" />
          </security>
        </binding>
      </webHttpBinding>
    </bindings>
    <services>
       <service behaviorConfiguration="AuthenticationServicesBehavior"
          name="AuthenticationServices">
          <endpoint address="authenticate" behaviorConfiguration="AspNetAjaxBehaviorXml"
             binding="webHttpBinding" bindingConfiguration="WebSslBinding"
             name="AuthenticationEndPoint" bindingNamespace="<mynamespace>"
             contract="IService" />
       </service>

“添加服务参考”给了我这个:

<bindings>
   <customBinding>
    <binding name="AuthenticationEndPoint">
     <!--    WsdlImporter encountered unrecognized policy assertions in ServiceDescription 'EchoAppsServices':    -->
     <!--    <wsdl:binding name='AuthenticationEndPoint'>    -->
     <!--        <sp:HttpsToken xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">..</sp:HttpsToken>    -->
     <textMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16"
      messageVersion="Soap12" writeEncoding="utf-8">
      <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
       maxBytesPerRead="4096" maxNameTableCharCount="16384" />
     </textMessageEncoding>
    </binding>
   </customBinding>
  </bindings>
  <client>
   <endpoint binding="customBinding" bindingConfiguration="AuthenticationEndPoint"
    contract="AuthenticationService"
    name="AuthenticationEndPoint" />
  </client>

我认为我无法创建开箱即用的默认客户端与WsdlImporter的问题有关。当我使用svcutil时,我收到类似的错误:

Warning: The following Policy Assertions were not Imported:
  XPath://wsdl:definitions[@targetNamespace='<mynamespace>']/wsdl:binding[@na
me='AuthenticationEndPoint']
  Assertions:
    <sp:HttpsToken xmlns:sp='http://schemas.xmlsoap.org/ws/2005/07/securitypolic
y'>..</sp:HttpsToken>

我确定它与我的自签名证书有关,但是我无法让makecert给我一个功能正常的证书,而CA不会导致我的IIS崩溃。

环境详情: VS2008 XP 64bit .NET 3.5 IIS6

提前感谢您的帮助......

3 个答案:

答案 0 :(得分:1)

如果要创建客户端代理类,为什么要使用webHttpBinding。为什么不使用wsHttpBinding?

webHttpBinding的重点在于您可以在客户端上使用HttpWebRequest和Microsoft.Http.HttpClient等标准Http工具包来访问该服务。

答案 1 :(得分:1)

不确定这只是一个错字 - 但你的代码和配置不匹配。

请参阅您的代码:

serviceBinding.Security.Transport.ClientCredentialType = 
      HttpClientCredentialType.Windows;

但在你的配置中:

<webHttpBinding>
    <binding name="WebSslBinding">
      <security mode="Transport">
        <transport clientCredentialType="None" />
      </security>
    </binding>
</webHttpBinding>

在代码中,您将HttpClientCredentialType设置为Windows(集成Windows身份验证),但在配置中,您将其设置为None

尝试更改配置以使用Windows:

      <security mode="Transport">
        <transport clientCredentialType="Windows" />
      </security>

再试一次。

如果没有得到更好的结果:为什么不在导入后手动更新客户端配置?众所周知,WSDL导入器在这里和那里都有打嗝,而且大多数情况下,只需使用你的大脑和手动配置调整就可以解决问题。您基本上只需要将<endpointBehaviors><bindings>部分复制到客户端配置中,并定义引用相应端点的<client>部分 - 这几乎都是。

答案 2 :(得分:0)

你提出了一个很好的观点,但我开始着手制作一个可以从客户端或服务器调用的RESTful Web服务。我现在可以从它的方式调用它,但我必须明确配置服务器端。这是不可接受的,因为此应用程序将部署到多个最终用户,并且更改配置文件比修补更容易。我知道我可以创建多个端点,但我宁愿只需要为每个服务维护一个端点。 谢谢你的回复。