国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
WCF之旅(1):創(chuàng)建一個(gè)簡(jiǎn)單的WCF程序

[原創(chuàng)]我的WCF之旅(1):創(chuàng)建一個(gè)簡(jiǎn)單的WCF程序

 

寫在前面

在Microsoft提出.NET戰(zhàn)略以來, 先后推出了一系列產(chǎn)品和技術(shù), 這些產(chǎn)品和技術(shù)為我們?cè)?NET平臺(tái)下建立企業(yè)級(jí)的分布式應(yīng)用提供了很大的 便利。這些技術(shù)和產(chǎn)品包括:.NET  Remoting,XML WebSerivce,WSE(2.0,3.0),Enterprise Service, MSMQ ......

我們知道,和一個(gè)相對(duì)獨(dú)立的應(yīng)用不同,我們開發(fā)一個(gè)分布式應(yīng)用, 尤其是開發(fā)一個(gè)企業(yè)級(jí)的分布式應(yīng)用, 我們需要考慮較多的東西。比如我們要考慮數(shù)據(jù)在不同的應(yīng)用之間傳遞時(shí)采取什么樣的機(jī)制, 這種數(shù)據(jù)傳遞是否是安全的,可靠的;如何在分布式的環(huán)境下進(jìn)行異常處理;如何把分別在 不同應(yīng)用中執(zhí)行的操作納入同一個(gè)事務(wù)……

對(duì)于我們上面提到的這些 問題, 這些都是開發(fā)分布式應(yīng)用考慮的典型的問題。值得慶幸的是,Microsoft開發(fā)的分布式的產(chǎn)品能夠部分的解決這些問題。.NET Remoting 為我們?cè)?NET平臺(tái)下提供了非常好的解決方案(我個(gè)人認(rèn)為,.NET Remoting是.NET平臺(tái)下最為成熟的分布式技術(shù)。比如相較于另一個(gè)使用更為廣泛的技術(shù)XML Web Service,它具有一些自己獨(dú)特的特性:可以使用不同的傳輸層協(xié)議進(jìn)行通信——Http & TCP;可以使用不同的消息編碼方式——Bianry & Text (XML);可以寄宿到IIS和任何一種托管的應(yīng)用下 ——Console Application 、WinForm Application、 Windows Service……;Server端可以通過雙向通信回調(diào)(Callback)客戶端的操作;……)XML Web Service為使我們實(shí)現(xiàn)跨平臺(tái)的系統(tǒng)能夠集成顯得如此簡(jiǎn)單。隨著技術(shù)的不斷發(fā)展,相關(guān)的技術(shù)規(guī)范(WS-* Specification)不斷完善, XML Web Service現(xiàn)在已經(jīng)成為使用最為廣泛的分布式技術(shù)了。XML Web Service能夠得到如此廣泛的推廣,這得得益于Microsoft先后兩次推出的Web Service Enhancement (WSE 2.0 、WSE 3.0)。如果沒有WSE, 單純的asmx下的如此的擔(dān)保和不可靠。WSE為Web Service解決了幾大難題:Security、Reliable Messaging、transaction Handling以及大數(shù)據(jù)的有效傳輸。 MSMQ作為一種簡(jiǎn)單而有效的機(jī)制為不同應(yīng)用之間數(shù)據(jù)的傳遞提供了保障。

其實(shí),通過合理利用上面這些分布式的技術(shù)完全可以為我們建立的一套適合不同層次需要的分布式構(gòu)架。但這里面仍然存在一些問題,那就是上面這些技術(shù)和產(chǎn)品只能解決某一方面的問題; 比如.NET Remoting雖然在.NET平臺(tái)下是一個(gè)很好的依靠, 但是考慮到他不能提供不同平臺(tái)之間的互操作性。另外,這些技術(shù)適合用了完全不同的編程方式,使得我們很難從容地從其中一種轉(zhuǎn)移到另一種上來?;谶@些原因, 我們需要一套全新的技術(shù)整合以上都這些技術(shù), 于是我們有了今天的WCF——Windows Communication Foundation。WCF建立一套框架,是我們通過一致的編程模式,使用不同的技術(shù)構(gòu)建我們的分布式應(yīng)用。 

雖然很早開始接觸WCF,但所學(xué)的總是零零碎碎?,F(xiàn)在開始系統(tǒng)地研究WCF,希望與大家一同分享我的一些所得, 同時(shí)希望能通過這樣的一個(gè)機(jī)會(huì)與大家一些探討WCF,不對(duì)的地方希望大家指正。

一開始我們先建立一個(gè)簡(jiǎn)單程序看WCF如何工作:

1 建立整個(gè)應(yīng)用的簡(jiǎn)單構(gòu)架

整個(gè)構(gòu)架如圖所示,這個(gè)Solution由5個(gè)Project組成:Artech.WCFService.Contract;  Artech.WCFService.Service;Artech.WCFService.Hosting;Artech.WCFService.Client;http://localhost/WCFService。

  • Artech.WCFService.Contract: Class Library Project,用來保存Contract(Service Contact、Message Contract、Data Contract), 之所以把Contract獨(dú)立出來的原因是考慮到他同時(shí)被Server端——Service本身和Service Hosting和Client端使用。(現(xiàn)在很多的參考書,包括MSDN都使用ServiceModel Metadata Utility Tool (Svcutil.exe)這樣的一個(gè)工具來訪問Service的Metadata Endpoint來生成我們的客戶段代碼,這些代碼就包括Service Contract(一般是一個(gè)Interface),實(shí)現(xiàn)了這個(gè)Contract的Proxy Class(一個(gè)集成自System.ServiceModel.CientBase的一個(gè)Class)和相應(yīng)的Configuration?!∵@個(gè)工具確實(shí)給我提供了很大的方便。但我不推薦使用這樣的方法(我天生不傾向?qū)τ谶@些代碼生成器),因?yàn)槲矣X得, 在Contract可得的情況下-比如Service和Client都是自己開發(fā),讓Service和Client實(shí)現(xiàn)的Contract是同一個(gè)Contract能夠保證一致性。這個(gè)Project引用System.ServiceModel DLL。
  •   Artech.WCFService.Service:Class Library Project,Service的業(yè)務(wù)邏輯, 這個(gè)Project引用Artech.WCFService.Contract Project和System.ServiceModel DLL。
  •   Artech.WCFService.Hosting:Console Application, 用于以Self-Hosting的方式Host Service。這個(gè)Project引用Artech.WCFService.Contract和Artech. Project WCFService.Service。Project和System.ServiceModel DLL。
  •   Artech.WCFService.Client:Console Application, 用以模擬現(xiàn)實(shí)中的調(diào)用Service的Clinet。這個(gè)Project引用Artech.WCFService.Contract Project 和System.ServiceModel DLL。
  •  http://localhost/WCFService: Web Site Project, 用于模擬如何把Service Host到IIS中。這個(gè)Project引用Artech.WCFService.Contract、Artech.WCFService.Service和System.ServiceModel DLL。

 

2 創(chuàng)建Service Contract

在這個(gè)例子中我們建立一個(gè)簡(jiǎn)單的案例,做一個(gè)計(jì)算器, 假設(shè)我們只要求他做簡(jiǎn)單的加法運(yùn)算就可以了。在Artech.WCFService.Contract添加一個(gè)interface,名稱叫做ICalculator。

 

using System.Collections.Generic;
using System.Text;
using System.ServiceModel;

namespace Artech.WCFService.Contract
{
    [ServiceContract]
    
public interface ICalculator
    {
        [OperationContract]
        
double Add(double x, double y);
    }
}

使一個(gè)Interface成為Service Contract的方法很簡(jiǎn)單,就是把ServiceContractAttribute應(yīng)用到這個(gè)interface上,并在代表單個(gè)Operation的方法上應(yīng)用OperationContractAttribute。這個(gè)使用Custom Attribute的編程模式被稱為聲明式的編程(Declarative)方式, 他在.NET Framework 1.1以前用到的地方還不是太多,在.NET Framework 2.0, 尤其是NET Framework 3.0中,這種方式已經(jīng)變得隨處可見了。

我們可以把Contract定義成一個(gè)Interface,也可以把它定義到一個(gè)Class中——這個(gè)Class中既包涵Service本身又作為一個(gè)Contract而存在。但我推薦使用第一種方法——Serive和Contract相分離。

在WCF中,Contract的功能實(shí)際上就定義一個(gè)Service包含哪些可用的Operation, 以及的每個(gè)Opertaion的方法簽名。從消息交換(Message Exchange)的角度講,Contract定義了調(diào)用相應(yīng)的Serive采取的消息交換的模式(Message Exchange Pattern - MEP),我們經(jīng)常使用的MEP包括三種:Oneway, Request/Response,和Duplex。因?yàn)檎{(diào)用Service的過程實(shí)際就是消息交換的過程, 以常見的Request/Response為例。Client調(diào)用某個(gè)方面遠(yuǎn)程訪問Service,所有的輸入?yún)?shù)被封裝到Request Soap Message并被發(fā)送到Service端, Service端監(jiān)聽到這個(gè)Soap Request,創(chuàng)建相應(yīng)的Service Object并調(diào)用相關(guān)的操作,最后將Result(這可以是Return Value,Reference Parameter和Output Parameter)封裝成Soap Message發(fā)送回Client端。這里需要注意,如果采用的是Request/Response的模式,即使相應(yīng)的操作沒有Return Value,Reference Parameter和Output Parameter(它被標(biāo)記為void),Service仍然會(huì)返回一個(gè)空的Soap Message給Client端。

3 創(chuàng)建Service

前面我們已經(jīng)創(chuàng)建了我的Artech.WCFService.Contract。其實(shí)我們從Contract這個(gè)單詞上講, 它就是一種契約,一種承諾。 他表明在上面簽了字你就的履行Contract上義務(wù)。Service就是這樣一個(gè)需要履行Contract義務(wù)的人。在這個(gè)例子中, Contract以Interface的方式定義的一些Operation。作為Service, 在Contract上簽字的方式就是實(shí)現(xiàn)這樣的一個(gè)Interface。 下面的Service得到code, 很簡(jiǎn)單。

using System;

using System.Collections.Generic;

using System.Text;

using System.ServiceModel;

using Artech.WCFService.Contract;

 

namespace Artech.WCFService.Service

{

    
public class CalculatorService:ICalculator

    {

        
#region ICalculator Members

 

        
public double Add(double x, double y)

        {

            
return x + y;

        }

 

        
#endregion

    }

}


4.Hosting Service

就像Remoting一樣,我們繼承自System.MarshalByRefObject 的對(duì)象必須Host到某一個(gè)運(yùn)行的進(jìn)程中, 他才開始監(jiān)聽來自Client端的請(qǐng)求,當(dāng)Client才能通過Proxy遠(yuǎn)程的調(diào)用,Remoting Infrastructure監(jiān)聽到來自Client端的請(qǐng)求,他會(huì)激活相應(yīng)的remote Object(我們只考慮Server Activate Object——SAO)。實(shí)際上對(duì)于WCF Service也需要一個(gè)Host環(huán)境才有其發(fā)揮作用的舞臺(tái)。就像Remoting一樣,你可以使用任何一種Managed Application——Console Application、WinForm Application、ASP.NET Application——作為它的Host環(huán)境?!∧闵踔量梢杂盟麳ost到Windows Service中和IIS中(后面我將會(huì)講到如何做)。

我們知道WCF中,Client端和Service端是通過Endpoint來通信的,Endpoint有包含3個(gè)部分,經(jīng)典地稱為ABC.

A代表Address,它包含一個(gè)URI,它指明Service存在于網(wǎng)絡(luò)的某個(gè)地方,也就是說它為Client斷指明在什么地方去找到這個(gè)Service。很多人認(rèn)識(shí)Address僅僅只是一個(gè)具有Identity的URI,實(shí)際上不然, Address不止于此, 它還包含一些Header,這些信息在某些情況下對(duì)于如何尋址有很大的意義(比如在client的最終Service之間還有一些Intermediary節(jié)點(diǎn)的情況下)。 在.NET中, Address用System.ServiceModel.EndpointAddress 來表示。

B代表Binding,Binding封裝了所有Client和Service段消息交換的通信細(xì)節(jié)。比如他定義了通信應(yīng)該采用的Transport-比如我們是因該采用Http, TCP,Named Pipe或者是MSMQ;通信的內(nèi)容應(yīng)該采取怎樣的編碼——比如是Text/XML,Binary還是MTOM。以上這些都得一個(gè)Binding所必須定義的內(nèi)容, 此外,Binding 還可以定義一些其他的有關(guān)通信的內(nèi)容, 比如Security,Reliable Messaging, Session, Transaction等等。正因?yàn)锽inding對(duì)于通信的重要性,只有Service端的Binding和Client的Binding相互匹配的時(shí)候,他們之間在可以相互通信。如何使Client Binding 匹配Service Binding呢?最簡(jiǎn)單也是最直接的方法就是使用相同的Binding。WCF為我們定義了一系列的System Defined Binding,這些Binding在Transport,Interoperability,Security,Session Support,以及Transaction Support方面各有側(cè)重。基本上WCF為我們定義的這些Binding 已經(jīng)夠我們使用的了,如果,實(shí)在滿足不了要求, 你還可以建立自己的Custom Binding。

C 代表Contract這在上面已經(jīng)提及,這里不再累贅。

Host的本質(zhì)就是把一個(gè)Service 置于一個(gè)運(yùn)行中的進(jìn)程中,并以Endpoint的形式暴露出來,并開始監(jiān)聽來自Client端的請(qǐng)求。這里值得注意的是,同一個(gè)Service可以注冊(cè)若干不同的Endpoint,這樣不同的Client就可以以不同的方式來訪問同一個(gè)Service.比如,同一個(gè)Intranet的Client可以以TCP的方式訪問Service,另一個(gè)存在已Internet中的Client則只能以Http的方式訪問。

 

using System;

using System.Collections.Generic;

using System.Text;

using System.ServiceModel;

using Artech.WCFService.Contract;

using Artech.WCFService.Service;

using System.ServiceModel.Description;

 

namespace Artech.WCFService.Hosting

{

    
class Program

    {

        
static void Main(string[] args)

        {

            HostingServiceViaCode();

        }

 

        

        
static void HostingServiceViaCode()

        { 

            
//Specify the base Address

            Uri baseUri 
= new Uri("http://localhost:8080/calculatorService");

            
//create a new ServiceHost object and specify the corresponding Service and base Address

            
//It is recommended to apply the using pattern to make sure the sevice host can be closed properly. 

            
using (ServiceHost calculatorServiceHost = new ServiceHost(typeof(CalculatorService), baseUri))

            {

                
//Create a Binding for Endpoint.

                BasicHttpBinding Binding 
= new BasicHttpBinding();

                
//Create a Service Endpoint by specify the Address(it is absolute or relative path based on the base Address, the empty string indicates the Address equals base Address),

                
//Binding(the basicHttpBinding created) and Contrace(it is now the type info of the contract interface)

                calculatorServiceHost.AddServiceEndpoint(
typeof(ICalculator), Binding, string.Empty);

 

                
//Such a segment of code snip shows how to make the metadata exposed to the outer world by setting the Service metadata behavior

                
//Find the Service metadata behavior if exists, otherwize return null.

                ServiceMetadataBehavior behavior 
= calculatorServiceHost.Description.Behaviors.Find<ServiceMetadataBehavior>();

 

                
//If the Service metadata behavior has not to added to the Service. we will create a new one and evaluate the HttpGetEnabled&HttpGetUrl to make outer world can retrieve to metadata.

                
if (behavior == null)

                {

                    behavior 
= new ServiceMetadataBehavior();

                    behavior.HttpGetEnabled 
= true;

                    
//HttpGetUrl is absolute or relative based on base Address

                    behavior.HttpGetUrl 
= baseUri;

                    
//We must add the new behavior created to the behavior collection, otherwize it will never take effect.

                    calculatorServiceHost.Description.Behaviors.Add(behavior);

                }

                
//if the metadata behavior exists in the behavior collection, we just need to evaluate the HttpGetEnabled&HttpGetUrl

                
else

                {

                    behavior.HttpGetEnabled 
= true;

                    behavior.HttpGetUrl 
= baseUri;

                }

 

                
//Add the opened event handler to make a friendly message displayed after opening the Service host indicating the Service begin to listen to request from Clients.

                calculatorServiceHost.Opened 
+= delegate { Console.WriteLine("Calculator Service begin to listen via the Address:{0}", calculatorServiceHost.BaseAddresses[0].ToString()); };

                
//Open the Service host make it begin to listen to the Clients.

                calculatorServiceHost.Open();

 

                Console.Read();

            }

        }

    }

}

 

 

我們現(xiàn)在可以單獨(dú)運(yùn)行Hosting Projet,以下是運(yùn)行后的截圖。


當(dāng)Hosting啟動(dòng)之后,由于我們?yōu)樗_啟了Http Enabled,Hosting為專門創(chuàng)建一個(gè)Metadata Endpoint,通過訪問這個(gè)Endpoint,我們可獲取Service相關(guān)的Metadata。像ServiceModel Metadata Utility Tool (Svcutil.exe) 這得工具就是通過獲取Metadata來幫我們生成相應(yīng)的客戶端代碼和配置文件。當(dāng)Hosting被啟動(dòng)之后,我們可以在IE中輸入Metadata Endpoint的Address來測(cè)試Service的可訪問性。如下圖。


以上我們完全以代碼的方式Host一個(gè)已經(jīng)創(chuàng)建的Service。在這個(gè)例子中,Endpoint的所有信息都在代碼中指定。實(shí)際上真正的項(xiàng)目開發(fā)之中,我們基本上不采用這樣的方法。這是因?yàn)椋琒ervice的Addresss以及Binding 信息在開發(fā)階段和部署階段往往是不一樣的。所以我們通常的做法是把這些信息放在Config文件中,這樣我們可以根據(jù)實(shí)際的需求改變存儲(chǔ)于Config文件中的信息,比如我們把Service移植上另外的位置,我們只要改變Endpoint的Address配置信息就可以了; 再比如,我們把原來存在Intranet的Service放到Internet上,原來可能基于TCP的Binding必須改成基于Http的Binding,在這種情況下,我們依然可修改配置文件就可以了,這樣的改動(dòng)通常是很小的,并且修改了配置文件之后我們不需要對(duì)現(xiàn)有的代碼進(jìn)行重編譯和重部署,它們可以其實(shí)生效。下面我們來看看如何創(chuàng)建基于Configuration的Hosting。

首先,在Artech.WCFService.Hosting中創(chuàng)建App.config,并編寫如下結(jié)構(gòu)的配置信息。

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<system.ServiceModel>

    
<Services>

      

 

      
<Service name="Artech.WCFService.Service.CalculatorService" behaviorConfiguration=" calculatorBehavior">

        
<host>

          
<baseAddresses>

            
<add baseAddress="http://localhost:8888/GeneralCalculator"/>

          
</baseAddresses>

        
</host>

        
<Endpoint Address="" Binding ="basicHttpBinding" contract="Artech.WCFService.Contract.ICalculator"></Endpoint>

      
</Service>

    
</Services>

 

    
<behaviors>

      
<ServiceBehaviors>

        
<behavior name="calculatorBehavior">

          
<ServiceMetadata httpGetEnabled="true" httpGetUrl=""/>

      
</ServiceBehaviors>

    
</behaviors>

    

 
</system.ServiceModel>

</configuration>


然后我們相應(yīng)地改變我們的Hosting代碼,現(xiàn)在由于我們的Endpoint的信息都放在我們的配置文件中,所以我們可以較大的簡(jiǎn)化我們的Hosting 代碼。

using System;

using System.Collections.Generic;

using System.Text;

using System.ServiceModel;

using Artech.WCFService.Contract;

using Artech.WCFService.Service;

using System.ServiceModel.Description;

 

namespace Artech.WCFService.Hosting

{

    
class Program

    {

        
static void Main(string[] args)

        {

            
using (ServiceHost calculatorServiceHost = new ServiceHost(typeof(CalculatorService)))

            {

                calculatorServiceHost.Opened 
+= delegate { Console.WriteLine("Calculator Service begin to listen via the Address:{0}", calculatorServiceHost.BaseAddresses[0].ToString()); };

                calculatorServiceHost.Open();

                Console.Read();

            }

        }

    }

}


當(dāng)calculatorServiceHost.Open();被調(diào)用之后,系統(tǒng)會(huì)查看calculatorServiceHost對(duì)應(yīng)的Service的類型,結(jié)果發(fā)現(xiàn)它的類型是Artech.WCFService.Service. CalculatorService(在ServiceHost被創(chuàng)建時(shí)最為第一個(gè)傳入?yún)?shù));然后會(huì)在config文件中Services部分中找name屬性和這個(gè)Service type相匹配,找到之后,把它host進(jìn)來,然后添加它在配置文件中定義的所有的Endpoint,并設(shè)置在配置文件中定義的所有Binding 以及 Behavior。

現(xiàn)在我們運(yùn)行Hosting,將會(huì)得到同上面一樣的結(jié)果。同樣,在IE中輸入Metadata Endpoint的Address,也會(huì)看到上解圖一樣的顯示。

5.創(chuàng)建Client

到現(xiàn)在為止,Service端的工作已經(jīng)完成,當(dāng)你啟動(dòng)Hosting的時(shí)候,一個(gè)可用的Service就已經(jīng)存在了。現(xiàn)在所做的事情是如何創(chuàng)建我們的客戶段程序去使用這個(gè)Service。幾乎所有的WCF的書,其中包括MSDN都是叫你如何使用Service Utility這樣的一個(gè)工具來幫你生成客戶端代碼和配置信息。為了讓我們能夠清晰的Client的整體內(nèi)容, 我們現(xiàn)在選擇手工的方式來編寫這樣的部分代碼。

幾乎所有分布式的調(diào)用都有這樣的一個(gè)概念,調(diào)用的具體實(shí)現(xiàn)被封裝在Server端,Client不可能也不需要了解這個(gè)具體實(shí)現(xiàn),它所關(guān)心的就是我如何去調(diào)用,也就是說Cient需要的不是Service的實(shí)現(xiàn),而是一個(gè)interface。WCF也是一樣,Client不需要了解Service的具體實(shí)現(xiàn),它只需要獲得Service 的Contract,已經(jīng)如何與Service通信就足夠了。說到Contract和通信,我們很自然地會(huì)想到Endpoint,不錯(cuò),Endpoint恰恰給我們提供這兩個(gè)方面的內(nèi)容。所以到現(xiàn)在我們可以這樣說,這樣在Client建立和Serivce端相匹配的Endpoint,Client就可以調(diào)用它所希望的Service。前面提到Endpoint包括三個(gè)部分, Address, Binding,Contract那我們現(xiàn)在來看看Client如何獲得這3要素的信息。

在System。Service。Model 命名空間里,定義了一個(gè)類abstract class ClientBase<TChannel>,給我們調(diào)用Service提供極大的便利。我們只要是我們的Client繼承這樣一個(gè)類,并為它指定Endpoint的三要素就一切OK了,下面我們來看看我們可以以那些方式來指定這些內(nèi)容

1. Conract:我們看到了ClientBase是一個(gè)Generic的類,我們?cè)趧?chuàng)建一個(gè)繼承自這個(gè)類的時(shí)候必須給它指定特定的TChannel.我們可以把Contract對(duì)應(yīng)的類型作為Client的generic類型。

2. Binding和Address:和Service端的Endpoint一樣,我們可以把相關(guān)的信息放在我們的Client端代碼里面,也可以放在Client的Config里面。那個(gè)這些數(shù)據(jù)如何應(yīng)用要我們創(chuàng)建的派生自ClientBase的類的對(duì)象上呢。其實(shí)很簡(jiǎn)單,ClientBase給我們定義了若干重載的構(gòu)造函數(shù),我們只要定義我們相應(yīng)的構(gòu)造函數(shù)應(yīng)簡(jiǎn)單地調(diào)用基類的構(gòu)造函數(shù)。下面列出了ClientBase定義的全部的構(gòu)造函數(shù)

protected ClientBase():這個(gè)構(gòu)造函數(shù)沒有任何的參數(shù),它用于Endpoint的信息全部存放于Config

protected ClientBase(InstanceContext callbackInstance):指定一個(gè)Callback instance用于Service回調(diào)Client代碼,這用Deplex Communication。

protected ClientBase(string EndpointConfigurationName):指定一個(gè)ID,它標(biāo)識(shí)configuration 文件中定義的某一個(gè)Endpoint。這個(gè)方法在使用不同的Endpoint調(diào)用同一個(gè)Service的情況下用到的比較多。

  • ClientBase(Binding Binding, EndpointAddress remoteAddress);顯示的指定Binding 和Address
  • ClientBase(InstanceContext callbackInstance, string EndpointConfigurationName)
  • ClientBase(string EndpointConfigurationName, EndpointAddress remoteAddress)
  • ClientBase(string EndpointConfigurationName, string remoteAddress)
    ClientBase(InstanceContext callbackInstance, Binding Binding, EndpointAddress remoteAddress)
  • ClientBase(InstanceContext callbackInstance, string EndpointConfigurationName, EndpointAddress remoteAddress)
  • ClientBase(InstanceContext callbackInstance, string EndpointConfigurationName, string remoteAddress)

介紹完ClientBase后, 我們來創(chuàng)建我們自己的CalculatorClient。下面的相應(yīng)的Code

using System;

using System.Collections.Generic;

using System.Text;

using System.ServiceModel;

using System.ServiceModel.Channels;

using Artech.WCFService.Contract;

 

namespace Artech.WCFService.Client

{

    
class CalculatorClient:ClientBase<ICalculator>,ICalculator

 

    {

        
internal CalculatorClient()

            : 
base()

        { }

 

        
#region ICalculator Members

 

        
public double Add(double x, double y)

        {

            
return this.Channel.Add(x, y);

        }

 

        
#endregion

    }

}


上面的例子中我們僅僅定義了一個(gè)無參的構(gòu)造函數(shù),因?yàn)槲覀儠?huì)把所有的Endpoint信息放在Config文件里面:

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

 
<system.ServiceModel>

    
<Client>

            
<Endpoint Address="http://localhost:8080/WCFService/CalculatorService " Binding="basicHttpBinding" contract="Artech.WCFService.Contract.ICalculator" />

          
</Client>    

 
</system.ServiceModel>

</configuration>

然后我們?cè)貱lient Project中的Program class里面通過這樣的方式調(diào)用CalculatorService;

using System;

using System.Collections.Generic;

using System.Text;

using System.ServiceModel;

using System.ServiceModel.Channels;

using Artech.WCFService.Contract;

 

namespace Artech.WCFService.Client

{

    
class Program

    {

        
static void Main(string[] args)

        {

            
try

            {

                  
using (CalculatorClient caluclator = new CalculatorClient())

                    {

                                Console.WriteLine(
"Begin to invocate the calculator Service
");

                                Console.WriteLine(
"x + y = {2} where x = {0} and y = {1}"12, caluclator.Add(12));

 

                                Console.Read();

                      }

                }

            }

 

            
catch (Exception ex)

            {

                Console.WriteLine(
"StackTrace:{0}", ex.StackTrace);

                Console.WriteLine(
"Message:{0}", ex.Message);

                Console.Read();

            }            

        }        

    }

}
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Endpoint Overview
WCF開發(fā)日志 -- WCF啟動(dòng)多個(gè)服務(wù)
WCF配置文件全攻略
WCF配置文件 - 落霞與孤騖齊飛,秋水共長(zhǎng)天一色
WCF服務(wù)編程
[WCF 4.0新特性] 標(biāo)準(zhǔn)終結(jié)點(diǎn)與無(.SVC)文件服務(wù)激活
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服