.NET動態調用WebService

這不是一篇教你瞭解WebService的博文,也不是對WebService的深入理解, 這是一篇教你在開發過程中,如果動態的調用WebService一個方法.

在比較常見的WebService調用,我們一般是選擇在項目中,新建添加引用的方式來引用WebService服務. 例如下面的方式  : 

1 : 在項目中新建添加服務引用

 

2 : 輸入引用服務器的地址

然後我們的程序集下就會多出剛纔新建的Service 引用相關的文件引用.

然後我們就可以直接在代碼中調用ServiceReference1 , 就可以了,在這裏就不在繼續的詳細做解釋說明了,我們還是回到主題,如何減去這些步驟,因爲這樣的方式添加引用,1 : 不好擴展, 2 : 比較繁瑣 

下面介紹一種方式用來動態的調用WebService References :

一 : 在項目中右鍵選擇新建

然後我們修改Component類本部.

我們添加完成之後,Component類內部默認爲:

  public partial class Component1 : Component
    {
        public Component1()
        {
            InitializeComponent();
        }
        public Component1(IContainer container)
        {
            container .Add(this );
            InitializeComponent();
        }
    }


在此我們需要做一些修改.

首先在項目中添加WebService的引用.

 

然後將Component類 繼承自 SoapHttpClientProtocol

至此將Component類改爲 :

 [ WebServiceBinding (Namespace = "http://tempuri.org/" )]
    public partial class Component1 : SoapHttpClientProtocol
    {
        public Component1()
        {
            InitializeComponent();
        }
        public Component1(IContainer container)
        {
            container . Add(this );
            InitializeComponent();
        }
    }

SoapHttpClientProtocol SoapHttpClientProtocol類可以直接訪問指定的webService的指定方法 

若要與 XML Web services 通信,請爲要調用的 XML Web services 創建一個間接或直接從 WebClientProtocol 派生的代理類。 可以不用手動創建代理類,而使用 Web 服務描述語言工具 (Wsdl.exe) 爲給定 XML Web services 的服務說明創建代理類。 當爲 SOAP 協議生成代理類時,對 XML Web services 方法的同步調用通過 Invoke 方法進行,而異步調用通過BeginInvoke 方法和 EndInvoke 方法進行。

然後在配置文件中寫入我們所要引用的服務器鏈接地址 :

<add key="ServiceAddress" value="http://localhost:7340/CourseMakerService.asmx" />

在我們添加的Conponent Class 構造中調用服務器鏈接地址

  public Component1( string serviceUrl)
        {
            if (serviceUrl. Equals( "UpdateServiceAddress" ))
                base .Url = ConfigurationManager . AppSettings["UpdateServiceAddress" ];
            else
                base .Url = ConfigurationManager . AppSettings["ServiceAddress" ];
        }


然後當我們想要調用WebService中的方法時,只需要在Component1類中寫. 

如我的調用方式 : 

[ WebServiceBinding(Namespace = "http://tempuri.org/" )]
    public class OffLineLearingClient : SoapHttpClientProtocol
    {
        public OffLineLearingClient( string serviceUrl)
        {
            if (serviceUrl. Equals( "UpdateServiceAddress" ))
                base .Url = ConfigurationManager . AppSettings["UpdateServiceAddress" ];
            else
                base .Url = ConfigurationManager . AppSettings["ServiceAddress" ];
        }
        public OffLineLearingClient()
        {
            base .Url = ConfigurationManager . AppSettings["ServiceAddress" ];
        }
        [ SoapDocumentMethod ]
        public YHBJUser GetUser( YHBJUser user)
        {
            return base . Invoke("GetUser" , new object [] { user })[0 ] as YHBJUser ;
        }
        [ SoapDocumentMethod ]
        public List < YHBJClass> GetTrainings11( string userId)
        {
            return base . Invoke("GetTrainings11" , new object [] { userId })[0 ] as List <YHBJClass > ;
        }


這樣我們就可以動態的實現如果調用WebService了.


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章