First of all, the ServiceContract attribute must be on the interface, and not on the PrintMessage () method.
Using the revised version of your example, you can do it this way.
[ServiceContract]
public interface IService
{
[OperationContract]
string PrintMessage(string message);
}
public class Service : IService
{
public string PrintMessage(string message)
{
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();
Service.TextUpdater = ShowMessageBox;
ServiceHost myService = new ServiceHost(typeof(IService), uri);
}
public void ShowMessageBox(object sender, UpdateTextEventArgs e)
{
Invoke((MethodInvoker) delegate {
MessageBox.Show(e.Text);
} );
}
}
This is essentially a solution proposed by @Simon Fox, i.e. use delegate. Hopefully this just puts some meat on the bones, so to speak.
source
share