Question

I want to implement a web service using c#. I am interested in implementing the server and not a consumer. The web service has to be run by an independent software, because I don't have a webserver such as IIS to deploy it.

In Linux/Unix I know that I can take a wsdl file, I generate the C code for it by using gsoap, I implement the routines on the server side and I build the standalone program linking libgsoap.

Now I want to do the same using c# and the .net platform.

The wsdl file I am using to study it the the same for gsoap, you can find it here.

The next step is to download it and to run wsdl -server cal.wsdl to obtain calc.cs .

At this point I am stuck, because I am not able to find documentation on how to proceed.

How can I implement program that extends the abstract class calc I obtained by running wsdl that starts a small web server performs the required functions? Can you please point me to some good documentation?

Added

When I run wsdl -server cal.wsdl I get this code

/// <remarks/>
/// <remarks>
///gSOAP 2.7.9k generated service definition
///</remarks>
[System.Web.Services.WebServiceAttribute(Namespace="http://websrv.cs.fsu.edu/~engelen/calc.wsdl")]
[System.Web.Services.WebServiceBinding(Name="calc", Namespace="http://websrv.cs.fsu.edu/~engelen/calc.wsdl")]
public abstract partial class calc : System.Web.Services.WebService {

    /// <remarks>
///Service definition of function ns__add
///</remarks>
    [System.Web.Services.WebMethodAttribute()]
    [System.Web.Services.Protocols.SoapRpcMethodAttribute("", RequestNamespace="urn:calc", ResponseNamespace="urn:calc")]
    [return: System.Xml.Serialization.SoapElement("result")]
    public abstract double add(double a, double b);

    /// <remarks>
///Service definition of function ns__sub
///</remarks>
    [System.Web.Services.WebMethodAttribute()]
    [System.Web.Services.Protocols.SoapRpcMethodAttribute("", RequestNamespace="urn:calc", ResponseNamespace="urn:calc")]
    [return: System.Xml.Serialization.SoapElement("result")]
    public abstract double sub(double a, double b);

    /// <remarks>
///Service definition of function ns__mul
///</remarks>
    [System.Web.Services.WebMethodAttribute()]
    [System.Web.Services.Protocols.SoapRpcMethodAttribute("", RequestNamespace="urn:calc", ResponseNamespace="urn:calc")]
    [return: System.Xml.Serialization.SoapElement("result")]
    public abstract double mul(double a, double b);

    /// <remarks>
///Service definition of function ns__div
///</remarks>
    [System.Web.Services.WebMethodAttribute()]
    [System.Web.Services.Protocols.SoapRpcMethodAttribute("", RequestNamespace="urn:calc", ResponseNamespace="urn:calc")]
    [return: System.Xml.Serialization.SoapElement("result")]
    public abstract double div(double a, double b);

    /// <remarks>
///Service definition of function ns__pow
///</remarks>
    [System.Web.Services.WebMethodAttribute()]
    [System.Web.Services.Protocols.SoapRpcMethodAttribute("", RequestNamespace="urn:calc", ResponseNamespace="urn:calc")]
    [return: System.Xml.Serialization.SoapElement("result")]
    public abstract double pow(double a, double b);
}

This is an abstract class, because all the method on the server side are not implement. Therefore I expect that, similarly to what I usually do with gsoap, I'll have to extend this class and override the abstract methods, which is pretty easy.

The problem is that after that I don't know how to write a program that takes the inherited class and starts a web server and expose the web service on the network.

Was it helpful?

Solution

That's not so complicated, but will require some work from your side. You can create a service using WCF technology.

I suppose you're going to use Visual Studio and create an application for .NET. If your goal is Mono, you will have to adopt the example below somehow.

1 - Create a console .NET project and add manually references to System.ServiceModel and System.Runtime.Serialization.

2 - You should convert your abstract class to an interface. This will be the hardest part of your work, because you should achieve the same WSDL as a result.

For example:

[System.Web.Services.WebServiceAttribute(Namespace="http://websrv.cs.fsu.edu/~engelen/calc.wsdl")]
[System.Web.Services.WebServiceBinding(Name="calc", Namespace="http://websrv.cs.fsu.edu/~engelen/calc.wsdl")]
public abstract partial class calc : System.Web.Services.WebService {
/// <remarks>
///Service definition of function ns__add
///</remarks>
[System.Web.Services.WebMethodAttribute()]
[System.Web.Services.Protocols.SoapRpcMethodAttribute("", RequestNamespace="urn:calc", ResponseNamespace="urn:calc")]
[return: System.Xml.Serialization.SoapElement("result")]
public abstract double add(double a, double b); 

Convertion example:

using System.ServiceModel;
// other usings, namespace etc

[ServiceContract(Namespace = "http://websrv.cs.fsu.edu/~engelen/calc.wsdl", Name = "calc")]
public interface ICalcService
{
    [OperationContract(Name = "add")]
    [return: MessageParameter(Name = "result")]
    [XmlSerializerFormatAttribute(Style=OperationFormatStyle.Rpc, Use=OperationFormatUse.Encoded)]
    double add(double a, double b);

    // the rest of methods
}

Alternatively, you could try to generate a C# proxy class using svcutil:

svcutil http://www.genivia.com/calc.wsdl

You'll get calc.cs as a result, so you can pull out service and data contracts from it.

3 - Anyway, after you create an interface with your service contract, you create its implementation:

public class CalcServiceImpl : ICalsService
{
    public double add(double a, double b)
    {
        return a + b;
    }

    // the rest
}

4 - After that you should create a ServiceHost instance. Like that:

ServiceHost host = new ServiceHost(typeof(CalcServiceImpl), new Uri("http://myhost/MyServices")))
host.AddServiceEndpoint(typeof(ICalcService), new BasicHttpBinding(), "CalcService");
host.Open();
Console.ReadKey();
host.Close();

Of course, this is a very simplified example, but I hope it will give you the direction.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top