- WCF 教程
- WCF - 首頁
- WCF - 概述
- WCF - 與 Web 服務的比較
- WCF - 開發人員工具
- WCF - 架構
- WCF - 建立 WCF 服務
- WCF - 託管 WCF 服務
- WCS - IIS 託管
- WCF - 自託管
- WCF - WAS 託管
- WCF - Windows 服務託管
- WCF - 使用 WCF 服務
- WCF - 服務繫結
- WCF - 例項管理
- WCF - 事務
- WCF - RIA 服務
- WCF - 安全性
- WCF - 異常處理
- WCF 資源
- WCF - 快速指南
- WCF - 有用資源
- WCF - 討論
WCF - 使用 WCF 服務
WCF 服務允許其他應用程式訪問或使用它們。WCF 服務可以透過多種方式使用,具體取決於託管型別。在這裡,我們將解釋逐步使用 WCF 服務的方法,適用於以下每種流行的託管選項:
- 使用在 IIS 5/6 中託管的 WCF 服務
- 使用自託管的 WCF 服務
- 使用在 Windows 啟用服務中託管的 WCF 服務
- 使用在 Windows 服務中託管的 WCF 服務
使用在 IIS 5/6 中託管的 WCF 服務
下面詳細討論了在 IIS 5/6 中託管的 WCF 服務的使用過程。此外,討論還包括如何建立代理和控制檯應用程式。
步驟 1 - 服務託管在 IIS 中後,我們必須在客戶端應用程式中使用它。在建立客戶端應用程式之前,我們需要為服務建立一個代理。客戶端應用程式使用此代理與服務進行互動。要建立代理,請執行 Visual Studio 2008 命令提示符。使用服務實用程式,我們可以建立代理類及其配置資訊。
svcutil https:///IISHostedService/Service.svc
執行此命令後,將在預設位置生成兩個檔案。
MyService.cs - WCF 服務的代理類
output.config - 關於服務的配置資訊
步驟 2 - 現在,我們將開始使用 Visual Studio 2008 建立控制檯應用程式(客戶端應用程式)。
步驟 3 - 新增引用“System.ServiceModel”;這是 WCF 的核心 dll。
步驟 4 - 建立一個代理類。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyServiceClient {
Class Program {
Static void Main(string[] args) {
// Creating Proxy for the MyService
ServiceClient Client = newServiceClient();
Console.WriteLine("Client calling the service...");
Console.WriteLine("Hello Ram");
Console.Read();
}
}
}
輸出如下所示:
使用自託管 WCF 服務
這裡,逐步解釋了使用自託管 WCF 服務的整個過程,並在必要時提供足夠的程式碼和螢幕截圖。
步驟 1 - 服務已託管,現在我們需要為客戶端實現代理類。建立代理的方法有很多。
使用 SvcUtil.exe,我們可以建立代理類及其帶有端點的配置檔案。
向客戶端應用程式新增服務引用。
實現 ClientBase<T> 類
在這三種方法中,實現 ClientBase<T> 是最佳實踐。如果您使用其他兩種方法,則每次在服務實現中進行任何更改時都需要建立一個代理類。但 ClientBase<T> 並非如此。它只在執行時建立代理,因此它會處理所有事情。
為此,建立一個包含 System.ServiceModel 和 MyCalculatorService 引用 的代理類。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using MyCalculatorService;
namespace MyCalculatorServiceProxy {
// WCF create proxy for ISimpleCalculator using ClientBase
Public class MyCalculatorServiceProxy :
ClientBase<ISimpleCalculator>,
ISimpleCalculator {
Public int Add(int num1, int num2) {
//Call base to do funtion
returnbase.Channel.Add(num1, num2);
}
}
}
現在,建立一個包含 System.ServiceModel 和 MyCalculatorServiceProxy 引用 的控制檯應用程式。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using MyCalculatorServiceProxy;
namespace MyCalculatorServiceClient {
classProgram {
Static void Main(string[] args) {
MyCalculatorServiceProxy.MyCalculatorServiceProxy proxy = newMyCalculatorServiceProxy.MyCalculatorServiceProxy();
Console.WriteLine("Client is running at " + DateTime.Now.ToString());
Console.WriteLine("Sum of two numbers. 5 + 5 =" + proxy.Add(5,5));
Console.ReadLine();
}
}
}
步驟 2 - 端點(與服務相同)資訊應新增到客戶端應用程式的配置檔案中。
<?xmlversion = "1.0"encoding = "utf-8" ?>
<configuration>
<system.serviceModel>
<client>
<endpoint address
="https://:8090/MyCalculatorServiceProxy/ISimpleCalculator"
binding = "wsHttpBinding" contract "MyCalculatorServiceProxy.ISimpleCalculator">
</endpoint>
</client>
</system.serviceModel>
</configuration>
步驟 3 - 在執行客戶端應用程式之前,您需要執行服務。以下是客戶端應用程式的輸出。
使用在 WAS 中託管的 WCF 服務
使用在 WAS 中託管的 WCF 服務是一個簡單的過程,只需幾個步驟即可完成。步驟如下:
- 將代理類和配置檔案新增到客戶端應用程式。
- 為 MathServiceClient 建立物件並呼叫方法。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespaceWASHostedClient {
classProgram {
staticvoid Main(string[] args) {
MathServiceClient client = newMathServiceClient();
Console.WriteLine("Sum of two number 5,6");
Console.WriteLine(client.Add(5, 6));
Console.ReadLine();
}
}
}
輸出如下所示。
使用在 Windows 服務中託管的 WCF 服務
下面詳細說明了如何在 Windows 服務中託管 WCF 服務的逐步過程,並提供程式碼和說明。
成功託管後,我們可以為服務建立代理類,並在客戶端應用程式中開始使用。這裡,它顯示了使用 IIS 託管型別。
新增 ServiceModel 的引用。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespaceWindowServiceClient {
classProgram {
staticvoid Main(string[] args) {
//Creating Proxy for the MyService
MyServiceClient client = newMyServiceClient();
Console.WriteLine("Client calling the service...");
Console.WriteLine("Sum of two numbers 5,6");
Console.WriteLine(client.Add(5, 6));
Console.WriteLine("Subtraction of two numbers 6,5");
Console.WriteLine(client.Sub(6, 5));
Console.WriteLine("Multiplication of two numbers 6,5");
Console.WriteLine(client.Mul(6, 5));
Console.WriteLine("Division of two numbers 6,3");
Console.WriteLine(client.Div(6, 3));
Console.Read();
}
}
}
輸出如下所示: