0

So i have the below data in app.config of the project that has a serrvice reference:

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="P4U" />
            <binding name="P4U1">
                <security mode="TransportWithMessageCredential" />
            </binding>
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://*.svc"
            binding="basicHttpBinding" bindingConfiguration="P4U" contract=*.ServiceContract"
            name="P4U" />
        <endpoint address="https://*.svc"
            binding="basicHttpBinding" bindingConfiguration="P4U1" contract="*.ServiceContract"
            name="P4U1" />
    </client>
</system.serviceModel>

I cant use this data in config. Instead, i have to create them in code. I used BasicHttpBinding and it works fine for http calls but i can get it to work for https. I tried basichttps and wshttpbinding. The username and password is send in header. Any idea what to do? I tried with TransportWithMessageCredential with basichttp and basichttps.

Below detail is from the client document:

For each request a SOAP header is required, like in the sample: …

<soap:Header>
<Authentication xmlns="http://*">
<User>customer specific, to be provided</User>
<Password>customer specific, to be provided</Password>
</Authentication>
</soap:Header>

Below code is that i have right now. It is the constructor:

private Binding serviceBinding ;    
public ShippingService(string url, AuthenticationHeader webAuthenticationHeader, LogHelper logger)
    {
        authenticationHeader = webAuthenticationHeader;
        this.Logger = logger;
        var uri = new Uri(url);
        if(uri.Scheme== "https")
        {
            this.serviceBinding = new BasicHttpBinding(BasicHttpSecurityMode.TransportWithMessageCredential);
        }
        else
        {
            this.serviceBinding = new BasicHttpBinding();
        }

        this.endpointAddress = new EndpointAddress(url);
    }

And in the service call method i have the below line:

ServiceContractClient clientService = new ServiceContractClient (serviceBinding, endpointAddress);

The first exception i got was one below when i was using wshttpbinding:

Content Type application/soap+xml; charset=utf-8 was not supported by service https://*ervice.svc. The client and service bindings may be mismatched. For HTTP's.

I changed back to basic and since then i have been only getting the below exception:

The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state.

Aswin Francis
  • 87
  • 1
  • 13
  • can you show us the code how you have tried to create client configurations? – S.Dav Mar 23 '18 at 10:52
  • @S.Dav i have updated the question details – Aswin Francis Mar 26 '18 at 06:47
  • "Faulted state" means the channel has seen en error already. You need to look for exceptions earlier. – H H Mar 26 '18 at 07:13
  • @HenkHolterman the last known exception is the one below i think: Content Type application/soap+xml; charset=utf-8 was not supported by service https://*ervice.svc. The client and service bindings may be mismatched. For HTTP's. If the service goes into a faulted state, can I not debug again? I don't have access to the service code as it is third party. – Aswin Francis Mar 26 '18 at 07:53
  • Great, add that error msg to the question. I stopped reading half way. – H H Mar 26 '18 at 07:55
  • @HenkHolterman done :) – Aswin Francis Mar 26 '18 at 08:04
  • Your Https binding is missing or incomplete. I suggest https://stackoverflow.com/questions/14874529/what-is-the-difference-between-basichttpsbinding-and-wshttpbinding-with-transpor?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – H H Mar 26 '18 at 09:42
  • Or provide the correct, extensive information here. – H H Mar 26 '18 at 09:42
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/167555/discussion-between-aswin-francis-and-henk-holterman). – Aswin Francis Mar 26 '18 at 11:05

1 Answers1

0

If you end up with getting an exception that doesn’t give a lot of clue on what is the root cause( like the fault state we were getting), do the below steps to guide you find possible solution

  1. Update the web.config to generate service logs https://learn.microsoft.com/en-us/dotnet/framework/wcf/diagnostics/configuring-message-logging

  2. Run the application so you hit the error

  3. Open the service log to find the root cause

[ServiceLogError]https://i.stack.imgur.com/i52jC.jpg

I got the error: Username is not provided. Even though while debugging, the exception i got was service is in faulted state.

  1. Use the power of google: https://amadotech.blogspot.in/2009/11/error-username-is-not-provided-specify.html

So in short i had to add the below two lines of code clientService.ClientCredentials.UserName.UserName = "xyz"; clientService.ClientCredentials.UserName.Password = "123";

Rest of the code remains same

Aswin Francis
  • 87
  • 1
  • 13