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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

IIS8托管WCF服务

發(fā)布時(shí)間:2023/12/13 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 IIS8托管WCF服务 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

WCF服務(wù)程序本身不能運(yùn)行,需要通過其他的宿主程序進(jìn)行托管才能調(diào)用WCF服務(wù)功能,常見的宿主程序有IIS,WAS,Windows服務(wù),當(dāng)然在學(xué)習(xí)WCF技術(shù)的時(shí)候一般使用控制臺(tái)應(yīng)用程序或WinForm程序進(jìn)行托管。本文將詳細(xì)介紹如何使用IIS8托管WCF服務(wù)程序以及解決可能會(huì)碰到的一些問題。步驟比較多,還需耐心看完!

一、安裝IIS8

1.本機(jī)器是Win8操作系統(tǒng),默認(rèn)沒有安裝IIS。安裝IIS8很簡(jiǎn)單,具體步驟是:控制面板→程序和功能→啟用或關(guān)閉Windows功能,勾選Internet信息服務(wù)節(jié)點(diǎn)下的部分功能,

如圖所示:

2.安裝完畢,重啟系統(tǒng)后,在瀏覽器中輸入?http://localhost/,即可看見IIS8界面,表示安裝成功,如圖:

3.啟用WCF服務(wù)中的HTTP激活功能,具體步驟是:控制面板→程序和功能→啟用或關(guān)閉Windows功能,勾選“.Net Framework 4.5 高級(jí)服務(wù)”節(jié)點(diǎn)下的部分功能,

如圖所示:

?

二、編寫WCF服務(wù)應(yīng)用程序

1.新建解決方案“IISHostWCF”,添加“WCF服務(wù)類庫”項(xiàng)目,命名為“WCFService”,如圖

該WCF服務(wù)的功能很簡(jiǎn)單,根據(jù)參數(shù)Id獲取相應(yīng)的價(jià)格,代碼如下:

1)服務(wù)接口代碼

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.ServiceModel;namespace WCFService {[ServiceContract]public interface IGetPrice{[OperationContract]string GetPriceByProductId(int id);} } View Code

2)實(shí)現(xiàn)接口的服務(wù)類代碼

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace WCFService {public class GetPrice : IGetPrice{public string GetPriceByProductId(int id){string price = "0.00";switch (id){case 1:price = "12.34";break;case 2:price = "45.60";break;case 3:price = "78.99";break;default:price = "100.00";break;}return price;}} } View Code

2.在解決方案中,添加→新建網(wǎng)站,選擇“WCF服務(wù)”,命名為“WCFWebSite”,如圖

1)把新生成的IService.cs和Service.cs文件刪除

2)添加在第一個(gè)步驟里新建的WCF服務(wù)類庫,WCFService.dll

3).修改Service.svc文件

4)使用“WCF服務(wù)配置編輯器”編輯web.config文件

web.config代碼如下:

<?xml version="1.0" encoding="utf-8"?> <configuration><appSettings><add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /></appSettings><system.web><compilation debug="false" targetFramework="4.5" /><httpRuntime targetFramework="4.5"/></system.web><system.serviceModel><services><service behaviorConfiguration="WCFServiceBehavior" name="WCFService.GetPrice"><endpoint address="basic" binding="basicHttpBinding" bindingConfiguration=""name="basicEndPoint" contract="WCFService.IGetPrice" /><endpoint address="mex" binding="mexHttpBinding" bindingConfiguration=""name="mexEndPoint" contract="IMetadataExchange" /><host><baseAddresses><add baseAddress="http://localhost:8002/" /></baseAddresses></host></service></services><behaviors><serviceBehaviors><behavior name="WCFServiceBehavior"><serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /><serviceDebug includeExceptionDetailInFaults="false" /></behavior></serviceBehaviors></behaviors><protocolMapping><add binding="basicHttpsBinding" scheme="https" /></protocolMapping> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /></system.serviceModel><system.webServer><modules runAllManagedModulesForAllRequests="true"/><!--若要在調(diào)試過程中瀏覽 Web 應(yīng)用程序根目錄,請(qǐng)將下面的值設(shè)置為 True。在部署之前將該值設(shè)置為 False 可避免泄露 Web 應(yīng)用程序文件夾信息。--><directoryBrowse enabled="true"/></system.webServer></configuration> View Code

5)發(fā)布“WCF服務(wù)網(wǎng)站”

三、使用IIS托管WCF服務(wù)

1.打開IIS管理器,添加網(wǎng)站,如圖

2.添加完網(wǎng)站后,右鍵菜單→管理網(wǎng)站→瀏覽,彈出頁面,選擇瀏覽“Service.svc”文件,

打開后,發(fā)現(xiàn)會(huì)報(bào)錯(cuò):

經(jīng)過查找資料,原來是IIS8默認(rèn)沒有添加處理svc文件的處理程序,需手動(dòng)添加:

1)添加MIME類型

文件擴(kuò)展名:.svc;MIME類型:application/octet-stream。

2)添加處理程序映射

請(qǐng)求路徑:?*.svc;

類型:System.ServiceModel.Activation.HttpHandler;

名稱:svc-Integrated

添加完畢后,重新啟動(dòng)網(wǎng)站,再次瀏覽即可成功:

四、測(cè)試IIS托管的WCF服務(wù)

1.通過VS自帶的WCF測(cè)試工具

打開“VS2012開發(fā)人員命令提示”工具,輸入“wcftestclient”,即可打開“WCF測(cè)試客戶端”:

添加WCF服務(wù)測(cè)試地址:http://localhost:8001/Service.svc/mex

測(cè)試結(jié)果如下:

2.新建控制臺(tái)客戶端來測(cè)試WCF服務(wù),添加服務(wù)引用

以下是簡(jiǎn)單的測(cè)試代碼:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace WCFClient {class Program{static void Main(string[] args){WCFGetPrice.GetPriceClient proxy = new WCFGetPrice.GetPriceClient("basicEndPoint");Console.WriteLine(proxy.GetPriceByProductId(3));Console.ReadKey();}} } View Code

結(jié)果如下:

?

至此使用IIS托管WCF服務(wù)應(yīng)用程序詳細(xì)步驟完畢。

好困啊~~~

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/shaomenghao/p/3477693.html

總結(jié)

以上是生活随笔為你收集整理的IIS8托管WCF服务的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

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