• 学习调用WCF服务的各种方法


    1.开发工具调用WCF

    这中方法很方便也很简单,很多工作VS就帮我们完成了。相信大家也不会对这种方法陌生。这里简单提一下。打开VS,在项目中添加服务引用: 在config中自动声明了有关服务的节点信息,这样VS就创建了调用服务的代理:

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

    2.C#动态调用WCF

    这个方法比较实用,可以通过工具或代码生成代理类generatedProxy.cs和配置文件app.config,来和WCF进行交互。不需要人为的手动进行服务的引用。生成代理类 

    vs工具中:

    工具--svcutil:
    参数: /language:cs /out:generatedProxy.cs /config:app.config http://localhost:9002/Service1.svc

     有了这个代理类,工作就好做啦!通过这个代理类就可以调用WCF了。

    这样,如果多个服务的方法相同,只是address不同(分布在不同的服务器)。这样的调用是很不错的选择! 除此之外,我们可以采用通道工厂的方式生成客户端服务对象实例,但是前提还是需要上面生成的代理类的帮助。大家可以参看大牛Robin的文章(下面有链接)。

    3.JS(jQuery)调用WCF

    这里实现的思想和ASP.NET Ajax的有些类似,只不过有一些工作需要我们自己来完成,并且这个方法很灵活。 首先是WCF上:我们要在类和方法前进行如下的声明:

    [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

  • 相关阅读:
    OpenGL Pixel Linked-List
    Unity multi_compile
    Bindless Textures
    chmod递归设置文件属性
    push submodule
    NodeJS Debugger
    重载new操作符
    OpenGL瓶颈
    NGUI架构和Draw Call合并原理
    字符串哈希函数(String Hash Functions)
  • 原文地址:https://www.cnblogs.com/starksoft/p/4974157.html
Copyright © 2020-2023  润新知