本文介绍了如何能与主窗体自托管(WinForm的)WCF服务进行交互?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么我想要实现的简化版本:

Simplified version of what I'm trying to achieve:

  • 我在后台WinForms应用程序运行隐藏的(visible = FALSE)。
  • 在它有只有一种形式,我不停的默认名称 - Form1中
  • 在此WinForms应用程序承载WCF服务。现在我们把它称为监听器服务。
  • 在此监听器服务有一个名为DisplayAlert()这是公开为服务功能功能
  • 在一个应用程序坐在另一台机器上通过一个标准的WCF服务调用将消息发送到监听器服务

我已经得到了所有上述的工作就好了。我可以通过code步骤和观看消息作为DisplayAlert()函数被调用的流程。

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:

- 我想直接与的托管它使窗体的WinForm的托管服务进行交互的DisplayAlert()函数可见

我想要做的是设置能见度为true,并呼吁在WinForm另一个函数。

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

这似乎对我来说,它应该是简单,只要添加一个参考的形式,或使窗体上的公共职能和服务类调用它,但我甚至无法弄清楚如何引用Form1中从内的服务类

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.

我失去了一些东西明显?如何我甚至引用Form1的实例,它是承载服务?

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....

  • 在ListenerService(AlertReceived,虚拟无效OnAlertReceived)创建活动,认为在表上,我可以添加一个事件处理程序。
    • 没有骰子。我不实例化一个ListenerService类直接,它在ServiceHost的运行。
    • 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.
      • 我可能失去了一些东西在这里很明显,但我不知道。

      任何其他建议?

      我可以添加code。如果它帮助。

      I can add code if it helps.

      推荐答案

      使用这种方法,你已经完全线程安全的应用程序,并且没有任何限制。

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

      服务合同定义

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

      服务实现

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

      Program.cs的

      Program.cs

      [STAThread]
      static void Main()
      {        
          var host = new ServiceHost(typeof (Service));
          host.Open();
      
          Application.SetCompatibleTextRenderingDefault(false);
          Application.EnableVisualStyles();
          Application.Run(new Form1());
       }
      

      形式实施

      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;
          }
      }
      

      客户端

      class Program
      {
          static void Main(string[] args)
          {
              Console.WriteLine("Press Enter to show form");
              Console.ReadLine();
      
              var client = new ServiceClient();
              client.DisplayAlert();
          }
      }
      

      这篇关于如何能与主窗体自托管(WinForm的)WCF服务进行交互?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 01:06