日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

WCF开发入门的六个步骤

發(fā)布時(shí)間:2023/11/30 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 WCF开发入门的六个步骤 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

在這里我就用一個(gè)據(jù)于一個(gè)簡(jiǎn)單的場(chǎng)景:服務(wù)端為客服端提供獲取客戶信息的一個(gè)接口讀取客戶信息,來(lái)完成WCF開發(fā)入門的六個(gè)步驟。

1.?????? 定義WCF服務(wù)契約

A.????? 項(xiàng)目引用節(jié)點(diǎn)右鍵添加引用。

B.????? 在代碼文件里,添加以下命名空間的引用

using ;

using System;

C.????? 新建一個(gè)命為ICustomerService 接口,并添加一個(gè)獲取客戶信息的方法定義名為CustomerInfomation,返回字符串類型的客戶信息。

D.???? 為接口ICustomerService添加ServiceContract的屬性修飾使它成為WCF服務(wù)中公開的接口。?

E.????? 為方法CustomerInfomation添加OperationContract的屬性修飾使它成為WCF服務(wù)公開接口中公開的成員。

F.????? 代碼:

?1?using?System;
?2?
?3?using?;
?4?
?5?namespace?ConWCF
?6?
?7?{???[ServiceContract(Namespace?=?"")]
?8?
?9?????public?interface?CustomerService
10?
11?????{?
12?
13?????????[OperationContract]
14?
15?????????String?CustomerInformation();
16?
17?????}
18?
19?}
20?

?

2.?????? 實(shí)現(xiàn)WCF服務(wù)契約

?

實(shí)現(xiàn)WCF服務(wù)契約很簡(jiǎn)單,就是實(shí)現(xiàn)上一步聚定義的WCF服務(wù)契約定義的接口就可以。下面看代碼

?1?using?System;
?2?
?3?using?;
?4?
?5?namespace?ConWCF
?6?
?7?{???[ServiceContract(Namespace?=?"")]
?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.?????? 啟動(dòng)WCF服務(wù)

A添加一個(gè)應(yīng)用程序配置文件,文件件名為。

B配置WCF服務(wù)的基本地址,如下所示

<host>

?????????????????????? <baseAddresses>

??????????????????????????? <addbaseAddress="http://localhost:8000/conwcfr"/>

?????????????????????? </baseAddresses>

?????????????????? </host>

C配置WCF服務(wù)的端口。Address=“”,意思就是使用上面配置的基本地址,當(dāng)然也可以在這里指定。Bingding=“wsHttpBinding”,意思是WCF服務(wù)使用的是HTTP協(xié)議。再接下來(lái)就是配置WCF服務(wù)契約了(命名空間.服務(wù)契約接口名),如下所示:

<endpointaddress=""

??????????????????????????? ?binding="wsHttpBinding"

??????????????????????????? ?contract="" />

D配置文件

E啟動(dòng)服服就簡(jiǎn)單了

ServiceHost host = new ServiceHost(typeof(CustomerService));

??????????????????? ();

??????????????????? ("客戶信息服務(wù)已啟動(dòng)");

??????????????????? ("按任意鍵結(jié)束服務(wù)!");

??????????????????? ();

?????????? ();

F當(dāng)服務(wù)啟動(dòng)時(shí),在IE欄中輸入: http://localhost:8000/conwcfr,將會(huì)收到一些幫助的提示信息。

G異常:配置文件中的服務(wù)名稱一定是:命名空間.實(shí)現(xiàn)WCF服務(wù)契約類的名稱,否則將會(huì)發(fā)生找到不配置的異常。

???????????? <service

?????????????????? name=""

?? ?異常信息: Service '' 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.

這個(gè)異常搞得我昏了半天,害得我以為從IIS、端口到配置環(huán)境排除錯(cuò)誤,就是搞不明白為什么會(huì)跟類的命稱聯(lián)系起來(lái)。不過(guò),最終也解決了。

4.?????? 創(chuàng)建一個(gè)基本的WCF客服端

WCF服務(wù)端創(chuàng)建好啊,創(chuàng)建客戶端就容易多了,直接用SVCUTIL 命令行工具去完成代碼的生成。我安裝了WINDOWS SDK,其帶了一個(gè)CMDShell 命令行工具,打開后就可以運(yùn)行SVCUTIL命令,這個(gè)命令是運(yùn)行于 framework 3.0以上環(huán)境。查看詳細(xì)幫助信息可以輸入:svcutil /?,回車。

1.?啟動(dòng)上幾步驟創(chuàng)建好的WCF服務(wù)端。

2.?在CMDShell工具中用CD 轉(zhuǎn)到你要存放客戶端代碼的目錄下,輸入以下命令生成代碼和配置文件。

D:"client>svcutil /language:c# /out:?/config: http:/

/localhost:8000/conwcfr

上面命令指定了要生成代碼的語(yǔ)言,代碼文件和配置文件名,WCF服務(wù)端地址,注意運(yùn)行命令時(shí)必須確定WCF服務(wù)端正在運(yùn)行中。

5.?????? WCF客服端基本配置

WCF客戶端配置就是配置調(diào)用WCF服務(wù)端的協(xié)議,輸傳寬帶,服務(wù)地址,安全等等信息。下面就上一步驟命令自動(dòng)生成的配置文件。

?1?<?xml?version="1.0"?encoding="utf-8"?>
?2?<configuration>
?3?????<>
?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?????</>
35?</configuration>

?

6.?????? 使用WCF客戶端

1.??? 在客戶端項(xiàng)目中項(xiàng)目引用節(jié)點(diǎn)右鍵添加引用.

2.??? 添加第四部中創(chuàng)建的客戶端代碼文件和配置文件。

3.??? 客戶端調(diào)用服務(wù)端的服務(wù),只要?jiǎng)?chuàng)建生成客戶端類的實(shí)例就可調(diào)用了,但要確認(rèn)服務(wù)端正在起用狀態(tài),如下

?

?1 using?System;
?2
?3 namespace?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=();
18
19 ????????????(message);
20
21 ????????????();
22
23 ????????}
24
25 ????}
26
27 }
28

總結(jié)

以上是生活随笔為你收集整理的WCF开发入门的六个步骤的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。