【问题标题】:Programatically Creating Wcf client with WsHttpBinding使用 WsHttpBinding 以编程方式创建 Wcf 客户端
【发布时间】:2017-03-30 04:43:57
【问题描述】:

我已经用 WSDL 文件添加了 ServiceReference, 客户端抛出错误:

找不到名称为“Endpoint1”和合同的端点元素 ServiceModel 客户端配置中的“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 与正在执行的程序集是否在不同的项目中?
  • 在同一个项目中

标签: c# wcf proxy client


【解决方案1】:

在您的 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}

根据您的需要交换 {} 中的值。这只能运行一次。

【讨论】:

    猜你喜欢
    • 2010-12-11
    • 1970-01-01
    • 2011-02-15
    • 1970-01-01
    • 2018-09-08
    • 1970-01-01
    • 1970-01-01
    • 2011-11-07
    相关资源
    最近更新 更多