简体   繁体   English

端点和 WSHttpBinding 以编程方式

[英]Endpoint and WSHttpBinding programmatically

I'm trying to move the endpoint and wshttpbinding configuration to ac# file so I can change the endpoint address at runtime (select from dropdown, process etc).我正在尝试将endpointwshttpbinding配置移动到 ac# 文件,以便我可以在运行时更改端点地址(从下拉列表、进程等中选择)。 However after creating a WsHttpBinding object, EndpointAddress object and passing them to my client.但是,在创建WsHttpBinding对象、 EndpointAddress对象并将它们传递给我的客户端之后。 It will throw the following exception:它将抛出以下异常:

System.ServiceModel.FaultException: The caller was not authenticated by the service System.ServiceModel.FaultException: 调用者未经服务验证

This is the same exception I get if the user credentials are incorrect.如果用户凭据不正确,这与我得到的异常相同。 However they haven't changed from using the Web.config file to creating these config options programmatically.但是,它们并没有从使用Web.config文件更改为以编程方式创建这些配置选项。

Web.config (works): 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

Programmatically (does not work):以编程方式(不起作用):

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

I've tried following tutorials / looking at answers but I can't figure it out.我试过遵循教程/查看答案,但我无法弄清楚。

My solution我的解决方案

Keep the binding the same.保持绑定不变。

Change the endpoint so there is no address更改端点以便没有地址

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

Change endpoint at run time by changing the Uri object and pass the endpoint name your service as the first argument通过更改 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";

You can remove EVERYTHING in your app.config - so there is no binding or interface info.您可以删除 app.config 中的所有内容 - 因此没有绑定或界面信息。

Your runtime code is this:你的运行时代码是这样的:

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

Just for reference, here is my app.config:仅供参考,这是我的 app.config:

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

/************ You can dumb this down to this: ********************************/ /************ 你可以把它简化成这样:******************************** ***/

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

暂无
暂无

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

相关问题 什么用作端点绑定而不是wsHttpBinding(对于Silverlight) - What to use as endpoint binding instead of wsHttpBinding (for Silverlight) 找不到与绑定WSHttpBinding的端点的scheme http匹配的基址 - Could not find a base address that matches scheme http for the endpoint with binding WSHttpBinding 以编程方式修改端点ReaderQuotas - Modify endpoint ReaderQuotas programmatically 以编程方式配置端点 - Configure endpoint programmatically 找不到与绑定WSHttpBinding的端点匹配方案http的基地址。 注册的基址方案为[https] - Could not find a base address that matches scheme http for the endpoint with binding WSHttpBinding. Registered base address schemes are [https] WCF错误。 找不到与绑定WSHttpBinding的端点的方案http匹配的基地址 - Error with WCF. Could not find a base address that matches scheme http for the endpoint with binding WSHttpBinding Https和无法找到与绑定WSHttpBinding的端点的方案https匹配的基地址 - Https and Could not find a base address that matches scheme https for the endpoint with binding WSHttpBinding 在WCF中以编程方式在Endpoint Contract上添加操作 - Programmatically add operations on an Endpoint Contract in WCF 这两个代码应该等效吗? 以编程方式设置端点 - This two codes should be equivalent? Setting Endpoint programmatically 以编程方式确定C#中Web服务的终结点 - Programmatically determine endpoint for Web Service in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM