WCF, Accessing a windows forms controls from a service

后端 未结 5 830
南旧
南旧 2021-01-05 22:17

I have a WCF service that is hosted inside a Windows Form.

How can I access the controls of the form from the methods in my service?

for example I have

5条回答
  •  清歌不尽
    2021-01-05 23:09

    First of all, the ServiceContract attribute should be on the interface, not the PrintMessage() method.

    Using a corrected 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)
        {
            // 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);
            } );
        }
    }
    

    This is essentially the solution suggested by @Simon Fox, i.e., use a delegate. This hopefully just puts some flesh on the bones, so to speak.

提交回复
热议问题