簡體   English   中英

通過WsHttpBinding以編程方式創建Wcf客戶端

[英]Programatically Creating Wcf client with WsHttpBinding

我已使用WSDL文件添加ServiceReference,客戶端拋出錯誤:

在ServiceModel客戶端配置部分中找不到名稱為'Endpoint1'且合同為'ServiceReference.IService'的終結點元素。 這可能是因為找不到您的應用程序的配置文件,或者是因為在客戶端元素中找不到與該名稱匹配的端點元素。

我不確定為什么沒有選擇app.config端點?

的app.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.serviceModel>
    <bindings>
      <wsHttpBinding>            
        <binding name="wsHttpPoint">
          <security mode="TransportWithMessageCredential">
            <transport clientCredentialType="None" />
            <message clientCredentialType="UserName" establishSecurityContext="false" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <client>
      <endpoint address="https://example.com:5555/Service.svc"
        binding="wsHttpBinding" bindingConfiguration="wsHttpPoint" contract="ServiceReference.IService"
        name="Endpoint1" />
    </client>
  </system.serviceModel>
</configuration>    

手動創建代理:

WSHttpBinding binding = new WSHttpBinding();

binding.Security.Mode = SecurityMode.Transport;            

EndpointAddress address = new EndpointAddress("https://example.com:5555/Service.svc");

Client.ClientCredentials.UserName.UserName = "";    
Client.ClientCredentials.UserName.Password = "";

Client = new ConnectionServiceClient(binding, address);

現在有一個新錯誤:

無法打開connectionSecure通道,因為與遠程端點的安全協商失敗。 這可能是由於在用於創建通道的EndpointAddress中缺少或錯誤指定了EndpointIdentity所致。 請驗證EndpointAddress指定或暗示的EndpointIdentity正確標識了遠程端點。

我是否正確設置了綁定? 我看到的每個帖子都談論basicHttpBinding,是否有用於安全傳輸的wsHttpbinding示例?

在您的app.config中,客戶端需要知道服務器以哪個帳戶運行。 只需添加:

<endpoint address="https://example.com:5555/Service.svc"
    binding="wsHttpBinding" bindingConfiguration="wsHttpPoint" contract="ServiceReference.IService"
    name="Endpoint1">
    <identity>
      <userPrincipalName value="AccountName@domain" />
    </identity>
  </endpoint>

另外,可能需要將使用的端口注冊給執行服務的用戶。 以管理員身份打開“開發人員命令提示符”,然后運行:

netsh http add urlacl url=http://+:{Port}/ user={Domain\User}

根據您的需要交換{}的值。 這只能運行一次。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM