學習調用WCF服務的各類方法

1.開發工具調用WCF javascript

這中方法很方便也很簡單,不少工做VS就幫咱們完成了。相信你們也不會對這種方法陌生。這裏簡單提一下。打開VS,在項目中添加服務引用: 在config中自動聲明瞭有關服務的節點信息,這樣VS就建立了調用服務的代理:css

ServiceReference1.Service1Client poxy = new ServiceReference1.Service1Client(); poxy.服務中相應的方法。

2.C#動態調用WCFhtml

這個方法比較實用,能夠經過工具或代碼生成代理類generatedProxy.cs和配置文件app.config,來和WCF進行交互。不須要人爲的手動進行服務的引用。生成代理類 java

vs工具中:jquery

工具--svcutil:
參數: /language:cs /out:generatedProxy.cs /config:app.config http://localhost:9002/Service1.svcweb

 有了這個代理類,工做就好作啦!經過這個代理類就能夠調用WCF了。ajax

這樣,若是多個服務的方法相同,只是address不一樣(分佈在不一樣的服務器)。這樣的調用是很不錯的選擇! 除此以外,咱們能夠採用通道工廠的方式生成客戶端服務對象實例,可是前提仍是須要上面生成的代理類的幫助。你們能夠參看大牛Robin的文章(下面有連接)。json

3.JS(jQuery)調用WCF服務器

這裏實現的思想和ASP.NET Ajax的有些相似,只不過有一些工做須要咱們本身來完成,而且這個方法很靈活。 首先是WCF上:咱們要在類和方法前進行以下的聲明:app

[ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class WCFservice
    {
        [OperationContract]
        [WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
        public string SayHello(string name)
        {
            return "hello:"+name;
        }
    }

接着就是配置文件

<system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="AllenBehavior">
          <enableWebScript />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
    <services>
      <service name="jqueryWCF.WCFservice">
        <endpoint address="" behaviorConfiguration="AllenBehavior" binding="webHttpBinding" contract="jqueryWCF.WCFservice" />
      </service>
    </services>
  </system.serviceModel>

<behavior name="AllenBehavior"><enableWebScript /></behavior>

準備工做作好後就能夠前臺調用了:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>wcf</title>
<script language="javascript" type="text/javascript" src="jquery.js"></script>
<script language="javascript" type="text/javascript">
function sayhello(){
    var name = $("#name").val();
    $.ajax({
        type: 'post',
        url: '/WCFservice.svc/SayHello',
        contentType: 'text/json',
        data: '{"name":"'+name+'"}',
        success: function(msg) {
        var a = eval('('+msg+')');
        if(String(a.d).length>0){alert(a.d);}
        else{alert("服務器超時");}
        }
    });
    }
</script>
<style type="text/css">
#content{height: 181px;width: 549px;}
#title{width: 544px;}
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        name:<input type="text" id="name" />
        <br />
    <input type="button"  value="hello" onclick="sayhello();" />
    </div>
    </form>
</body>
</html>

這裏的一些注意事項你們能夠但看dudu的文章(下面有連接)。這樣,咱們就能夠利用jQuery調用wcf了。


參考學習資料:
Robin:http://www.cnblogs.com/jillzhang/archive/2008/07/26/1252171.html
dudu:http://www.cnblogs.com/dudu/archive/2009/07/14/1523082.html
liulun:http://www.cnblogs.com/liulun/articles/1425382.html

相關文章
相關標籤/搜索