-
Soap
Hello Everybody:
I need to communicate with a webservice via soap from the server.I've found lots of examples of what a soap message is but i can't find any code of how to create one in C#.net and how to actually send it to the webservice from the server..
Any help much appreciated.
Thanks,
Rahil
-
you might be able to use the wsdl.exe tool, point it at that web service, and it'll try to build a proxy class for you. Then you can just use the proxy class and not create any soap at all(.net will do that for you under the covers). If they use a generic interface, you're good to go, if they use custom stuff that .net might not know about you're out of luck on the automatic stuff. We've had to recently roll our own xml to interface with a java-based web service from .net. The xml was REALLY simple, it expected something like <Xml><UpdateEmployee><FirstName>Chris</FirstName></UpdateEmployee></Xml> and that's all it needed(not exactly, the xml was a little more complicated but essentially it just wanted some nodes like that. The namespaces are pretty confusing still to me, but using .net makes that a little easier.
-
Here's a hello world c# web service:
PHP Code:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
namespace StupidWebServices
{
/// <summary>
/// Totally useless WebService.
/// </summary>
[WebService(Namespace="http://panteravb.com/StupidWebServices")]
public class HelloWorld : System.Web.Services.WebService
{
[WebMethod]
public string SayHello(string yourName)
{
return "Hello " + yourName;
}
}
}
...it's located here. I compiled that with the following cmd line:
Code:
csc.exe /t:library /out:bin/StupidWebServices.dll /r:System.dll,System.Web.dll,System.Web.Services.dll HelloWorld.asmx.cs
Next, I copied this to the server and to create a client proxy class to use that webservice I used wsdl like this: which results in a file named HelloWorldWS.cs and looks like this:
PHP Code:
//------------------------------------------------------------------------------
// <autogenerated>
// This code was generated by a tool.
// Runtime Version: 1.1.4322.573
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </autogenerated>
//------------------------------------------------------------------------------
//
// This source code was auto-generated by wsdl, Version=1.1.4322.573.
//
using System.Diagnostics;
using System.Xml.Serialization;
using System;
using System.Web.Services.Protocols;
using System.ComponentModel;
using System.Web.Services;
/// <remarks/>
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Web.Services.WebServiceBindingAttribute(Name="HelloWorldSoap", Namespace="http://panteravb.com/StupidWebServices")]
public class HelloWorld : System.Web.Services.Protocols.SoapHttpClientProtocol {
/// <remarks/>
public HelloWorld() {
this.Url = "http://www.panteravb.com/HelloWorld.asmx";
}
/// <remarks/>
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://panteravb.com/StupidWebServices/SayHello", RequestNamespace="http://panteravb.com/StupidWebServices", ResponseNamespace="http://panteravb.com/StupidWebServices", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
public string SayHello(string yourName) {
object[] results = this.Invoke("SayHello", new object[] {
yourName});
return ((string)(results[0]));
}
/// <remarks/>
public System.IAsyncResult BeginSayHello(string yourName, System.AsyncCallback callback, object asyncState) {
return this.BeginInvoke("SayHello", new object[] {
yourName}, callback, asyncState);
}
/// <remarks/>
public string EndSayHello(System.IAsyncResult asyncResult) {
object[] results = this.EndInvoke(asyncResult);
return ((string)(results[0]));
}
}
Now to use this, I start a new C# console app, add a reference to System.Web.Services, add the HelloWorldWS.cs to my new project and then use this code:
PHP Code:
using System;
using System.Web.Services;
namespace HelloWorldWsTest
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
HelloWorld hw = new HelloWorld();
Console.WriteLine(hw.SayHello("Chris"));
Console.WriteLine("Press <Enter> to exit...");
Console.ReadLine();
}
}
}
.. and that's it. Poke around http://panteravb.com/HelloWorld.asmx to see what the soap looks like(that's what .net ends up doing for you).
-
what's the webservice you need to communicate with?