通过WCF公开Winforms应用程序内部 [英] Exposing a winforms application internals through WCF

查看:53
本文介绍了通过WCF公开Winforms应用程序内部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试迈向IPC和WCF的第一步,到目前为止,我的脸一直不知所措.我有一个Winforms应用程序,我想对远程调用者进行一些测试. Winforms应用程序的大部分业务逻辑都局限于一个单独的实例,该实例负责所有后台工作.我想通过IPC机制公开一些功能. WCF似乎是前进的方向,所以我从那开始.

I am trying to take my first steps into IPC and the WCF, and so far, I'm falling flat on my face. I have a Winforms application that I would like to instrument somewhat for remote callers. The winforms application has most of its business logic confined to a singleton that takes care of all the background work. I would like to expose some of the functionality through an IPC mechanism. WCF seems like the way forward, so I started out with that.

我尝试的是在解决方案中添加一个WCF服务库项目,我希望通过该项目公开一些调用.当我在VS调试器中启动Winforms项目时,它正常运行,并且WcfSvcHost启动.我可以使用WcfTestClient与WCF服务进行通信.

What I tried is adding a WCF Service Library project to my solution, through which I would like to expose some calls. When I start the Winforms project in the VS debugger, it runs as normal, and the WcfSvcHost starts up. I can communicate to the WCF service with the WcfTestClient.

但是,当我尝试访问包含要与之通信的代码的单例时,似乎正在获取一个新的单例对象.显然,我做错了.我猜测发生的是该服务在不同的进程中运行,因此没有真正的共享代码,因此也没有共享的单例.

However, when I try to access the singleton holding the code I would like to communicate with, it seems like I am getting a new singleton object. Clearly, I'm doing it wrong; what I guess is happening is that the service runs in a different process, so there is no real shared code, and hence no shared singleton.

我不确定该如何继续.我选择将WCF用于IPC是错误的选择吗?我应该将WCF端点集成到Winforms应用程序中吗?我尝试的方法是否可行?

I'm not sure how I should continue. Is my choice of using WCF for IPC the wrong one? Should I integrate the WCF endpoints in the Winforms application? Is what I'm trying even feasible?

我认为这是如此高级,而且也是如此简单,以至于任何代码示例都将毫无用处.我想我错了.所以一些代码:

I figured this was so high-level, and also so simple that any code sample would be useless. I think I was wrong. So some code:

在WinForms程序集中:

In the WinForms assembly:

public partial class Form1 : Form
{
  public Form1()
    {
      InitializeComponent();
      label1.Text = MySingleton.Instance.InitedAt.ToString();
    }
}

public class MySingleton
{
  private static MySingleton instance = new MySingleton();
  private DateTime inited;

  private MySingleton()
  {
    this.inited = DateTime.Now;
  }

  public static MySingleton Instance
  {
    get
    {
      return instance;
    }
  }

  public DateTime InitedAt
  {
    get
    {
      return this.inited;
    }
  }
}

在WCFServiceLibrary程序集中:

In the WCFServiceLibrary assembly:

[ServiceContract]
public interface IApplicationProbe {

  [OperationContract]
  string DoesItWork();

  [OperationContract]
  string SingletonInited();
}

public class ApplicationProbe : IApplicationProbe {
  public string DoesItWork(){
    return "Why yes, yes it does";
  }

  public string SingletonInited(){
    return MySingleton.Instance.InitedAt.ToString();
  }
}

当我通过WcfTestClient查询SingletonInited时,得到的InitedAt与Winforms单例的实例化日期时间不同.

when I query SingletonInited through the WcfTestClient, I get an InitedAt which is not the same DateTime as the instatiation of the winforms singleton.

Edit2:

我按原样运行此代码(使用自动生成的脚手架围绕Winforms东西).表单上的标签显示的时间不同于WCF调用返回的时间,表明它是一个不同的实例.

I have this code running as is (with the auto-generated scaffolding around the Winforms stuff). The label on the form displays a different time than the time returned from the WCF call, demonstrating it is a different instance.

推荐答案

此处的问题是WCF服务主机在托管服务,而不是应用程序本身.这导致应用程序在单独的ApplicationDomain中运行,进而导致创建新的单例.改用自托管解决了这个问题.

The problem here was that WCF Service Host was hosting the service, rather than the application itself. This caused the application to run in a seperate ApplicationDomain, which in turn caused a new singleton to be created. Switching to self-hosting solved the problem.

这篇关于通过WCF公开Winforms应用程序内部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆