简体   繁体   中英

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). However after creating a WsHttpBinding object, EndpointAddress object and passing them to my client. It will throw the following exception:

System.ServiceModel.FaultException: The caller was not authenticated by the service

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

<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 = 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.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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