7

I have to call a WCF service. The WCF service is on and I can edit its configuration.

I want to create a client that calls the service. I cannot add the service reference to my client, so I am trying to call it with a HttpClient.

The client side code:

    using (var client = new HttpClient())
    {
        //soapString is my input class serialized
        var content = new StringContent(soapString, Encoding.UTF8, "text/xml");
        using (var postResponse = client.PostAsync("http://localhost:52937/Attempts.svc/", content).Result)
        {
            string postResult = postResponse.Content.ReadAsStringAsync().Result;

        }
    }

The server side code:

    [ServiceContract]
    public interface IAttempts
    {
        [OperationContract]
        void ReceiveAttemptResult(ReceiveAttemptResultInput result);
    }

    public class Attempts : IAttempts
    {
        string _backendUrl;

        public void ReceiveAttemptResult(ReceiveAttemptResultInput result)
        {
           //...
        }
    }

And in the end the web.config server side:

    <system.serviceModel>
      <services>
        <service name="it.MC.Listeners.Attempts">
          <endpoint address="" contract="it.MC.Listeners.IAttempts" binding="basicHttpBinding"/>
          <endpoint address="mex" contract="IMetadataExchange" binding="mexHttpBinding"/>
        </service>
      </services>
      <behaviors>
        <serviceBehaviors>
          <behavior name="">
            <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
            <serviceDebug includeExceptionDetailInFaults="false" />
          </behavior>
        </serviceBehaviors>
      </behaviors>
      <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    </system.serviceModel>

When I call the service, I just obtain an empty string and I cannot stop in debug inside the service... What's wrong?

Thank you

strongbutgood
  • 655
  • 7
  • 22
Simone
  • 2,304
  • 6
  • 30
  • 79
  • Your endpoint url seems to be incomplete. It should be something like: `http://localhost:52937/Attempts.svc/Endpoint` – Oluwafemi Aug 03 '16 at 09:26
  • I have changed the url as following: `http://localhost:52937/PaymentAttempts.svc/ReceiveAttemptResult` but nothing changed :( – Simone Aug 03 '16 at 09:32
  • 4
    For future readers. I run WcfTestClient and looked at HTTP headers it sent . So i added `httpClient.DefaultRequestHeaders.Add("SOAPAction", "TheSameAsIn_WcfTestClient") ad it works ! ` – Disappointed Dec 05 '16 at 14:05

1 Answers1

1

Just in case this bedevils anyone else. Thank you @Disappointed for your missing piece of the puzzle, it prompted me to run the thing in WCF Test Client with Fiddler open to see what I was missing:

    HttpClient httpClient = new HttpClient();
    httpClient.DefaultRequestHeaders.Add("SOAPAction", "http://tempuri.org/IMyService/Mymethod_Async");
    string soapEnvelope = "<s:Envelope xmlns:s= \"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><Mymethod_Async xmlns=\"http://tempuri.org/\"/></s:Body></s:Envelope>";
    var content = new StringContent(soapEnvelope, Encoding.UTF8, "text/xml");
    HttpResponseMessage hrm = httpClient.PostAsync("http://MyService.MyDomain.com/MyService.svc", content).Result;
Tom Regan
  • 3,580
  • 4
  • 42
  • 71