首页 新闻 赞助 找找看

Silverlight与wcf通信时,总是隔一次才能取回消息,帮忙解决下

0
悬赏园豆:50 [已解决问题] 解决于 2010-07-09 09:38

首先我的demo结构是这样的,我将wcf添加的web程序内,主要是实现这样一个过程:

1.进入默认页(加载了index.xaml)后,点击按钮,弹出loginwin窗口是继承childwindow类。

2.在loginwin窗口点击确定按钮后,在确定按钮事件里与wcf通信,取回一个字符串。

3.自动关闭loginwin窗口,并把刚才取回的字符串传给父窗口(index.xaml)显示。

现在问题是:

    我进入默认页后,点击按钮,第一次弹出loginwin窗口,点击确定后,并没有返回字符串,再点击默认页的按钮,再弹出loginwin窗口,这时候点确定后,却返回了wcf回复的字符串,规律就是loginwin窗口总是隔一次才能返回我想要的效果,必须是弹出的偶数次才会想wcf取回消息。

希望大家能帮我解决下,我新接触wcf和silverlight这一块,所以很多地方不是太明白,谢谢大家!

下面我贴一下几个类的代码:

Service.cs:wcf服务的后台代码

 

1 [ServiceContract(Namespace = "")]
2 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
3  public class Service
4 {
5 [OperationContract]
6 public void DoWork()
7 {
8 // 在此处添加操作实现
9   return;
10 }
11
12 // 在此处添加更多操作并使用 [OperationContract] 标记它们
13   [OperationContract]
14 public string ValidateUser(string username, string password)
15 {
16 if (username == "admin" && password == "123")
17 {
18 return username + " is login!";
19 }
20 else
21 {
22 return username + " or " + password + " is error!";
23 }
24 }
25 }

 

loginwin.xaml.cs:

 

1 public partial class loginWin : ChildWindow
2 {
3 public string msg;
4 public string MSG
5 {
6 get { return msg; }
7 set { msg = value; }
8 }
9 ServiceClient client;
10 public loginWin()
11 {
12 InitializeComponent();
13 }
14 //loginwin确定按钮事件
15   private void OKButton_Click(object sender, RoutedEventArgs e)
16 {
17 this.MSG = "OK";
18 client = new ServiceClient();
19 //client = ServerManager.GetPox();这个是跨域用的,也不用看,暂时没用
20   client.ValidateUserAsync(this.textBox1.Text, this.passwordBox1.Password);
21 //client.ValidateUserCompleted +=new EventHandler<ValidateUserCompletedEventArgs>(client_ValidateUserCompleted);
22   client.ValidateUserCompleted += (sender2, e2) => { MSG = e2.Result + ""; };
23 client.CloseAsync();
24 this.DialogResult = true;
25 this.Close();
26 }
27
28 private void CancelButton_Click(object sender, RoutedEventArgs e)
29 {
30 this.MSG = "Cancel";
31 this.DialogResult = false;
32 }
33 //这个不用看,上面用lambda表达式重新写过了
34   void client_ValidateUserCompleted(object sender2, ValidateUserCompletedEventArgs e) { MSG = e.Result + ""; }
35 }

 

index.xaml.cs:默认调用页面

 

1 public partial class index : UserControl
2 {
3 loginWin lw = new loginWin();
4 public index()
5 {
6 InitializeComponent();
7 }
8
9 private void hyperlinkButton1_Click(object sender, RoutedEventArgs e)
10 {
11 lw.Closed+=new EventHandler(lw_Closed);
12 lw.Show();
13 }
14 //当loginwin关闭时将值传给此页
15   void lw_Closed(object sender, EventArgs e)
16 {
17 loginWin lw = sender as loginWin;
18 this.label1.Content = lw.MSG;
19 }
20 }

 

Clientconfig文件:其中的地址没问题,这个是我用在iis内的地址,换成vs自带调试器的地址,问题同样

 

1 <configuration>
2 <system.serviceModel>
3 <bindings>
4 <customBinding>
5 <binding name="CustomBinding_Service">
6 <binaryMessageEncoding />
7 <httpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" />
8 </binding>
9 </customBinding>
10 </bindings>
11 <client>
12 <endpoint address="http://192.168.0.8/Service.svc?wsdl"
13 binding="customBinding" bindingConfiguration="CustomBinding_Service"
14 contract="ServiceReference1.Service" name="CustomBinding_Service" />
15 </client>
16 </system.serviceModel>
17  </configuration>

 

 

 

 

 

 

问题补充: 刚才我又仔细分下了下,loginwin跟wcf通信那两句是异步的吧,当点击按钮的时候结果已经返回到前台了,这个时候异步回来的数据并没有反映到界面上来,不知道对不对,那要实现传值的话,我应该怎么做呢
cnb_mtime的主页 cnb_mtime | 初学一级 | 园豆:58
提问于:2010-07-09 08:52
< >
分享
最佳答案
0

把 client.CloseAsync();      this.DialogResult = true;       this.Close(); 放到 ValidateUserCompleted += 的委托里去.

收获园豆:50
Launcher | 高人七级 |园豆:45045 | 2010-07-09 09:33
对哈,我咋没想到呢,厉害诶,一下就看出来了,我还想问一下,Silverlight窗口传值还有什么样的解决办法,这样太丑了- -
cnb_mtime | 园豆:58 (初学一级) | 2010-07-09 09:38
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册