欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

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

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

 

寫(xiě)在前面

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

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

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

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

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

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

1 建立整個(gè)應用的簡(jiǎn)單構架

整個(gè)構架如圖所示,這個(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,用來(lái)保存Contract(Service Contact、Message Contract、Data Contract), 之所以把Contract獨立出來(lái)的原因是考慮到他同時(shí)被Server端——Service本身和Service Hosting和Client端使用。(現在很多的參考書(shū),包括MSDN都使用ServiceModel Metadata Utility Tool (Svcutil.exe)這樣的一個(gè)工具來(lái)訪(fǎng)問(wèn)Service的Metadata Endpoint來(lái)生成我們的客戶(hù)段代碼,這些代碼就包括Service Contract(一般是一個(gè)Interface),實(shí)現了這個(gè)Contract的Proxy Class(一個(gè)集成自System.ServiceModel.CientBase的一個(gè)Class)和相應的Configuration?!∵@個(gè)工具確實(shí)給我提供了很大的方便。但我不推薦使用這樣的方法(我天生不傾向對于這些代碼生成器),因為我覺(jué)得, 在Contract可得的情況下-比如Service和Client都是自己開(kāi)發(fā),讓Service和Client實(shí)現的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, 用以模擬現實(shí)中的調用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è)計算器, 假設我們只要求他做簡(jiǎn)單的加法運算就可以了。在A(yíng)rtech.WCFService.Contract添加一個(gè)interface,名稱(chēng)叫做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應用到這個(gè)interface上,并在代表單個(gè)Operation的方法上應用OperationContractAttribute。這個(gè)使用Custom Attribute的編程模式被稱(chēng)為聲明式的編程(Declarative)方式, 他在.NET Framework 1.1以前用到的地方還不是太多,在.NET Framework 2.0, 尤其是NET Framework 3.0中,這種方式已經(jīng)變得隨處可見(jiàn)了。

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

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

3 創(chuàng )建Service

前面我們已經(jīng)創(chuàng )建了我的Artech.WCFService.Contract。其實(shí)我們從Contract這個(gè)單詞上講, 它就是一種契約,一種承諾?!∷砻髟谏厦婧灹俗帜憔偷穆男蠧ontract上義務(wù)。Service就是這樣一個(gè)需要履行Contract義務(wù)的人。在這個(gè)例子中, Contract以Interface的方式定義的一些Operation。作為Service, 在Contract上簽字的方式就是實(shí)現這樣的一個(gè)Interface?!∠旅娴腟ervice得到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 的對象必須Host到某一個(gè)運行的進(jìn)程中, 他才開(kāi)始監聽(tīng)來(lái)自Client端的請求,當Client才能通過(guò)Proxy遠程的調用,Remoting Infrastructure監聽(tīng)到來(lái)自Client端的請求,他會(huì )激活相應的remote Object(我們只考慮Server Activate Object——SAO)。實(shí)際上對于WCF Service也需要一個(gè)Host環(huán)境才有其發(fā)揮作用的舞臺。就像Remoting一樣,你可以使用任何一種Managed Application——Console Application、WinForm Application、ASP.NET Application——作為它的Host環(huán)境?!∧闵踔量梢杂盟麳ost到Windows Service中和IIS中(后面我將會(huì )講到如何做)。

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

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

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

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

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

 

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

            }

        }

    }

}

 

 

我們現在可以單獨運行Hosting Projet,以下是運行后的截圖。


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


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

首先,在A(yíng)rtech.WCFService.Hosting中創(chuàng )建App.config,并編寫(xiě)如下結構的配置信息。

<?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>


然后我們相應地改變我們的Hosting代碼,現在由于我們的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();

            }

        }

    }

}


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

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

5.創(chuàng )建Client

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

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

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

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

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

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

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

protected ClientBase(string EndpointConfigurationName):指定一個(gè)ID,它標識configuration 文件中定義的某一個(gè)Endpoint。這個(gè)方法在使用不同的Endpoint調用同一個(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后, 我們來(lái)創(chuàng )建我們自己的CalculatorClient。下面的相應的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è)無(wú)參的構造函數,因為我們會(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>

然后我們再Client Project中的Program class里面通過(guò)這樣的方式調用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();

            }            

        }        

    }

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

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久