【问题标题】:Endpoint and WSHttpBinding programmatically以编程方式端点和 WSHttpBinding
【发布时间】:2014-11-24 09:37:04
【问题描述】:

我正在尝试将 endpointwshttpbinding 配置移动到 c# 文件,以便我可以在运行时更改端点地址(从下拉列表、进程等中选择)。但是在创建WsHttpBinding 对象后,EndpointAddress 对象并将它们传递给我的客户。它会抛出以下异常:

System.ServiceModel.FaultException:调用者未通过身份验证 通过服务

如果用户凭据不正确,我会遇到同样的异常。但是,它们并没有从使用 Web.config 文件更改为以编程方式创建这些配置选项。

Web.config(有效):

<binding name="myService" maxReceivedMessageSize="2147483647">
    <readerQuotas
        maxDepth="2147483647"
        maxStringContentLength="2147483647"
        maxArrayLength="2147483647"
        maxBytesPerRead="2147483647"
        maxNameTableCharCount="2147483647" />

    <security mode="TransportWithMessageCredential">
        <transport clientCredentialType="None" />
        <message clientCredentialType="UserName" establishSecurityContext="false" />
    </security>
</binding>


<client>
    <endpoint
        address="https://address/myservice.svc"
        binding="wsHttpBinding"
        bindingConfiguration="myService"
        contract="MyService.IMyService"
        name="myService"
    />
</client>

MyService service = new MyService();
service.username = "user";
service.password = "pass";
//Success

以编程方式(不起作用):

WSHttpBinding wsHttp = new WSHttpBinding();
wsHttp.MaxReceivedMessageSize = 2147483647;
wsHttp.ReaderQuotas.MaxDepth = 2147483647;
wsHttp.ReaderQuotas.MaxStringContentLength = 2147483647;
wsHttp.ReaderQuotas.MaxArrayLength = 2147483647;
wsHttp.ReaderQuotas.MaxBytesPerRead = 2147483647;
wsHttp.ReaderQuotas.MaxNameTableCharCount = 2147483647;
wsHttp.Security.Mode = SecurityMode.TransportWithMessageCredential;
wsHttp.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
wsHttp.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
wsHttp.Security.Message.EstablishSecurityContext = false;

EndpointAddress endpoint = new EndpointAddress("https://address/myservice.svc");

MyService service = new MyService(wsHttp, endpoint);
service.username = "user";
service.password = "pass";
//System.ServiceModel.FaultException: The caller was not authenticated by the service

我已尝试遵循教程/查看答案,但我无法弄清楚。

【问题讨论】:

  • 您在哪一行遇到错误。
  • @AshokRathod 每当我从服务调用任何方法时都会收到错误消息。 service.myMethod()

标签: c# asp.net wcf


【解决方案1】:

我的解决方案

保持绑定不变。

更改端点,因此没有地址

<client>
  <endpoint
    binding="wsHttpBinding" bindingConfiguration="myService"
    contract="MyService.IMyService" name="myService" />
</client>

通过更改 Uri 对象在运行时更改端点并将端点名称作为第一个参数传递给您的服务

Uri uri = new Uri("https://address/myservice.svc");
var address = new EndpointAddress(uri);
service= new MyService("myService", address);
service.username = "user";
service.password = "pass";

【讨论】:

    【解决方案2】:

    您可以删除 app.config 中的所有内容 - 因此没有绑定或接口信息。

    你的运行时代码是这样的:

    //create an endpoint address
    var address = new EndpointAddress("http://localhost:51353/EmployeeService.svc");
    
    //create a WSHttpBinding
    WSHttpBinding binding = new WSHttpBinding();
    
    //create a channel factory from the Interface with binding and address
    var channelFactory = new ChannelFactory<IEmployeeService>(binding, address);
    
    //create a channel
    IEmployeeService channel = channelFactory.CreateChannel();
    
    //Call the service method on the channel
    DataSet dataSet = channel.SelectEmployees();
    

    仅供参考,这里是我的app.config:

    <configuration>
        <startup> 
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
        </startup> 
    </configuration>
    

    /************ 您可以将其简化为:************************** ******/

    var factory = new ChannelFactory<IEmployeeService>(new WSHttpBinding());
    var channel = factory.CreateChannel(new EndpointAddress("http://localhost:51353/EmployeeService.svc"));
    

    【讨论】:

      猜你喜欢
      • 2017-03-30
      • 2012-07-19
      • 1970-01-01
      • 1970-01-01
      • 2010-11-03
      • 1970-01-01
      • 2019-09-29
      • 1970-01-01
      • 2016-05-25
      相关资源
      最近更新 更多