2014-01-07 32 views
0

我想用c#實現一個web服務。我有興趣實現服務器而不是消費者。 Web服務必須由一個獨立的軟件運行,因爲我沒有像IIS這樣的網絡服務器來部署它。我知道我可以帶一個wsdl文件,使用gsoap爲它生成C代碼,我在服務器端實現例程,並構建鏈接libgsoap的獨立程序。沒有網絡服務器的獨立c#webservice

現在我想用c#和.net平臺來做同樣的事情。

我用來研究它的wsdl文件與gsoap一樣,你可以找到它here

下一步是下載它並運行wsdl -server cal.wsdl以獲得calc.cs

在這一點上,我卡住了,因爲我無法找到關於如何進行的文檔。

如何實現擴展抽象類calc的程序?我通過運行wsdl獲得的啓動一個小型web服務器來執行所需的功能?你能指點我一些很好的文檔嗎?

新增

當我運行wsdl -server cal.wsdl我得到這個代碼

/// <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); 
} 

這是一個抽象類,因爲所有在服務器端的方法沒有實現。因此,我期待着,就像我通常用gsoap做的那樣,我將不得不擴展這個類並重寫抽象方法,這很容易。

問題是,之後我不知道如何編寫一個程序,該程序採用繼承的類並啓動Web服務器並在網絡上公開Web服務。

+0

聽起來就像你試圖從wsdl生成web服務實現。這不可能。一個wsdl文件不包含邏輯,它只告訴客戶端它需要與服務器進行通信的方法和類型。 – i3arnon

+0

是的,這就是爲什麼我在Linux/Unix中編寫代碼時使用gsoap生成C代碼,編譯前我還實現了服務器的例程,以便它知道在調用web服務時必須執行的操作。 –

+0

你問如何編寫代碼和C#並編譯它? – i3arnon

回答

3

這並不複雜,但需要從你身邊做一些工作。您可以使用WCF技術創建服務。

我想你會使用Visual Studio併爲.NET創建一個應用程序。如果你的目標是單聲道,你將不得不採取下面的例子。

1 - 創建一個控制檯.NET項目並手動添加對System.ServiceModel和System.Runtime.Serialization的引用。

2 - 您應該將您的抽象類轉換爲接口。這將是您工作中最難的部分,因爲您應該達到相同的WSDL。

例如:

[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); 

皈依例如:

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 
} 

或者,你可以嘗試使用SvcUtil工具來生成C#代理類:

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

你會得到鈣.cs作爲結果,因此您可以從中提取服務和數據合同。

3 - 無論如何,你創建服務合同的接口後,你創造它的實現:

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

    // the rest 
} 

4 - 之後,你應該創建一個ServiceHost的實例。像這樣:

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

當然,這是一個非常簡單的例子,但我希望它會給你方向。

+0

謝謝你的回答,我明白我需要在wsdl文件上運行svcutil來獲得代理類。我只是有一個更多的問題:一旦我運行svcutil calc.wsdl,你能確認我只需要實現接口calcPortType,然後我將它實例化爲我nstep 4? –

+0

基本上是的,但正如我所說創建接口可能是一個難題,因爲您的結果服務必須與使用原始WSDL生成的客戶端兼容。例如,必須從接口刪除[System.ServiceModel.OperationContractAttribute(Action =「」,ReplyAction =「*」)]屬性,向ServiceContractAttribute添加參數Name =「calc」,應用其他屬性[DataContractFormat(Style = OperationFormatStyle.Rpc)]到接口等。我也建議你使用app.config來提供服務配置。 – Ronnix