1. 程式人生 > >基於WebService的C#程式與C#程式之間的方法呼叫

基於WebService的C#程式與C#程式之間的方法呼叫

一、WebService

二、建立兩個Web專案

1、建立web空專案

檔案——新建——專案:Visual C#——Web——ASP.NET Web應用程式——空 

2、新增web服務

右鍵專案名稱——新增——新建項:web服務

看到的介面為:

3、除錯

點選工具欄裡的瀏覽器進行除錯,可以看到第一個helloword()的執行結果: 

4、根據這個步驟建立兩個WebService,分別為WebService1和WebService2。此處為兩個WebService新增不同的方法以便更好地區分。

//WebService1

        [WebMethod(Description = "求和方法")]
        public int Sum(int a, int b)
        {
            int sum = a + b;
            return sum;
        }

//WebService2

        [WebMethod(Description = "求積方法")]
        public int Product(int a, int b)
        {
            int product = a * b;
            return product;
        }

二、WebService1呼叫WebService2:固定連結

1、方法一:新增Web引用

1)新增Web引用

在WebService1中:右鍵引用——新增服務引用——高階——新增Web引用——url:輸入WebService2釋出後的連結——轉到——新增引用。

在輸入WebService2釋出後的連結並點選轉到按鈕後,可看到出現了WebService2的網頁介面;在新增引用按鈕上面,有一個Web引用名,預設為localhost,在程式碼中會用到,一般最好修改為其它名字;點選新增引用後,可以看到解決方案管理器裡引用下面多了Web References。

此時Web.config裡增加了相關配置程式碼:

  <applicationSettings>
    <WebApplication1.Properties.Settings>
      <setting name="WebApplication1_localhost_WebService2" serializeAs="String">
        <value>http://localhost:64862/WebService2.asmx</value>
      </setting>
    </WebApplication1.Properties.Settings>
  </applicationSettings>

2)在WebService1中呼叫WebService2的Product()函式:

        [WebMethod]
        public int WebService(int a, int b)
        {
            localhost.WebService2 webService = new localhost.WebService2(); //localhost為上面新增引用時Web引用名的預設值,最好修改為其它
            int count = webService.Product(a, b);
            return count;
        }

然後執行:預設以xml格式顯示結果

 

2、方法二:新增服務引用

1)在WebService1中:右鍵引用——新增服務引用——在地址中輸入WebService2的連結——轉到——確定。

點選轉到後,服務中出現WebService2,點選它,出現WebService2Soap,再點選它,操作中出現WebSercvice的兩個函式。一般預設名稱空間為ServiceReference1,與上面的localhost是一個作用,最好修改它。點選確定後,引用裡多了下圖的兩個引用。

此時Web.config裡增加了相關配置程式碼:

  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="WebService2Soap" />
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:64862/WebService2.asmx" binding="basicHttpBinding"
        bindingConfiguration="WebService2Soap" contract="ServiceReference1.WebService2Soap"
        name="WebService2Soap" />
    </client>
  </system.serviceModel>

2)在WebService1中呼叫WebService2的Product()函式:

        [WebMethod]
        public int WebService()
        {
            ServiceReference1.WebService2SoapClient webservice = new ServiceReference1.WebService2SoapClient(); //ServiceReference1為新增服務引用的名稱空間
            int count = webservice.Product(2, 3);
            return count;
        }

三、WebService1呼叫WebService2:動態連結

1、以引數形式表示服務引用的連結

本地除錯時一般為固定地址,當需要釋出或遷移時,最好就要使用動態連結呼叫了。

此處使用上面的第二種方法進行動態呼叫。先在WebService2SoapClient上右鍵——轉到定義,檢視WebService2SoapClient提供的方法,可以看到它可以接受兩個引數:服務引用的名稱、服務引用的連結。

在Web.config中找到服務引用的名字:name="WebService2Soap",將WebService2的埠改為60713,修改上面的程式碼:

        [WebMethod]
        public int WebService()
        {
            ServiceReference1.WebService2SoapClient webservice = new ServiceReference1.WebService2SoapClient("WebService2Soap", "http://localhost:60713/WebService1.asmx");
            int count = webservice.Product(2, 4);
            return count;
        }

當需要隨時動態修改連結時,將引數2服務引用的連結寫到程式的配置檔案裡,此處改為一個變數,然後用變數去讀取配置檔案裡的連線。

2、配置檔案

1)在WebService1專案的bin目錄下建立一個config.xml檔案(開啟資料夾,在資料夾中新建,工程目錄會自動顯示它),可做如下編寫:

<config>
  <webservice_path>
    <value>http://localhost:60713/WebService1.asmx</value>
  </webservice_path>
</config>

2)右鍵專案名稱——新增——新建項:全域性應用程式類Global.asax: 

3)在Global.asax的Application_Start()中讀取配置檔案資訊:

using System.Xml;
using System.IO;

protected void Application_Start(object sender, EventArgs e)
{
       XmlDocument configXml = new XmlDocument();
       string strPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin", "config.xml");// 上面新建的"config.xml";
       if (File.Exists(strPath))
       {
           //載入xml檔案並且讀取檔案根節點(每個xml檔案有且僅有一個根節點)
           configXml.Load(strPath);
           XmlNode root = configXml.SelectSingleNode("config");
           //讀取config.xml檔案裡的webservice_path
           XmlNodeList webservice_path= root.SelectNodes("webservice_path");
           string abc = ((XmlElement)webservice_path.Item(0)).ChildNodes.Item(0).InnerText;
       }
}

4)對於配置檔案config.xml來說,檔案的內容是隨時可變的,屬於動態變數。但是對於C#工程來說,它讀取的永遠都是config.xml的節點,節點名是不變的,它是靜態變數。也即是隻要程式執行,Global.asax裡讀取的值就不會改變,程式裡引用的這個對應值不會改變。如果要改變程式裡的值,只有停止程式,修改後重新執行才會成功。

為了使結構清晰,新建一個類Config.cs,在類中定義靜態變數,作為程式在執行時的變數名。這樣,讀取配置檔案的結構就是:config.xml(可隨時修改的配置檔案)——Global.asax(讀取配置檔案的值)——Config.class定義值對應的變數名,以供程式使用。config.xml和Config.class的名字可以修改,Global.asax最好不要修改。 

因此根據上面的例子,就需要新增一個類Config.class替代上面的變數abc:右鍵專案名稱——新增——類,將類名設為Config.cs,在其中設定讀取config.xml的靜態變數,一般把該變數名設定為與xml檔案中相同:

namespace WebApplication1
{
    public class Config
    {
        public static string webservice_path;
    }
}

修改第3步中最後一句程式碼,用上面定義的靜態變數webservice_path替換其中定義的字串abc

 //把string abc 改為已經定義好的變數Config.webservice_path
Config.webservice_path = ((XmlElement)webservice_path.Item(0)).ChildNodes.Item(0).InnerText;

在其他地方需要使用webservice_path時直接就使用Config.webservice_path即可。 

需要注意的是,引用類時必須是在相同的名稱空間下,如果是不同的名稱空間,需要使用“using namespace”引用名稱空間,或者使用“名稱空間.類名.函式名/方法名/變數名”來引用其他名稱空間的方法。

3、把VS介面顯示在WebService1.asmx上,執行程式即可。

當需要修改連結時,直接修改xml裡的內容即可。

當配置檔案裡原本是WebService2的連結時,修改連結,有兩種情況:

1)依舊呼叫WebService2,配置檔案裡寫WebService2的新連結;

2)呼叫其他WebService,配置檔案裡寫其他WebService的連結,如WebService3,但由於在連結下面跟著的程式碼是WebService2中的函式,因此如果WebService3中沒有該函式,程式碼會報錯,而如果有該函式,則呼叫的是WebService3中的函式。