首先我的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>
把 client.CloseAsync(); this.DialogResult = true; this.Close(); 放到 ValidateUserCompleted += 的委托里去.