我很新的編程,所以如果我的問題不夠具體,請耐心等待。現在我正在嘗試將一個簡單的客戶端登錄到我的服務器。所以服務器應用程序知道哪些用戶連接。當客戶端連接時,我想要在更新用戶列表的服務器上觸發一個事件。但它不,我不明白爲什麼。希望你能幫助。Wpf服務器/客戶端登錄。問題與事件
在代碼中,我已經刪除了用戶應該如何顯示在serverApp中。現在我只需要這個活動就可以工作。
在我的服務庫:
INetworkService合同:
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();
}
}
網絡服務類:
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);
}
}
}
}
的UserInfo類:
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;
}
}
在我ServerApp(WPF):
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!");
}
}
}
在我ClientApp(WPF):(已經取得了服務引用到服務器)
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);
}
}
}
通過使用什麼ilya.dorofeev說關於使NetworkService [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]的一個實例,並提供主機與您所描述的那個實例,它的工作原理..謝謝:) – user1077186 2012-04-10 09:34:50