ListBasedPublishSubscribe 範例說明以列表為基礎的 Publish-Subscribe 模式實作為 Windows Communication Foundation (WCF) 程式。
備註
此範例的安裝程式和建置指示位於本主題結尾。
以清單為基礎的 Publish-Subscribe 設計模式在 Microsoft 的《模式與實務》出版物中的《整合模式》一書中有詳細說明。 Publish-Subscribe 模式會將資訊傳遞給已訂閱資訊主題的收件者集合。 以清單為基礎的發佈/訂閱系統會維護一個訂閱者清單。 當有共用資訊時,清單上的每個訂閱者都會傳送一份複本。 此範例示範動態清單型發行-訂閱模式,其中用戶端可以視需要經常訂閱或取消訂閱。
以清單為基礎的 Publish-Subscribe 範例包含客戶端、服務和數據源程式。 可以有多個用戶端和一個以上的數據源程序執行。 用戶端訂閱服務、接收通知和取消訂閱。 數據源程式會將資訊傳送至服務,以便與所有目前的訂閱者共用。
在此範例中,用戶端和數據源是控制台程式(.exe 檔案),而服務是裝載在 Internet Information Services (IIS) 中的連結庫(.dll)。 用戶端和數據源活動會顯示在桌面上。
服務會使用雙工通訊。 服務 ISampleContract 合約會與 ISampleClientCallback 回調合約配對。 服務會執行訂閱和取消訂閱服務操作,客戶端可用以加入或離開訂閱者清單。 服務也會實作 PublishPriceChange 服務作業,數據源程式會呼叫此作業,以提供服務的新資訊。 用戶端程式會實作 PriceChange 服務作業,服務會呼叫此作業,以通知所有訂閱者價格變更。
// Create a service contract and define the service operations.
// NOTE: The service operations must be declared explicitly.
[ServiceContract(SessionMode=SessionMode.Required,
CallbackContract=typeof(ISampleClientContract))]
public interface ISampleContract
{
[OperationContract(IsOneWay = false, IsInitiating=true)]
void Subscribe();
[OperationContract(IsOneWay = false, IsTerminating=true)]
void Unsubscribe();
[OperationContract(IsOneWay = true)]
void PublishPriceChange(string item, double price,
double change);
}
public interface ISampleClientContract
{
[OperationContract(IsOneWay = true)]
void PriceChange(string item, double price, double change);
}
服務會使用 .NET Framework 事件做為機制,通知所有訂閱者有關新資訊。 當客戶端藉由呼叫 Subscribe 來聯結服務時,它會提供事件處理程式。 當用戶端離開時,將取消訂閱其事件處理程式。 當數據源呼叫服務以報告價格變更時,服務會觸發這個事件。 這會呼叫每個已註冊的用戶端所對應的服務實例,並使其事件處理程式執行。 每個事件處理程式都會透過其回呼函式,將資訊傳遞至其用戶端。
public class PriceChangeEventArgs : EventArgs
{
public string Item;
public double Price;
public double Change;
}
// The Service implementation implements your service contract.
[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]
public class SampleService : ISampleContract
{
public static event PriceChangeEventHandler PriceChangeEvent;
public delegate void PriceChangeEventHandler(object sender, PriceChangeEventArgs e);
ISampleClientContract callback = null;
PriceChangeEventHandler priceChangeHandler = null;
//Clients call this service operation to subscribe.
//A price change event handler is registered for this client instance.
public void Subscribe()
{
callback = OperationContext.Current.GetCallbackChannel<ISampleClientContract>();
priceChangeHandler = new PriceChangeEventHandler(PriceChangeHandler);
PriceChangeEvent += priceChangeHandler;
}
//Clients call this service operation to unsubscribe.
//The previous price change event handler is unregistered.
public void Unsubscribe()
{
PriceChangeEvent -= priceChangeHandler;
}
//Information source clients call this service operation to report a price change.
//A price change event is raised. The price change event handlers for each subscriber will execute.
public void PublishPriceChange(string item, double price, double change)
{
PriceChangeEventArgs e = new PriceChangeEventArgs();
e.Item = item;
e.Price = price;
e.Change = change;
PriceChangeEvent(this, e);
}
//This event handler runs when a PriceChange event is raised.
//The client's PriceChange service operation is invoked to provide notification about the price change.
public void PriceChangeHandler(object sender, PriceChangeEventArgs e)
{
callback.PriceChange(e.Item, e.Price, e.Change);
}
}
當您執行範例時,請啟動數個用戶端。 用戶端訂閱服務。 然後執行數據源程式,以將資訊傳送至服務。 服務會將信息傳遞給所有訂閱者。 您可以在每個用戶端控制臺上看到活動,確認已收到資訊。 在客戶端視窗中按 ENTER 鍵以關閉用戶端。
若要設定和建置範例
請確定您已針對 Windows Communication Foundation 範例 執行One-Time 安裝程式。
若要建置解決方案的 C# 或 Visual Basic .NET 版本,請遵循建置 Windows Communication Foundation 範例 中中的指示。
在同一台電腦上運行範例
輸入下列位址,測試您是否可以使用瀏覽器存取服務:
http://localhost/servicemodelsamples/service.svc。 應該在回應中顯示確認頁面。從語言特定的資料夾中的 \client\bin\ 執行 Client.exe。 用戶端活動會顯示在用戶端主控台視窗上。 啟動數個用戶端。
在 \datasource\bin\ 的語言特定資料夾下執行 Datasource.exe。 數據源活動會顯示在主控台視窗上。 一旦數據源將資訊傳送至服務,應該將它傳遞至每個用戶端。
如果客戶端、數據源和服務程式無法通訊,請參閱 WCF 範例的疑難解答秘訣。
要在多台電腦上執行範例
設定服務機器:
在服務機器上,建立名為 ServiceModelSamples 的虛擬目錄。 Windows Communication Foundation 範例One-Time 安裝程式 Setupvroot.bat 批處理文件可用來建立磁碟目錄和虛擬目錄。
將服務程式檔案從 %SystemDrive%\Inetpub\wwwroot\servicemodelsamples 複製到服務計算機上的 ServiceModelSamples 虛擬目錄。 請務必在 \bin 目錄中包含檔案。
測試您是否可以使用瀏覽器從用戶端電腦存取服務。
設定客戶端電腦:
將用戶端程式檔案從 \client\bin\ 資料夾的 [語言特定資料夾] 底下複製到用戶端電腦。
在每個用戶端組態檔中,變更端點定義的位址值,以符合您服務的新位址。 將任何對「localhost」的引用取代為位址中的完整域名。
設定資料來源電腦:
將資料源程式檔案從 \datasource\bin\ 資料夾的 [語言特定資料夾] 底下複製到資料源電腦。
在數據源組態檔中,變更端點定義的位址值,以符合您服務的新位址。 將任何對「localhost」的引用取代為位址中的完整域名。
在用戶端電腦上,以命令提示符啟動 Client.exe。
在數據源計算機上,從命令提示字元啟動 Datasource.exe。