六步驟完成WCF開發
WCF是一個由微軟公司開發的一個.NET Framework 3.5的重要組成部分。大家在學習應用的過程中應該對這一工具有一個詳細的認識。首先,在這里我就用一個據于一個簡單的場景:服務端為客服端提供獲取客戶信息的一個接口讀取客戶信息,來完成WCF開發入門的六個步驟。#t#
WCF開發之1. 定義WCF服務契約
A. 項目引用節點右鍵添加System.ServiceModel引用。
B. 在代碼文件里,添加以下命名空間的引用
using System.ServiceModel;
using System;
C. 新建一個命為ICustomerService 接口,并添加一個獲取客戶信息的方法定義名為CustomerInfomation,返回字符串類型的客戶信息。
D. 為接口ICustomerService添加ServiceContract的屬性修飾使它成為WCF服務中公開的接口。
E. 為方法CustomerInfomation添加OperationContract的屬性修飾使它成為WCF服務公開接口中公開的成員。
F. 代碼:
- using System;
- using System.ServiceModel;
- namespace ConWCF
- { [ServiceContract(Namespace =
"http://Microsoft.ServiceModel.Samples")]- public interface CustomerService
- {
- [OperationContract]
- String CustomerInformation();
- }
- }
WCF開發之2. 實現WCF服務契約
實現WCF服務契約很簡單,就是實現上一步聚定義的WCF服務契約定義的接口就可以。下面看代碼
- using System;
- using System.ServiceModel;
- namespace ConWCF
- { [ServiceContract(Namespace =
"http://Microsoft.ServiceModel.Samples")]- public interface ICustomerService
- {
- [OperationContract]
- String CustomerInformation();
- }
- public class CustomerService:ICustomerService
- {
- #region ICustomerService 成員
- public string CustomerInformation()
- {
- return "這是客戶的信息!";
- }
- #endregion
- }
- }
WCF開發之3. 啟動WCF服務
A.添加一個應用程序配置文件,文件件名為App.config。
B.配置WCF服務的基本地址,如下所示
- < host>
- < baseAddresses>
- < addbaseAddressaddbaseAddress="http://localhost:8000/conwcfr"/>
- < /baseAddresses>
- < /host>
C.配置WCF服務的端口。Address=“”,意思就是使用上面配置的基本地址,當然也可以在這里指定。Bingding=“wsHttpBinding”,意思是WCF服務使用的是HTTP協議。再接下來就是配置WCF服務契約了(命名空間.服務契約接口名),如下所示:
- < endpointaddressendpointaddress=""
- binding="wsHttpBinding"
- contract="ConWCF.ICustomerService" />
D.配置文件
E.啟動服服就簡單了
- ServiceHost host = new ServiceHost(typeof(CustomerService));
- host.Open();
- Console.WriteLine("客戶信息服務已啟動");
- Console.WriteLine("按任意鍵結束服務!");
- Console.Read();
- host.Close();
F.當服務啟動時,在IE欄中輸入: http://localhost:8000/conwcfr,將會收到一些幫助的提示信息。
G.異常:配置文件中的服務名稱一定是:命名空間.實現WCF服務契約類的名稱,否則將會發生找到不配置的異常。
- < service
- name="ConWCF.CustomerService"
異常信息: Service 'ConWCF.CustomerService' has zero application (non-infrastructure) endpoints. This might be because no configuration file was found for your application, or because no service element matching the service name could be found in the configuration file, or because no endpoints were defined in the service element.
這個異常搞得我昏了半天,害得我以為從IIS、端口到配置環境排除錯誤,就是搞不明白為什么會跟類的命稱聯系起來。不過,最終也解決了。
#p#
WCF開發之4. 創建一個基本的WCF客服端
WCF服務端創建好啊,創建客戶端就容易多了,直接用SVCUTIL 命令行工具去完成代碼的生成。我安裝了WINDOWS SDK,其帶了一個CMDShell 命令行工具,打開后就可以運行SVCUTIL命令,這個命令是運行于 framework 3.0以上環境。查看詳細幫助信息可以輸入:svcutil /?,回車。
1. 啟動上幾步驟創建好的WCF服務端。
2. 在CMDShell工具中用CD 轉到你要存放客戶端代碼的目錄下,輸入以下命令生成代碼和配置文件。
D:"client>svcutil /language:c# /out:CustomerClient.cs /config:app.config http://localhost:8000/conwcfr
上面命令指定了要生成代碼的語言,代碼文件和配置文件名,WCF服務端地址,注意運行命令時必須確定WCF服務端正在運行中。
WCF開發之5. WCF客服端基本配置
WCF客戶端配置就是配置調用WCF服務端的協議,輸傳寬帶,服務地址,安全等等信息。下面就上一步驟命令自動生成的配置文件。
- < ?xml version="1.0" encoding="utf-8"?>
- < configuration>
- < system.serviceModel>
- < bindings>
- < wsHttpBinding>
- < binding name="WSHttpBinding_ICustomerService"
closeTimeout="00:01:00"- openTimeout="00:01:00" receiveTimeout="00:10:00"
sendTimeout="00:01:00"- bypassProxyOnLocal="false" transactionFlow="false"
hostNameComparisonMode="StrongWildcard"- maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
- messageEncoding="Text" textEncoding="utf-8"
useDefaultWebProxy="true" allowCookies="false">- < readerQuotas maxDepth="32" maxStringContentLength="8192"
maxArrayLength="16384"- maxBytesPerRead="4096" maxNameTableCharCount="16384" />
- < reliableSession ordered="true"
inactivityTimeout="00:10:00"enabled="false" />- < security mode="Message">
- < transport clientCredentialType="Windows"proxyCredentialType=
"None"realm="" />- < message clientCredentialType="Windows"
negotiateServiceCredential="true"algorithmSuite=
"Default"establishSecurityContext="true" />- < /security>
- < /binding>
- < /wsHttpBinding>
- < /bindings>
- < client>
- < endpoint address="http://localhost:8000/conwcfr"binding=
"wsHttpBinding"bindingConfiguration="WSHttpBinding_ICustomerService"
contract="ICustomerService"name="WSHttpBinding_ICustomerService">- < identity>
- < userPrincipalName value="30DA1D0B1D1E4D\Administrator" />
- < /identity>
- < /endpoint>
- < /client>
- < /system.serviceModel>
- < /configuration>
WCF開發之6. 使用WCF客戶端
在客戶端項目中項目引用節點右鍵添加System.ServiceModel引用. 添加第四部中創建的客戶端代碼文件和配置文件。 客戶端調用服務端的服務,只要創建生成客戶端類的實例就可調用了,但要確認服務端正在起用狀態,如下
- 1using System;
- namespace ConWCFCustomerClient
- {
- class Program
- {
- static void Main(string[] args)
- {
- CustomerServiceClient client = new CustomerServiceClient();
- string message=client.CustomerInformation();
- Console.WriteLine(message);
- Console.Read();
- }
- }
- }
以上就是對WCF開發的相關步驟介紹。