Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
370 views
in Technique[技术] by (71.8m points)

.net - How can a self-hosted (WinForm ) WCF service interact with the main form?

Simplified version of what I'm trying to achieve:

  • I have a WinForms app that runs hidden (Visible = false) in the background.
  • It's got only one Form, and I kept the default name - Form1
  • This WinForms app hosts a WCF Service. For now we'll call it the Listener service.
  • This Listener service has a function called "DisplayAlert()" that's exposed as a service function
  • An app sits on another machine sends a message to the Listener Service via a standard WCF service call

I've got all of the above working just fine. I can step through the code and watch the flow of messages as the DisplayAlert() function is called.

What I can't figure out, and I can't believe it's so hard to find how to do something this simple:

- I'd like the DisplayAlert() function in the hosted service interact directly with the WinForm that's hosting it to make the form visible.

All I want to do is set the Visibility to true, and call another function on the WinForm.

This seems to me like it should be as simple as adding a reference to the form, or making a public function on the form and calling it from the service class, but I can't even figure out how to reference Form1 from within the service class.

Am I missing something obvious? How do I even reference the instance of Form1, which is hosting the service?

I've gone down the path of....

  • Creating an event in the ListenerService (AlertReceived, virtual void OnAlertReceived), thinking that on the Form, I could add an event handler.
    • No dice. I'm not instantiating a ListenerService class directly, it's running in the ServiceHost.
  • Trying to reference the Application object from within the class, thinking I could reference it as Application.Form1, but nope. I can't even see the Application object from within the service class.
    • I'm probably missing something obvious here, but I'm not sure.

Any other suggestions?

I can add code if it helps.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Using this method you have completely a thread-safe application, and you don't have any limitation.

Service Contract Definition

[ServiceContract]
public interface IService
{
    [OperationContract]
    void DisplayAlert();
}

Service Implementation

public class Service:IService
{
    public void DisplayAlert()
    {
        var form = Form1.CurrentInstance;
        form.MySynchronizationContext.Send(_ => form.Show(), null);
    }
}

Program.cs

[STAThread]
static void Main()
{        
    var host = new ServiceHost(typeof (Service));
    host.Open();

    Application.SetCompatibleTextRenderingDefault(false);
    Application.EnableVisualStyles();
    Application.Run(new Form1());
 }

Form Implementation

public partial class Form1 : Form
{
    public static Form1 CurrentInstance;
    public SynchronizationContext MySynchronizationContext;
    private bool showForm = false;

    public Form1()
    {
        InitializeComponent();
        MySynchronizationContext = SynchronizationContext.Current;
        CurrentInstance = this;
    }

    // use this method for hiding and showing if you want this form invisible on start-up
    protected override void SetVisibleCore(bool value)
    {
        base.SetVisibleCore(showForm ? value : showForm);
    }

    public void Show()
    {
        showForm = true;
        Visible = true;   
    }

    public void Hide()
    {
        showForm = true;
        Visible = true;
    }
}

Client

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Press Enter to show form");
        Console.ReadLine();

        var client = new ServiceClient();
        client.DisplayAlert();
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...