Trước hết, thuộc tính ServiceContract phải nằm trên giao diện chứ không phải phương thức PrintMessage().
Sử dụng phiên bản được sửa chữa của ví dụ của bạn, bạn có thể làm theo cách này.
[ServiceContract]
public interface IService
{
[OperationContract]
string PrintMessage(string message);
}
public class Service : IService
{
public string PrintMessage(string message)
{
// Invoke the delegate here.
try {
UpdateTextDelegate handler = TextUpdater;
if (handler != null)
{
handler(this, new UpdateTextEventArgs(message));
}
} catch {
}
}
public static UpdateTextDelegate TextUpdater { get; set; }
}
public delegate void UpdateTextDelegate(object sender, UpdateTextEventArgs e);
public class UpdateTextEventArgs
{
public string Text { get; set; }
public UpdateTextEventArgs(string text)
{
Text = text;
}
}
public class MainForm : Form
{
public MainForm()
{
InitializeComponent();
// Update the delegate of your service here.
Service.TextUpdater = ShowMessageBox;
// Create your WCF service here
ServiceHost myService = new ServiceHost(typeof(IService), uri);
}
// The ShowMessageBox() method has to match the signature of
// the UpdateTextDelegate delegate.
public void ShowMessageBox(object sender, UpdateTextEventArgs e)
{
// Use Invoke() to make sure the UI interaction happens
// on the UI thread...just in case this delegate is
// invoked on another thread.
Invoke((MethodInvoker) delegate {
MessageBox.Show(e.Text);
});
}
}
Đây thực chất là giải pháp được đề xuất bởi @Simon Fox, tức là, sử dụng đại biểu. Điều này hy vọng chỉ cần đặt một số thịt trên xương, do đó, để nói chuyện.
Nguồn
2009-09-02 13:41:43
Điểm tốt về luồng. Điều này có thể là một nỗi đau :-) –