简体   繁体   English

通过WsHttpBinding以编程方式创建Wcf客户端

[英]Programatically Creating Wcf client with WsHttpBinding

I have added ServiceReference with WSDL file, Client is throwing error: 我已使用WSDL文件添加ServiceReference,客户端抛出错误:

Could not find endpoint element with name 'Endpoint1' and contract 'ServiceReference.IService' in the ServiceModel client configuration section. 在ServiceModel客户端配置部分中找不到名称为'Endpoint1'且合同为'ServiceReference.IService'的终结点元素。 This might be because no configuration file was found for your application, or because no endpoint element matching this name could be found in the client element. 这可能是因为找不到您的应用程序的配置文件,或者是因为在客户端元素中找不到与该名称匹配的端点元素。

I am not sure why the app.config endpoint is not picked up? 我不确定为什么没有选择app.config端点?

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>    

Manually creating Proxy: 手动创建代理:

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);

Now there is a new error: 现在有一个新错误:

connectionSecure channel cannot be opened because security negotiation with the remote endpoint has failed. 无法打开connectionSecure通道,因为与远程端点的安全协商失败。 This may be due to absent or incorrectly specified EndpointIdentity in the EndpointAddress used to create the channel. 这可能是由于在用于创建通道的EndpointAddress中缺少或错误指定了EndpointIdentity所致。 Please verify the EndpointIdentity specified or implied by the EndpointAddress correctly identifies the remote endpoint. 请验证EndpointAddress指定或暗示的EndpointIdentity正确标识了远程端点。

Am I setting the Binding correctly? 我是否正确设置了绑定? Every post I see talks about basichttpBinding, is there any wsHttpbinding example for Security Transport? 我看到的每个帖子都谈论basicHttpBinding,是否有用于安全传输的wsHttpbinding示例?

In your app.config, the client needs to know under which account the server runs. 在您的app.config中,客户端需要知道服务器以哪个帐户运行。 Just add: 只需添加:

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

As addition, it could be possible that you need to register your used port to the user that executes your service. 另外,可能需要将使用的端口注册给执行服务的用户。 Open your Developer Command Prompt as Administrator and run: 以管理员身份打开“开发人员命令提示符”,然后运行:

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

Exchange the Values in {} to your needs. 根据您的需要交换{}的值。 This must only be run once. 这只能运行一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM