I have a demo soap service which echos back the request
public class EchoWebService : SoapService
{
[SoapMethod("Echo")]
public string Echo(string request)
{
Console.WriteLine("Request: {0}", request);
return DateTime.Now + Environment.NewLine + request;
}
}
Service is hosted using TCP,
Uri to = new Uri("soap.tcp://localhost:5696");
EndpointReference EPR = new EndpointReference(to);
SoapReceivers.Add(EPR, new EchoWebService());
I can invoke the service by writing my own TCP SoapClient,
Uri to = new Uri("soap.tcp://localhost:5696");
EndpointReference EPR = new EndpointReference(to);
mysoapclient client1 = new mysoapclient(EPR);
Console.WriteLine(client1.GetResponse("Hello World"));
class mysoapclient : SoapClient
{
public mysoapclient(EndpointReference endpoint) : base(endpoint) { }
public string GetResponse(string request)
{
return base.SendRequestResponse("Echo",
request).GetBodyObject(typeof(string)).ToString();
}
}
Is there any way to invoke the service using TcpClient? I want to send the raw xml over TCP to invoke the service and read the response back from the network stream. I tried using following code and xml but it doesn't seem to work.
TcpClient client = new TcpClient("localhost", 5696);
byte[] request = Encoding.UTF8.GetBytes(xml);
NetworkStream stream = client.GetStream();
stream.Write(request, 0, request.Length);
byte[] response = new byte[client.ReceiveBufferSize];
client.GetStream().Read(response, 0, client.ReceiveBufferSize);
Console.WriteLine(Encoding.UTF8.GetString(response));
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Echo xmlns="http://tempuri.org/">
<request>Hello World</request>
</Echo>
</soap:Body>
</soap:Envelope>
Any help would be appreciated.