WCF開發的幾個頻驟

來源:互聯網
上載者:User
在這裡我就用一個據於一個簡單的情境:服務端為客服端提供擷取客戶資訊的一個介面讀取客戶資訊,來完成WCF開發入門的六個步驟。
1.       定義WCF服務契約

A.      項目引用節點右鍵添加System.ServiceModel引用。

B.      在代碼檔案裡,添加以下命名空間的引用

using System.ServiceModel;

using System;

C.      建立一個命為ICustomerService 介面,並添加一個擷取客戶資訊的方法定義名為CustomerInfomation,返回字串類型的客戶資訊。

D.     為介面ICustomerService添加ServiceContract的屬性修飾使它成為WCF服務中公開的介面。

E.      為方法CustomerInfomation添加OperationContract的屬性修飾使它成為WCF服務公開介面中公開的成員。

F.      代碼:

 1 using System;
 2
 3 using System.ServiceModel;
 4
 5 namespace ConWCF
 6
 7 {   [ServiceContract(Namespace = "http://microsoft.servicemodel.samples/")]
 8
 9     public interface ICustomerService
10
11     {
12
13         [OperationContract]
14
15         String CustomerInformation();
16
17     }
18
19 }
20

 

 

2.       實現WCF服務契約
 

實現WCF服務契約很簡單,就是實現上一步聚定義的WCF服務契約定義的介面就可以。下面看代碼

 1 using System;
 2
 3 using System.ServiceModel;
 4
 5 namespace ConWCF
 6
 7 {   [ServiceContract(Namespace = "http://microsoft.servicemodel.samples/")]
 8
 9     public interface ICustomerService
10
11     {
12
13         [OperationContract]
14
15         String CustomerInformation();
16
17     }
18
19     public class CustomerService:ICustomerService
20
21     {
22
23         #region ICustomerService 成員
24
25         public string CustomerInformation()
26
27         {
28
29             return "這是客戶的資訊!";
30
31         }
32
33         #endregion
34
35     }
36
37 }
38
39

 

3.       啟動WCF服務

A.添加一個應用程式設定檔,檔案件名為App.config。

B.配置WCF服務的基本地址,如下所示

<host>

                       <baseAddresses>

                            <addbaseAddress="http://localhost:8000/conwcfr%22/>

                       </baseAddresses>

                   </host>

C.配置WCF服務的連接埠。Address=“”,意思就是使用上面配置的基本地址,當然也可以在這裡指定。Bingding=“wsHttpBinding”,意思是WCF服務使用的是HTTP協議。再接下來就是配置WCF服務契約了(命名空間.服務契約介面名), contract中的值就是上面定義的介面,如下所示:

<endpoint  address=""

                             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、連接埠到配置環境排除錯誤,就是搞不明白為什麼會跟類的命稱聯絡起來。不過,最終也解決了。

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服務端正在運行中。

5.       WCF客服端基本配置

WCF用戶端配置就是配置調用WCF服務端的協議,輸傳寬頻,服務地址,安全等等資訊。下面就上一步驟命令自動產生的設定檔。

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <configuration>
 3     <system.serviceModel>
 4         <bindings>
 5             <wsHttpBinding>
 6                 <binding name="WSHttpBinding_ICustomerService" closeTimeout="00:01:00"
 7                     openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
 8                     bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
 9                     maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
10                     messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
11                     allowCookies="false">
12                     <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
13                         maxBytesPerRead="4096" maxNameTableCharCount="16384" />
14                     <reliableSession ordered="true" inactivityTimeout="00:10:00"
15                         enabled="false" />
16                     <security mode="Message">
17                         <transport clientCredentialType="Windows" proxyCredentialType="None"
18                             realm="" />
19                         <message clientCredentialType="Windows" negotiateServiceCredential="true"
20                             algorithmSuite="Default" establishSecurityContext="true" />
21                     </security>
22                 </binding>
23             </wsHttpBinding>
24         </bindings>
25         <client>
26             <endpoint address="http://localhost:8000/conwcfr" binding="wsHttpBinding"
27                 bindingConfiguration="WSHttpBinding_ICustomerService" contract="ICustomerService"
28                 name="WSHttpBinding_ICustomerService">
29                 <identity>
30                     <userPrincipalName value="30DA1D0B1D1E4D2\Administrator" />
31                 </identity>
32             </endpoint>
33         </client>
34     </system.serviceModel>
35 </configuration>

 

6.       使用WCF用戶端

在用戶端項目中項目引用節點右鍵添加System.ServiceModel引用.
添加第四部中建立的用戶端代碼檔案和設定檔。
用戶端調用服務端的服務,只要建立產生用戶端類的執行個體就可調用了,但要確認服務端正在起用狀態,如下
 

 1using System;
 2
 3namespace ConWCFCustomerClient
 4
 5{
 6
 7    class Program
 8
 9    {
10
11        static void Main(string[] args)
12
13        {
14
15            CustomerServiceClient client = new CustomerServiceClient();
16
17            string message=client.CustomerInformation();
18
19            Console.WriteLine(message);
20
21            Console.Read();
22
23        }
24
25    }
26
27}
28

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.