Engineer
Engineer

Reputation: 3

Consuming Self Hosted WCF With Http SOAP

Kindly, i made a simple WCF self console hosted service with the following code:

using System.ServiceModel;
namespace SimpleService
{
    [ServiceContract]
    public interface ISimpleService
    {
        [OperationContract]
        int IncrementNumber();
    }
}

And the implementation :

using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Threading;

namespace SimpleService

{
    [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single, InstanceContextMode = InstanceContextMode.Single)]
    public class SimpleService : ISimpleService
    {
        int _number;
        public int IncrementNumber()
        {
            _number = _number + 1;
            Thread.Sleep(100);
            Console.WriteLine(_number.ToString());
            return _number;
        }
    }
}

And I hosted in app with the following code :

using System;
using System.ServiceModel;

namespace Host
{
    class Program
    {
        public static void Main()
        {
            using (ServiceHost host = new
                ServiceHost(typeof(SimpleService.SimpleService)))
            {
                host.Open();
                Console.WriteLine("Host started @ " + DateTime.Now.ToString());
                Console.ReadLine();
            }
        }
    }
}

And app config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="mexBehavior">
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="mexBehavior" name="SimpleService.SimpleService">
        <endpoint address="SimpleService" binding="basicHttpBinding"
                  contract="SimpleService.ISimpleService" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080" />
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>
</configuration>

And now iam trying to call this service from Android mobile app that will call this service hopfully using SOAP Message through this address( http://amyamyamy.no-ip.org/ ) but i couldnt make it work till now!!! so i need your help regarding c# code that can call the above hosted service using SOAP message. Many thanks for your kind effort.

My Client app code is :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System.Xml;

namespace Client
{
    class Program
    {
        public static void Main()
        {
            //SimpleService.SimpleServiceClient client =
            //new SimpleService.SimpleServiceClient();
            var client = new WebClient();
            string data = GetSoap();
            Console.WriteLine(data);
  client.Headers.Add("Content-Type", "text/xml;charset=utf-8");
        client.Headers.Add("SOAPAction", "\"http://tempuri.org/IService1/GetData\"");
        var response = client.UploadString("http://amyamyamy.no-ip.org/SimpleService/SimpleService.svc", data);
        Console.WriteLine("Number after first call = " +response.ToString());
        Console.ReadLine();
    }
        static string GetSoap()
        {
            string xmlPayloadText = @"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:tem=""http://tempuri.org/"">
   <soapenv:Header/>
   <soapenv:Body>
      <tem:GetData>
         <!--Optional:-->
         <tem:value></tem:value>
      </tem:GetData>
   </soapenv:Body>
</soapenv:Envelope>";
            return xmlPayloadText;
        }
    }
}

Upvotes: 0

Views: 738

Answers (1)

Rahul
Rahul

Reputation: 77876

With 404 it means it giving you a service not found error and to my understanding that's because the <baseAddresses> part in your service configuration file

   <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8080" />
      </baseAddresses>
    </host>

You are actually using localhost wheras you should be using a proper server name or IP address.

Again, in your client application you are trying to get the service metadata WSDL using

http://amyamyamy.no-ip.org/SimpleService/SimpleService.svc

Which to me looks like, as if your service is deployed in IIS server but it's not and so you are not able to access the service.

You should rather change your base address like

   <host>
      <baseAddresses>
        <add baseAddress="http://my_actual_server_name:8080/" />
      </baseAddresses>
    </host>

In your client application try accessing it like

http://my_actual_server_name:8080/SimpleService

That is, your client code

var response = client.UploadString("http://my_actual_server_name:8080/SimpleService", data);

Upvotes: 1

Related Questions