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;

                }

            }

 

        }

    }

}

posted @ 2008-12-10 12:51  昕友软件开发  阅读(306)  评论(0编辑  收藏  举报
欢迎访问我的开源项目:xyIM企业即时通讯