国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

如何創建一個AJAX-Enabled WCF Service

2019-11-17 01:49:20
字體:
來源:轉載
供稿:網友

如何創建一個Ajax-Enabled WCF Service

 

原創地址:http://www.survivalescaperooms.com/jfzhu/p/4041638.html

轉載請注明出處

 

前面的文章中介紹過《Step by Step 創建一個WCF Service 》以及《如何使用WCF的Trace與Message Log功能》,本文介紹如何創建一個AJAX-Enabled WCF Service。

(一)創建一個WCF AJAX-enabled service

1. 打開Visual Studio 2012,創建一個asp.net Empty Web application PRoject,命名為SandwichServices。這時Visual Studio的web.config文件內容為:

 

<?xml version="1.0"?><configuration>  <system.web>    <compilation debug="true" targetFramework="4.0" />  </system.web></configuration>

 

2. 添加一個AJAX-enabled WCF Service,命名為CostService.svc

 

using System.ServiceModel;using System.ServiceModel.Activation;namespace SandwichServices{    [ServiceContract(Namespace = "SandwichServices")]    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]    public class CostService    {        // To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json)        // To create an Operation that returns XML,        //     add [WebGet(ResponseFormat=WebMessageFormat.Xml)],        //     and include the following line in the operation body:        //         WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";        [OperationContract]        public void DoWork()        {            // Add your operation implementation here            return;        }            }}

因為我們不打算使用TCP等HTTP之外的Protocol,所以設置為AspNetCompatibilityEnabled。

 

3. 修改Namespace ServiceContractAttribute,并添加一個CostOfSandwiches方法

 

using System.ServiceModel;using System.ServiceModel.Activation;namespace SandwichServices{    [ServiceContract(Namespace = "SandwichServices")]    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]    public class CostService    {        // To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json)        // To create an operation that returns XML,        //     add [WebGet(ResponseFormat=WebMessageFormat.Xml)],        //     and include the following line in the operation body:        //         WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";        [OperationContract]        public void DoWork()        {            // Add your operation implementation here            return;        }        [OperationContract]        public double CostOfSandwiches(int quantity)        {            return 1.25 * quantity;        }    }}

 

4. 這時Visual Studio生成的web.config文件

 

<?xml version="1.0"?><configuration>  <system.web>    <compilation debug="true" targetFramework="4.0" />  </system.web>  <system.serviceModel>    <behaviors>      <endpointBehaviors>        <behavior name="SandwichServices.CostServiceAspNetAjaxBehavior">          <enableWebScript />        </behavior>      </endpointBehaviors>    </behaviors>    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"        multipleSiteBindingsEnabled="true" />    <services>      <service name="SandwichServices.CostService">        <endpoint address="" behaviorConfiguration="SandwichServices.CostServiceAspNetAjaxBehavior"            binding="webHttpBinding" contract="SandwichServices.CostService" />      </service>    </services>  </system.serviceModel></configuration>

 

如果在瀏覽器中訪問CostService.svc,得到如下錯誤

Untitled

 

 

5. 修改web.config文件

 

<?xml version="1.0"?><configuration>  <system.web>    <compilation debug="true" targetFramework="4.0" />  </system.web>  <system.serviceModel>    <behaviors>      <endpointBehaviors>        <behavior name="SandwichServices.CostServiceAspNetAjaxBehavior">          <enableWebScript />        </behavior>      </endpointBehaviors>      <serviceBehaviors>        <behavior name="SandwichServices.CostServiceServiceBehavior" >          <serviceMetadata httpGetEnabled="true" />        </behavior>      </serviceBehaviors>    </behaviors>    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"        multipleSiteBindingsEnabled="true" />    <services>      <service name="SandwichServices.CostService" behaviorConfiguration="SandwichServices.CostServiceServiceBehavior">        <endpoint address="" behaviorConfiguration="SandwichServices.CostServiceAspNetAjaxBehavior"            binding="webHttpBinding" contract="SandwichServices.CostService" />        <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />      </service>    </services>  </system.serviceModel></configuration>

 

再次在瀏覽器中打開CostService.svc,可以正常訪問了。

image

 

(二)創建Client端,調用WCF Service

1. 創建一個aspx Page

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebClient.WebForm2" %><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title></title>    <script language="javascript" type="text/Javascript">        function Button1_onclick() {            var service = new SandwichServices.CostService();            service.CostOfSandwiches(3, onSuccess, null, null);        }        function onSuccess(result) {            alert(result);        }    </script></head><body>    <form id="form1" runat="server">    <div>        <p>        <input id="Button1" type="button" value="Price for 3 Sandwiches" onclick="return Button1_onclick()" />        </p>    </div>        <asp:ScriptManager ID="ScriptManager1" runat="server">            <Services>                <asp:ServiceReference Path="http://192.168.6.47:8080/CostService.svc" />            </Services>        </asp:ScriptManager>    </form></body></html>

 

2. 在瀏覽器中打開該頁面,然后用F12工具可以看到加載進來的JavaScript,是WCF Service生成的。

image

 

Fiddler

image

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 嫩江县| 乌拉特前旗| 宁蒗| 灵璧县| 博兴县| 绥德县| 日照市| 金坛市| 拉萨市| 丹阳市| 始兴县| 阿勒泰市| 霍城县| 天峨县| 华容县| 溆浦县| 峨边| 肃宁县| 襄汾县| 米林县| 玉林市| 宁化县| 云霄县| 香格里拉县| 平南县| 石棉县| 奈曼旗| 资溪县| 信阳市| 玛沁县| 平谷区| 阳曲县| 满洲里市| 青州市| 启东市| 马尔康县| 盱眙县| 东平县| 辉县市| 毕节市| 都安|