Note that there are some explanatory texts on larger screens.

plurals
  1. POWpf Server/client logon. Problems with events
    text
    copied!<p>I’m pretty new to programming, so bear with me if my question isn’t specific enough. Right now I’m trying to make a simple Client Logon to my server. So the server App knows which users are connected. When a client connects I want an event to fire on the server that update the userlist. But it doesn’t and I can’t figure out why. Hope you can help.</p> <p>In the codes I have removed how the users should be displayed in the serverApp. Right now I just need the event to work. </p> <p><strong>In my Service Library:</strong></p> <p>INetworkService contract:</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace NetworkLib { [ServiceContract] public interface INetworkService { [OperationContract] void Logon(UserInfo userInfo); [OperationContract] void Logout(); } } </code></pre> <p>NetworkService Class:</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace NetworkLib { public class NetworkService : INetworkService { public event EventHandler UserListChanged; public void Logon(UserInfo userInfo) { OnUserListChanged(); } public void Logout() { OnUserListChanged(); } private void OnUserListChanged() { var handler = UserListChanged; if (handler != null) { handler(this, EventArgs.Empty); } } } } </code></pre> <p>UserInfo Class:</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.Serialization; namespace NetworkLib { [DataContract] public class UserInfo { [DataMember] public string Name; } } </code></pre> <p><strong>In my ServerApp (WPF):</strong> </p> <pre><code>using System.ServiceModel; using NetworkLib; namespace ServerApp { public partial class MainWindow : Window { NetworkService networkService; public MainWindow() { InitializeComponent(); ServiceHost host = new ServiceHost(typeof(NetworkService)); host.Open(); networkService = new NetworkService(); networkService.UserListChanged += networkService_UserListChanged; } private void networkService_UserListChanged(object sender, EventArgs e) { MessageBox.Show("It Works!"); } } } </code></pre> <p><strong>In my ClientApp (WPF): (Have made a Service Reference to the Server)</strong></p> <pre><code>namespace ClientApp { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void button1_Click(object sender, RoutedEventArgs e) { ServiceReference.NetworkServiceClient proxy = new ServiceReference.NetworkServiceClient(); ServiceReference.UserInfo userInfo = new ServiceReference.UserInfo(); userInfo.Name = "Test"; proxy.Logon(userInfo); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload