zoukankan      html  css  js  c++  java
  • SilverLight学习笔记WCF服务

    编写WCF服务接口

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Runtime.Serialization;

    using System.ServiceModel;

    using System.Text;

     

    namespace SilverlightApplication11.Web

    {

        // 注意: 如果更改此处的接口名称 "IMyWCFService",也必须更新 Web.config 中对 "IMyWCFService" 的引用。

        [ServiceContract]

        public interface IMyWCFService

        {

            [OperationContract]

            string ReturnXML();

        }

    }

    编写服务类

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Runtime.Serialization;

    using System.ServiceModel;

    using System.Text;

     

    namespace SilverlightApplication11.Web

    {

        // 注意: 如果更改此处的类名 "MyWCFService",也必须更新 Web.config 中对 "MyWCFService" 的引用。

        public class MyWCFService : IMyWCFService

        {

            public string ReturnXML()

            {

                string s;

                s = @"<body IsMember='true'>

                      <form_LRB>

                        <YYSRBQ>3536456.98</YYSRBQ>

                        <YYCBBQ>456798.00</YYCBBQ>

                        <XSFYBQ>3456.00</XSFYBQ>

                        <YYLRBQ>255456.32</YYLRBQ>

                      </form_LRB>

                      <head>

                        <createTime>2008-12-10</createTime>

                        <form>

                          <formId>ADGH4368FDG3465</formId>

                          <instanceId>DG2H9J-DG22HG-ASF42F-55FFG</instanceId>

                        </form>

                      </head>

                      <base>

                        <NSRMC>东莞市愉达玻璃装饰工程有限公司</NSRMC>

                      </base>

                    </body>";

     

                return s;

            }

        }

    }

    修改Web.config

    特别注意一定要把binding 改为:basicHttpBinding默认是wsHttpBinding,否则会出错提示“运行出错“System.ServiceModel.ProtocolException

         <system.serviceModel>

             <behaviors>

                  <serviceBehaviors>

                       <behavior name="SilverlightApplication11.Web.MyWCFServiceBehavior">

                           <serviceMetadata httpGetEnabled="true"/>

                           <serviceDebug includeExceptionDetailInFaults="false"/>

                       </behavior>

                  </serviceBehaviors>

             </behaviors>

             <services>

                  <service behaviorConfiguration="SilverlightApplication11.Web.MyWCFServiceBehavior" name="SilverlightApplication11.Web.MyWCFService">

                       <endpoint address="" binding="basicHttpBinding" contract="SilverlightApplication11.Web.IMyWCFService">

                           <identity>

                                <dns value="localhost"/>

                           </identity>

                       </endpoint>

                       <endpoint address="mex" binding="basicHttpBinding" contract="IMetadataExchange"/>

                  </service>

             </services>

         </system.serviceModel>

    引用代理类

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Net;

    using System.Windows;

    using System.Windows.Controls;

    using System.Windows.Documents;

    using System.Windows.Input;

    using System.Windows.Media;

    using System.Windows.Media.Animation;

    using System.Windows.Shapes;

    using SilverlightApplication11.MyWCF;

    using System.Xml;

    using System.IO;

    using System.ServiceModel.Channels;

    using System.ServiceModel;

     

    namespace SilverlightApplication11

    {

        public partial class Page : UserControl

        {

            public Page()

            {

                InitializeComponent();

                GetService();

            }

     

            private void GetService()

            {

                Binding binding = new BasicHttpBinding();

                EndpointAddress endPoint = new EndpointAddress("http://localhost:4162/MyWCFService.svc");

     

                MyWCFServiceClient Mywcf = new MyWCFServiceClient(binding, endPoint);

                Mywcf.ReturnXMLCompleted += new EventHandler<ReturnXMLCompletedEventArgs>(wcf_ReturnXMLCompleted);

                Mywcf.ReturnXMLAsync();

            }

     

            void wcf_ReturnXMLCompleted(object sender, ReturnXMLCompletedEventArgs e)

            {

                if (e.Error == null)

                {

                    using (XmlReader responseReader = XmlReader.Create(new StringReader(e.Result)))

                    {

     

                        responseReader.ReadToFollowing("YYSRBQ");

                        decimal YYSRBQ = responseReader.ReadElementContentAsDecimal();

                        responseReader.ReadToFollowing("YYCBBQ");

                        decimal YYCBBQ = responseReader.ReadElementContentAsDecimal();

                        responseReader.ReadToFollowing("YYLRBQ");

                        decimal YYLRBQ = responseReader.ReadElementContentAsDecimal();

                        responseReader.ReadToFollowing("instanceId");

                        string instanceId = responseReader.ReadInnerXml();

                        responseReader.ReadToFollowing("NSRMC");

                        string name = responseReader.ReadInnerXml();

     

                        OutputText.Text = "表单实例:" + instanceId + ""n" + name + ""n营业收入:" + YYSRBQ + ""n营业成本:" + YYCBBQ + ""n营业利润:" + YYLRBQ;

                    }

                }

     

            }

        }

    }

    关于作者: 王昕(QQ:475660) 在广州工作生活30余年。十多年开发经验,在Java、即时通讯、NoSQL、BPM、大数据等领域较有经验。
    目前维护的开源产品:https://gitee.com/475660
  • 相关阅读:
    深入理解乐观锁与悲观锁
    mysql5.7 安装常见问题
    nginx 前后分离,地址重写,url匹配中遇到的问题
    nginx rewrite
    Nginx location 配置
    Nginx高级应用之Location Url 配置
    Zookeeper的功能以及工作原理
    [PY3]——IO——文件目录操作
    [PY3]——pwd | grp 模块
    [PY3]——时间处理——datetime | calendar
  • 原文地址:https://www.cnblogs.com/starcrm/p/1351780.html
Copyright © 2011-2022 走看看