文章主體可分為兩部分,分別介紹服務(wù)器端和客戶(hù)端的編程實(shí)現(xiàn)。細(xì)分的話(huà),可以分為六項(xiàng)任務(wù)。
· 服務(wù)器端
這是創(chuàng)建基本 Windows Communication Foundation (WCF) 服務(wù)和可以使用該服務(wù)的客戶(hù)端所需的六項(xiàng)任務(wù)中的第一項(xiàng)任務(wù)。
創(chuàng)建基本 WCF 服務(wù)時(shí),第一項(xiàng)任務(wù)是為與外界共享的服務(wù)創(chuàng)建協(xié)定,并在其中描述如何與該服務(wù)進(jìn)行通信。
具體步驟為:
1、 創(chuàng)建新的控制臺(tái)應(yīng)用程序項(xiàng)目。 在“新建項(xiàng)目”對(duì)話(huà)框中,選中“Visual Basic”或“Visual C#”,并選擇“控制臺(tái)應(yīng)用程序”模板,并命名為Service。 使用默認(rèn)的位置。
2、將默認(rèn)的Service 命名空間更改為 Microsoft.ServiceModel.Samples。
3、為項(xiàng)目提供對(duì) System.ServiceModel 命名空間的引用:右擊“解決方案資源管理器”中的“Service”項(xiàng)目,選擇“添加引用”項(xiàng),在彈出的對(duì)話(huà)框中的“.NET”選項(xiàng)卡里的“組件名稱(chēng)”中選擇“System.ServiceModel”,然后單擊“確定”。
下面是編程步驟:
1、為 System.ServiceModel 命名空間添加一個(gè) using 語(yǔ)句。
using System.ServiceModel;
2、創(chuàng)建一個(gè)新的ICalculator 接口,并將 ServiceContractAttribute 屬性應(yīng)用于該接口,并將 Namespace 值設(shè)置為“http://Microsoft.ServiceModel.Samples”。 此命名空間指定該服務(wù)在計(jì)算機(jī)上的路徑,并構(gòu)成該服務(wù)的基址部分。 請(qǐng)注意,在通過(guò)采用方括號(hào)表示法的屬性來(lái)批注接口或類(lèi)時(shí),該屬性類(lèi)可以從其名稱(chēng)中去掉“Attribute”部分。
[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
public interface ICalculator
3、在接口中創(chuàng)建方法聲明,并將 OperationContractAttribute 屬性應(yīng)用于每個(gè)要作為公共 WCF 協(xié)定的一部分公開(kāi)的方法。
[OperationContract]
double Add(double n1, double n2);
[OperationContract]
double Subtract(double n1, double n2);
[OperationContract]
double Multiply(double n1, double n2);
[OperationContract]
double Divide(double n1, double n2);
下面是創(chuàng)建服務(wù)協(xié)定的完整代碼段:
using System;
// Add the using statement for the Sytem.ServiceModel namespace
using System.ServiceModel;
namespace Microsoft.ServiceModel.Samples
{
// Define a service contract.
[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
public interface ICalculator
{
// Create the method declaration for the contract.
[OperationContract]
double Add(double n1, double n2);
[OperationContract]
double Subtract(double n1, double n2);
[OperationContract]
double Multiply(double n1, double n2);
[OperationContract]
double Divide(double n1, double n2);
}
}
........(整個(gè)服務(wù)器端的代碼未完,等講完任務(wù)三時(shí),再給出完整代碼)
1、創(chuàng)建一個(gè)新 CalculatorService 類(lèi),該類(lèi)從用戶(hù)定義的 ICalculator 接口繼承而來(lái)并實(shí)現(xiàn)該接口定義的協(xié)定功能。
public class CalculatorService : ICalculator
2、實(shí)現(xiàn)每個(gè)算術(shù)運(yùn)算符的功能。
public double Add(double n1, double n2)
{
double result = n1 + n2;
Console.WriteLine("Received Add({0},{1})", n1, n2);
// Code added to write output to the console window.
Console.WriteLine("Return: {0}", result);
return result;
}
public double Subtract(double n1, double n2)
{
double result = n1 - n2;
Console.WriteLine("Received Subtract({0},{1})", n1, n2);
Console.WriteLine("Return: {0}", result);
return result;
}
public double Multiply(double n1, double n2)
{
double result = n1 * n2;
Console.WriteLine("Received Multiply({0},{1})", n1, n2);
Console.WriteLine("Return: {0}", result);
return result;
}
public double Divide(double n1, double n2)
{
double result = n1 / n2;
Console.WriteLine("Received Divide({0},{1})", n1, n2);
Console.WriteLine("Return: {0}", result);
return result;
}
在創(chuàng)建和實(shí)現(xiàn)了服務(wù)協(xié)定后,下一步是運(yùn)行該服務(wù)。 運(yùn)行服務(wù)由三個(gè)步驟組成:配置、承載和打開(kāi)服務(wù)。
為服務(wù)的基址創(chuàng)建 Uri 實(shí)例。 此 URI 指定 HTTP 方案、本地計(jì)算機(jī)、端口號(hào) 8000,以及服務(wù)協(xié)定中為服務(wù)命名空間指定的服務(wù)路徑ServiceModelSample/Services。
Uri baseAddress = new Uri("http://localhost:8000/ServiceModelSamples/Service");
1. 創(chuàng)建一個(gè)新的 ServiceHost 實(shí)例以承載服務(wù)。 必須指定實(shí)現(xiàn)服務(wù)協(xié)定和基址的類(lèi)型。 對(duì)于此示例,我們將基址指定為http://localhost:8000/ServiceModelSamples/Services,并將 CalculatorService 指定為實(shí)現(xiàn)服務(wù)協(xié)定的類(lèi)型。
ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), baseAddress); |
2. 添加一個(gè)捕獲 CommunicationException 的 try-catch 語(yǔ)句,并在接下來(lái)的三個(gè)步驟中將該代碼添加到 try 塊中。
3. 添加公開(kāi)服務(wù)的終結(jié)點(diǎn)。 為此,必須指定終結(jié)點(diǎn)公開(kāi)的協(xié)議、綁定和終結(jié)點(diǎn)的地址。 此例中,將 ICalculator 指定為協(xié)定,將 WSHttpBinding 指定為綁定,并將 CalculatorService 指定為地址。 在這里請(qǐng)注意,我們指定的是相對(duì)地址。 終結(jié)點(diǎn)的完整地址是基址和終結(jié)點(diǎn)地址的組合。 在此例中,完整地址是 http://localhost:8000/ServiceModelSamples/Services/CalculatorService。
selfHost.AddServiceEndpoint( typeof(ICalculator), new WSHttpBinding(), "CalculatorService"); |
4. 啟用元數(shù)據(jù)交換。 為此,必須添加服務(wù)元數(shù)據(jù)行為。 首先創(chuàng)建一個(gè) ServiceMetadataBehavior 實(shí)例,將 HttpGetEnabled 屬性設(shè)置為 true,然后為服務(wù)添加新行為。
ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); smb.HttpGetEnabled = true; selfHost.Description.Behaviors.Add(smb); |
5. 打開(kāi) ServiceHost 并等待傳入消息。 用戶(hù)按 Enter 鍵時(shí),關(guān)閉 ServiceHost。
selfHost.Open();
Console.WriteLine("The service is ready.");
Console.WriteLine("Press <ENTER> to terminate service.");
Console.WriteLine();
Console.ReadLine();
// Close the ServiceHostBase to shutdown the service.
selfHost.Close();
下面是服務(wù)器端(即“Service”項(xiàng)目中program.cs文件中)的完整程序代碼:
using System;
using System.ServiceModel;
using System.ServiceModel.Description;
namespace Microsoft.ServiceModel.Samples
{
// Define a service contract.
[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
public interface ICalculator
{
[OperationContract]
double Add(double n1, double n2);
[OperationContract]
double Subtract(double n1, double n2);
[OperationContract]
double Multiply(double n1, double n2);
[OperationContract]
double Divide(double n1, double n2);
}
// Service class that implements the service contract.
// Added code to write output to the console window.
public class CalculatorService : ICalculator
{
public double Add(double n1, double n2)
{
double result = n1 + n2;
Console.WriteLine("Received Add({0},{1})", n1, n2);
Console.WriteLine("Return: {0}", result);
return result;
}
public double Subtract(double n1, double n2)
{
double result = n1 - n2;
Console.WriteLine("Received Subtract({0},{1})", n1, n2);
Console.WriteLine("Return: {0}", result);
return result;
}
public double Multiply(double n1, double n2)
{
double result = n1 * n2;
Console.WriteLine("Received Multiply({0},{1})", n1, n2);
Console.WriteLine("Return: {0}", result);
return result;
}
public double Divide(double n1, double n2)
{
double result = n1 / n2;
Console.WriteLine("Received Divide({0},{1})", n1, n2);
Console.WriteLine("Return: {0}", result);
return result;
}
}
class Program
{
static void Main(string[] args)
{
// Create a URI to serve as the base address.
Uri baseAddress = new Uri("http://localhost:8000/ServiceModelSamples/Service");
// Create ServiceHost
ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), baseAddress);
try
{
// Add a service endpoint.
selfHost.AddServiceEndpoint(
typeof(ICalculator),
new WSHttpBinding(),
"CalculatorService");
// Enable metadata exchange.
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
selfHost.Description.Behaviors.Add(smb);
// Start (and then stop) the service.
selfHost.Open();
Console.WriteLine("The service is ready.");
Console.WriteLine("Press <ENTER> to terminate service.");
Console.WriteLine();
Console.ReadLine();
// Close the ServiceHostBase to shutdown the service.
selfHost.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine("An exception occurred: {0}", ce.Message);
selfHost.Abort();
}
}
}
}
若要運(yùn)行服務(wù),啟動(dòng)項(xiàng)目文件夾下bin目錄中的 Service.exe即可。
前面三篇文章是講服務(wù)器端的部署和運(yùn)行,下面講講客戶(hù)端如何配置和使用。
1. 通過(guò)執(zhí)行以下步驟,在 Visual Studio 2005 中為客戶(hù)端創(chuàng)建新項(xiàng)目:
1. 在包含該服務(wù)(之前文章所述的服務(wù))的同一解決方案中的“解決方案資源管理器”(位于右上角)中,右擊當(dāng)前解決方案,然后選擇“添加新項(xiàng)目”。
2. 在“添加新項(xiàng)目”對(duì)話(huà)框中,選擇“Visual Basic”或“Visual C#”,選擇“控制臺(tái)應(yīng)用程序”模板,然后將其命名為 Client。 使用默認(rèn)的位置。
3. 單擊“確定”。
2. 為項(xiàng)目提供對(duì) System.ServiceModel 命名空間的引用:在“解決方案資源管理器”中右擊“Service”項(xiàng)目,從“.NET”選項(xiàng)卡上的“組件名稱(chēng)”列中選擇“System.ServiceModel”,然后單擊“確定”。
3. 為 System.ServiceModel 命名空間添加 using 語(yǔ)句:using System.ServiceModel;
4. 啟動(dòng)在前面的步驟中創(chuàng)建的服務(wù)。(即打開(kāi)在服務(wù)器項(xiàng)目中生成的Service.exe可執(zhí)行文件)
5. 通過(guò)執(zhí)行以下步驟,使用適當(dāng)?shù)拈_(kāi)關(guān)運(yùn)行Service Model Metadata Utility Tool (SvcUtil.exe) 以創(chuàng)建客戶(hù)端代碼和配置文件:
1. 通過(guò)選擇“開(kāi)始”菜單中的“Microsoft Windows SDK”項(xiàng)下的“CMD Shell”,啟動(dòng) Windows SDK 控制臺(tái)會(huì)話(huà)。
2. 導(dǎo)航到要放置客戶(hù)端代碼的目錄。 如果使用默認(rèn)設(shè)置創(chuàng)建 Client 項(xiàng)目,則目錄為 C:Documents and Settings<用戶(hù)名>DocumentsVisual Studio 2008ProjectsServiceClient。
3. 將命令行工具Service Model Metadata Utility Tool (SvcUtil.exe) 與適當(dāng)?shù)拈_(kāi)關(guān)一起使用以創(chuàng)建客戶(hù)端代碼。 下面的示例生成服務(wù)的代碼文件和配置文件。
svcutil.exe /language:cs /out:generatedProxy.cs /config:app.config http://localhost:8000/ServiceModelSamples/service
6. 在 Visual Studio 中將生成的代理添加到 Client 項(xiàng)目中,方法是在“解決方案資源管理器”中右擊“Client”并選擇“添加現(xiàn)有項(xiàng)”。 然后選擇在上一步中生成的 generatedProxy.cs 文件。
在 Visual Studio 中,將在前一過(guò)程中生成的 App.config 配置文件添加到客戶(hù)端項(xiàng)目中。 在“解決方案資源管理器”中右擊該客戶(hù)端,選擇“添加現(xiàn)有項(xiàng)”,然后從 C:Documents and Settings<用戶(hù)名>DocumentsVisual Studio 2008ProjectsServiceClientin 目錄中選擇 App.config 配置文件。
將app.config添加到項(xiàng)目中后,就算是完成了wcf客戶(hù)端的配置。因?yàn)榫唧w的配置信息,我們?cè)谑褂胹vcutil.exe工具時(shí),它就幫我們配置好并寫(xiě)入了app.config文件。
1、為要調(diào)用的服務(wù)的基址創(chuàng)建 EndpointAddress 實(shí)例,然后創(chuàng)建 WCF Client 對(duì)象。
//Create an endpoint address and an instance of the WCF Client.
EndpointAddress epAddress = new EndpointAddress("http://localhost:8000/ServiceModelSamples/Service/CalculatorService");
CalculatorClient client = new CalculatorClient(new WSHttpBinding(), epAddress);
2、從 Client 內(nèi)調(diào)用客戶(hù)端操作。
// Call the service operations.
// Call the Add service operation.
double value1 = 100.00D;
double value2 = 15.99D;
double result = client.Add(value1, value2);
Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);
// Call the Subtract service operation.
value1 = 145.00D;
value2 = 76.54D;
result = client.Subtract(value1, value2);
Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result);
// Call the Multiply service operation.
value1 = 9.00D;
value2 = 81.25D;
result = client.Multiply(value1, value2);
Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result);
// Call the Divide service operation.
value1 = 22.00D;
value2 = 7.00D;
result = client.Divide(value1, value2);
Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);
3、在 WCF 客戶(hù)端上調(diào)用 Close。
// Closing the client gracefully closes the connection and cleans up resources.
client.Close();
下面是客戶(hù)端的完整代碼:
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
namespace ServiceModelSamples
{
class Client
{
static void Main()
{
//Step 1: Create an endpoint address and an instance of the WCF Client.
EndpointAddress epAddress = new EndpointAddress("http://localhost:8000/ServiceModelSamples/Service/CalculatorService");
CalculatorClient client = new CalculatorClient(new WSHttpBinding(), epAddress);
// Step 2: Call the service operations.
// Call the Add service operation.
double value1 = 100.00D;
double value2 = 15.99D;
double result = client.Add(value1, value2);
Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);
// Call the Subtract service operation.
value1 = 145.00D;
value2 = 76.54D;
result = client.Subtract(value1, value2);
Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result);
// Call the Multiply service operation.
value1 = 9.00D;
value2 = 81.25D;
result = client.Multiply(value1, value2);
Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result);
// Call the Divide service operation.
value1 = 22.00D;
value2 = 7.00D;
result = client.Divide(value1, value2);
Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);
//Step 3: Closing the client gracefully closes the connection and cleans up resources.
client.Close();
Console.WriteLine();
Console.WriteLine("Press <ENTER> to terminate client.");
Console.ReadLine();
}
}
}
若要啟動(dòng)客戶(hù)端,請(qǐng)?jiān)凇伴_(kāi)始”菜單中的“Microsoft Windows SDK”項(xiàng)下選擇“CMD Shell”,從而啟動(dòng) Windows SDK 控制臺(tái)會(huì)話(huà)。 定位至 C:Documents and Settings<用戶(hù)名>DocumentsVisual Studio 2008ProjectsServiceClientobjDebug 目錄,鍵入 client,然后按Enter。 操作請(qǐng)求和響應(yīng)將出現(xiàn)在客戶(hù)端控制臺(tái)窗口中,如下所示。
Add(100,15.99) = 115.99
Subtract(145,76.54) = 68.46
Multiply(9,81.25) = 731.25
Divide(22,7) = 3.14285714285714
Press <ENTER> to terminate client.
網(wǎng)站題目:WCF入門(mén)-創(chuàng)新互聯(lián)
網(wǎng)站地址:http://sd-ha.com/article20/shpjo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開(kāi)發(fā)、網(wǎng)站制作、網(wǎng)站內(nèi)鏈、電子商務(wù)、動(dòng)態(tài)網(wǎng)站、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)