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;
??????????????? }
??????????? }
?
??????? }
??? }
}
轉載于:https://www.cnblogs.com/starcrm/archive/2008/12/10/1351780.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的SilverLight学习笔记--WCF服务的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SuperMap.IS.AjaxScri
- 下一篇: 微软发布最新开源Blog平台“Oxite