麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > JavaScript > 正文

用JavaScript調用WCF Service

2019-11-17 01:49:12
字體:
來源:轉載
供稿:網(wǎng)友

javaScript調用WCF Service

原創(chuàng)地址:http://www.companysz.com/jfzhu/p/4039604.html

轉載請注明出處

前面介紹過《Step by Step 創(chuàng)建一個WCF Service》和《使用WCF的Trace與Message Log功能》,本文介紹一下如何用Javascript來調用WCF Service。

WCF Service的代碼如下:

IHelloService.cs

using System.ServiceModel;using System.ServiceModel.Activation;using System.ServiceModel.Web;namespace HelloService{        [ServiceContract(Name = "IHelloService")]        public interface IHelloService    {        [OperationContract(Name="GetMessage")]                string GetMessage(string name);        [OperationContract]        Employee GetEmployee(int id);    }}

HelloService.cs

using System;namespace HelloService{        public class HelloService : IHelloService    {        public string GetMessage(string name)        {            return "Hello " + name;                    }        public Employee GetEmployee(int id)        {            return new Employee() { Id = id, Name="Neil Klugman", Birthdate=new DateTime(1930, 1, 31)};        }    }}

web.config文件,注意高亮部分:

<?xml version="1.0" encoding="utf-8" ?><configuration>  <system.serviceModel>    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />        <services>      <service name="HelloService.HelloService" behaviorConfiguration="metaBehavior">        <endpoint address="" binding="webHttpBinding" contract="HelloService.IHelloService" behaviorConfiguration="AjaxServiceBehavior"></endpoint>        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>        <host>          <baseAddresses>            <add baseAddress="http://localhost:8080"/>          </baseAddresses>        </host>      </service>          </services>    <behaviors>      <endpointBehaviors>        <behavior name="ajaxServiceBehavior">          <enableWebScript/>        </behavior>      </endpointBehaviors>      <serviceBehaviors>        <behavior name="metaBehavior">          <serviceDebug includeExceptionDetailInFaults="true" />          <serviceMetadata httpGetEnabled="true" />        </behavior>        <behavior name="">          <serviceMetadata httpGetEnabled="true" />          <serviceDebug includeExceptionDetailInFaults="false" />        </behavior>      </serviceBehaviors>    </behaviors>  </system.serviceModel></configuration>

創(chuàng)建一個客戶端web application,添加一個web form,WebForm1.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="HelloWebClient.WebForm1" %><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head id="Head1" runat="server">    <title>AJAX Service Client Page</title></head><body>    <form id="form1" runat="server">    <asp:ScriptManager ID="ScriptManager" runat="server">         <Services>             <asp:ServiceReference Path="http://192.168.6.47:8080/HelloService.svc" />         </Services>    </asp:ScriptManager>            <script lang="javascript" type="text/javascript">        function GetValueFromServer() {            var name = document.getElementById('txtValueContainer').value;            tempuri.org.IHelloService.GetMessage(name, onSuccess, onFailure, null);                    }        function onSuccess(result) {            document.getElementById('labelResult').value = result;        }        function onFailure(result) {            window.alert(result);        }    </script>    <div>        <input id="btnServiceCaller" type="button" value="Get Value" onclick="GetValueFromServer()"; />        <input id="txtValueContainer" type="text" value="" />         <input id="labelResult" type="text" value="" />    </div>    </form></body></html>

用瀏覽器打開WebForm1.aspx,使用Fiddler查看,因為代碼里有了對WCF Service的引用

<asp:ServiceReference Path="http://192.168.6.47:8080/HelloService.svc" />

所以頁面加載了JavaScript

image_thumb7

加載的JavaScript代碼為:

Type.registerNamespace('tempuri.org');tempuri.org.IHelloService = function () {    tempuri.org.IHelloService.initializeBase(this);    this._timeout = 0;    this._userContext = null;    this._succeeded = null;    this._failed = null;}tempuri.org.IHelloService.PRototype = {    _get_path: function () {        var p = this.get_path();        if (p) return p;        else return tempuri.org.IHelloService._staticInstance.get_path();    },    GetMessage: function (name, succeededCallback, failedCallback, userContext) {        /// <param name="name" type="String">System.String</param>        /// <param name="succeededCallback" type="Function" optional="true" mayBeNull="true"></param>        /// <param name="failedCallback" type="Function" optional="true" mayBeNull="true"></param>        /// <param name="userContext" optional="true" mayBeNull="true"></param>        return this._invoke(this._get_path(), 'GetMessage', false, { name: name }, succeededCallback, failedCallback, userContext);    },    GetEmployee: function (id, succeededCallback, failedCallback, userContext) {        /// <param name="id" type="Number">System.Int32</param>        /// <param name="succeededCallback" type="Function" optional="true" mayBeNull="true"></param>        /// <param name="failedCallback" type="Function" optional="true" mayBeNull="true"></param>        /// <param name="userContext" optional="true" mayBeNull="true"></param>        return this._invoke(this._get_path(), 'GetEmployee', false, { id: id }, succeededCallback, failedCallback, userContext);    }}tempuri.org.IHelloService.registerClass('tempuri.org.IHelloService', Sys.Net.WebServiceProxy);tempuri.org.IHelloService._staticInstance = new tempuri.org.IHelloService();tempuri.org.IHelloService.set_path = function (value) {    tempuri.org.IHelloService._staticInstance.set_path(value);}tempuri.org.IHelloService.get_path = function () {    /// <value type="String" mayBeNull="true">The service url.</value>    return tempuri.org.IHelloService._staticInstance.get_path();}tempuri.org.IHelloService.set_timeout = function (value) {    tempuri.org.IHelloService._staticInstance.set_timeout(value);}tempuri.org.IHelloService.get_timeout = function () {    /// <value type="Number">The service timeout.</value>    return tempuri.org.IHelloService._staticInstance.get_timeout();}tempuri.org.IHelloService.set_defaultUserContext = function (value) {    tempuri.org.IHelloService._staticInstance.set_defaultUserContext(value);}tempuri.org.IHelloService.get_defaultUserContext = function () {    /// <value mayBeNull="true">The service default user context.</value>    return tempuri.org.IHelloService._staticInstance.get_defaultUserContext();}tempuri.org.IHelloService.set_defaultSucceededCallback = function (value) {    tempuri.org.IHelloService._staticInstance.set_defaultSucceededCallback(value);}tempuri.org.IHelloService.get_defaultSucceededCallback = function () {    /// <value type="Function" mayBeNull="true">The service default succeeded callback.</value>    return tempuri.org.IHelloService._staticInstance.get_defaultSucceededCallba
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 蜜桃视频最新网址 | china对白普通话xxxx | 激情夜色 | 久久久久久三区 | 男人久久天堂 | 在线播放中文 | 99亚洲伊人久久精品影院红桃 | 欧美成人精品一区二区 | 国产精品亚洲综合一区二区三区 | 国产精品啪一品二区三区粉嫩 | 久久久久久久久久久久久久久伊免 | 天天草天天色 | 天天操很很操 | 激情小视频在线观看 | 欧美一级做一级爱a做片性 久久久资源网 | 狠狠色噜噜狠狠狠米奇9999 | 国产美女视频黄a视频免费 日韩黄色在线播放 | 久久久精品综合 | 国产日韩三区 | 日韩精品a在线观看 | 毛片a级毛片免费播放100 | 成人在线视频精品 | 黄色片免费看看 | 亚洲一区二区在线视频 | 欧美一级黄色录相 | 久久99亚洲精品久久99果 | av色偷偷| 精品一区二区三区在线视频 | 免费观看国产精品视频 | 免费的毛片 | 国产精品午夜未成人免费观看 | 最新中文字幕第一页视频 | 国产精品国产三级国产在线观看 | 欧美视频网 | 一区二区久久精品66国产精品 | 国产精品成人亚洲一区二区 | 久久亚洲精品久久国产一区二区 | 在线成人一区二区 | 福利在线免费 | 中文字幕 亚洲一区 | 国产毛片网 |