Silverlight中同步调用WebClient的解决办法,是同步!

Silverlight中同步调用WebClient的解决办法,是同步!,第1张

概述如何建立web服务并引用的细节,不是本文的介绍的目标,不再赘述。在silverlight调用服务器端服务的时候,默认情况下是进行异步调用的,代码如下: private void button2_Click(object sender, RoutedEventArgs e) { Service1Client sc = new Service1C

如何建立web服务并引用的细节,不是本文的介绍的目标,不再赘述。在silverlight调用服务器端服务的时候,默认情况下是进行异步调用的,代码如下:

           private voID button2_Click(object       sender,RoutedEventArgs e)
{
Service1ClIEnt sc
= new
Service1ClIEnt();
sc.DoWorkCompleted
+= new EventHandler<DoWorkCompletedEventArgs>
(sc_DoWorkCompleted);
sc.DoWorkAsync(textBox1.Text);

}

voID sc_DoWorkCompleted(object
sender,DoWorkCompletedEventArgs e)
{
textBox2.Text
=
e.Result;
}

若是你的调用非常复杂的话,比如当这个调用完成的时候开始下一个调用,然后又进行下一个调用,各个调用之间存在关联关系的话,一直XX_DoWorkCompleted会让你头大,并且不利于代码的管理。若碰到过这样的问题的朋友一定很希望如果能够同步调用就好了,这篇文章将帮到你。或者现在不需要,等你需要的时候记得用就行了,别像我当初那样难为的不行。

主要是需要引用一个类库的问题,这个类库是外国人写的,名称为DanielVaughan.dll,下载完之后,首先需要在项目中添加对它的引用,如下图,

然后在程序中添加对两个空间的引用,如下图:



将原来的添加botton1事件:

           private voID button1_Click(object       sender,RoutedEventArgs e)
{

string dd =
textBox1.Text;
string res = "NulL"
;
ThreadPool.QueueUserWorkItem(
delegate

{
Service1 sv
= ChannelManager.Instance.GetChannel<Service1> ();
/* Perform synchronous WCF call. */

res
= SynchronousChannelbroker.PerformAction<string,string> (sv.BeginDoWork,sv.EndDoWork,dd);
dispatcher.BeginInvoke(
delegate

{
textBox2.Text
+="/r/n同步调用--"+ res+"/r/n" ;
});
});
}



这样就可以实现对WebClIEnt的同步调用了,当你需要关联调用WebClIEnt3次以上的时候 可以考虑使用这个类库,如果只是简单的调用下的话,没有必要使用。
页面全部代码:

 

          <UserControl x:Class="SilverlightApplication2.MainPage"      
xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d
="http://schemas.microsoft.com/Expression/blend/2008"
xmlns:mc
="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable
="d"
d:DesignHeight
="300" d:DesignWIDth="400" xmlns:datainput="clr-namespace:System.windows.Controls;assembly=System.windows.Controls.Data.input" WIDth="640" Height="480">

<GrID x:name="LayoutRoot">
<GrID.Background>
<linearGradIEntBrush EndPoint="0.443,0.621" StartPoint="0.443,-2.509">
<GradIEntStop color="#FF5C6768"/>
<GradIEntStop color="White" Offset="1"/>
</linearGradIEntBrush>
</GrID.Background>
<button Content="同步调用服务" Height="40" HorizontalAlignment="left" margin="67,98,0" name="button1" VerticalAlignment="top" WIDth="120" Click="button1_Click" />
<datainput:Label Height="50" HorizontalAlignment="left" margin="67,188,0" name="label2" VerticalAlignment="top" WIDth="46" Content="状态:" FontSize="16" />
<TextBox Height="40" HorizontalAlignment="left" margin="165,27,0" name="textBox1" VerticalAlignment="top" WIDth="300" FontSize="16" />
<TextBox Height="100" HorizontalAlignment="left" margin="146,0" name="textBox2" VerticalAlignment="top" WIDth="400" FontSize="16" textwrapPing="Wrap" Text="尚未调用服务" />
<button Content="异步调用服务" Height="40" HorizontalAlignment="left" margin="346,0" name="button2" VerticalAlignment="top" WIDth="120" Click="button2_Click" />
<datainput:Label Height="40" HorizontalAlignment="left" margin="67,0" name="label1" VerticalAlignment="top" WIDth="92" FontSize="16" Content="输入文本:" />
</GrID>
</UserControl>



处理程序全部代码:

          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
SilverlightApplication2.ServiceReference1;
using
System.Threading;
using
DanIElVaughan;

namespace
SilverlightApplication2
{
public partial class
MainPage : UserControl
{
public
MainPage()
{
InitializeComponent();
UisynchronizationContext.Instance.Initialize(dispatcher);
}

private voID button1_Click(object
sender,dd);
dispatcher.BeginInvoke(
delegate

{
textBox2.Text
+="/r/n同步调用--"+ res+"/r/n" ;
});
});
}

private voID button2_Click(object
sender,RoutedEventArgs e)
{
Service1ClIEnt sc
= new
Service1ClIEnt();
sc.DoWorkCompleted
+= new EventHandler<DoWorkCompletedEventArgs>
(sc_DoWorkCompleted);
sc.DoWorkAsync(textBox1.Text);
}


voID sc_DoWorkCompleted(object
sender,DoWorkCompletedEventArgs e)
{
textBox2.Text
+= "异步调用--" + e.Result + "/r/n"
;
}
}
}

 

Service代码:

          using       System;
using
System.linq;
using
System.Runtime.Serialization;
using
System.ServiceModel;
using
System.ServiceModel.Activation;

namespace
SilverlightApplication2.Web
{
[ServiceContract(namespace
= ""
)]
[AspNetCompatibilityRequirements(RequirementsMode
=
AspNetCompatibilityRequirementsMode.Allowed)]
public class
Service1
{
[OperationContract]
public string DoWork(string
aa)
{
// 在此处添加 *** 作实现

return "调用服务完成,返回你输入的值:"+ aa;
}

// 在此处添加更多 *** 作并使用 [OperationContract] 标记它们

}
}

程序运行截图:

1.



2.
 



3.




欢迎广大园友共同探讨,觉得好的话请推荐下。本人技术水平有限,如有不足之处,还请园友多多批评指正,谢谢。

总结

以上是内存溢出为你收集整理的Silverlight中同步调用WebClient的解决办法,是同步!全部内容,希望文章能够帮你解决Silverlight中同步调用WebClient的解决办法,是同步!所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1078459.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存