1

我已经搜索和搜索,但找不到解决方案。似乎遇到这种情况会比较普遍......通过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 为我提供一个不会导致我的 IIS 崩溃的 CA 的有效证书。

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

在此先感谢您的帮助...

4

3 回答 3

1

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

webHttpBinding 的全部意义在于,您可以在客户端使用标准 Http 工具包(如 HttpWebRequest 和 Microsoft.Http.HttpClient)来访问服务。

于 2010-02-18T04:45:17.210 回答
1

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

在您的代码中查看:

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

但在你的配置中:

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

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

尝试更改您的配置以使用 Windows:

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

然后再试一次。

如果您没有得到更好的结果:为什么不在导入后手动更新客户端配置?众所周知,WSDL 导入器有时会出现问题,而且大多数情况下,只需动动脑筋并手动调整配置即可解决问题。您基本上只需将<endpointBehaviors><bindings>部分复制到您的客户端配置中并定义一个<client>引用适当端点的部分 - 这几乎就是全部。

于 2010-02-18T06:17:58.377 回答
0

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

于 2010-02-18T05:16:12.510 回答