若是你的調用非常復雜的話,比如當這個調用完成的時候開始下一個調用,然後又進行下一個調用,各個調用之間存在關聯關系的話,一直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,0" Name="button1" VerticalAlignment="Top" Width="120" Click="button1_Click" />
<dataInput:Label Height="50" HorizontalAlignment="Left" Margin="67,188,0,0" Name="label2" VerticalAlignment="Top" Width="46" Content="狀態:" FontSize="16" />
<TextBox Height="40" HorizontalAlignment="Left" Margin="165,27,0,0" Name="textBox1" VerticalAlignment="Top" Width="300" FontSize="16" />
<TextBox Height="100" HorizontalAlignment="Left" Margin="146,188,0,0" Name="textBox2" VerticalAlignment="Top" Width="400" FontSize="16" TextWrapping="Wrap" Text="尚未調用服務" />
<Button Content="異步調用服務" Height="40" HorizontalAlignment="Left" Margin="346,98,0,0" Name="button2" VerticalAlignment="Top" Width="120" Click="button2_Click" />
<dataInput:Label Height="40" HorizontalAlignment="Left" Margin="67,27,0,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, 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";
});
});
}
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.
歡迎大家共同探討,覺得好的話請推薦下。本人技術水平有限,如有不足之處,還請園友多多批評指正,謝謝。