I had an "oh my god I can't believe it's this easy" moment today when I deployed my first web service to C# from Java. I started off with my standard bare-bones J2EE server and in the Visual Studio environment. Being an XP guy, I wrote my test first with nUnit.
using System;
using NUnit.Framework;
namespace Simple
{
[TestFixture] public class SoapClientTest : Assertion
{
private SoapClient client;
[Test] public void EchoMessage()
{
this.client = new SoapClient();
AssertEquals("Hello world", client.echoHeader("Hello world"));
}
}
}
To make the test work, all I had to do was select Project->Add Web Reference and then enter in the WSDL reference to my web service. When I added in the echo service (http://localhost:8080/axis/EchoHeaders.jws?wsdl) that comes with Axis, it generated the objects for me. How cool is that? Here is the code:
using System;
namespace Simple
{
public class SoapClient
{
Simple.localhost.EchoHeadersService echoHeaderService = null;
public SoapClient()
{
echoHeaderService = new Simple.localhost.EchoHeadersService();
}
public string echoHeader(string message)
{
return echoHeaderService.echo(message);
}
}
}
Before getting to work, however, my google searches turned up a lot of crap examples. The only one that was useful was Developing a .Net client to interact with a WebSphere Web service. Most of it had to do with trying create a web service without using WDSL, and I guess that is understandable. Basically, if you need to look at MSDN or any documentation, you are doing it wrong. I've added the C# source into my simple template code.