Exposing a winforms application internals through 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.

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.

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?

EDIT: 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:

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

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

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


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.


I assume you're using your singleton class as a service (it implements a contract). I suggest you develop a WCF contract and a service, which will make calls to your singleton instead.

So you'll have something like this:

public class YourImpportantSingleton { public YourImpportantSingleton Instance { get; set; } public void DoSeriousBusiness(){...} } [ServiceContract] public interface IYourContract { void YourRemoteAction(); } public class YourService : IYourContract { public void YourRemoteAction() { YourImportantSingleton.Instance.DoSeriousBusiness(); } }

UPD: Ok, just realized, that you might not be using self-hosting in a winforms application, sorry for that waste of time.

Your options would be then either host a service with ServiceHost in your forms application or host a service separately (with IIS, for example) and make this service a keeper of your singleton. You'll have to change your forms application to call separate service, as it holds the state now, of course.


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 multiple services same contract in same Config
  • Authenticating using OfficeDev/office-js-helpers rather than adal
  • What changes can I make to a cloud-endpoint (app-engine) return object before I need a new API versi
  • Obtain access token for both Microsoft Graph and individual service API endpoints (Outlook REST APIs
  • Radio button show hover between two radio button using jquery
  • Django Rest Framework: getting lists from query_params without request.getlist
  • Simple command-line app I/O in Dart
  • web shop (shopping cart) on google app engine
  • Using SWIG with a build system [closed]
  • Client side validation mvc dropdown
  • Cordova Apache wrong module path
  • Prolog Ambiguous Output
  • Separate ID and Class for JS and CSS
  • PHP multiple file uploads
  • Less Conflicting Session Manager for Zope 2
  • netsh acl setting (need alternative method - registry settings?)
  • how to set variables in a php include file?
  • How to pass a value from ASP.NET MVC controller to ASP.NET webforms control inside MVC View?
  • Ubuntu and bcrypt
  • Breaking out column by groups in Pandas
  • How solve “Qt: Untested Windows version 10.0 detected!”
  • How can I sort a a table with VBA with given text condition?
  • Time complexity of a program which involves multiple variables
  • why do I get the error when installing the gem 'pg'? [duplicate]
  • FileReader+canvas image loading problem
  • Recording logins for password protected directories
  • Why value captured by reference in lambda is broken? [duplicate]
  • Is my CUDA kernel really runs on device or is being mistekenly executed by host in emulation?
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • TFS: Get latest causes slow project reloading
  • Controls, properties, events and timers running in design time
  • How to handle AllServersUnavailable Exception
  • In LanguageTool, how do you create a dictionary and use it for spell checking?
  • How do I rollback to a specific git commit
  • Angular 2 constructor injection vs direct access
  • Understanding cpu registers
  • How do I configure my settings file to work with unit tests?
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • Change div Background jquery
  • How do I use LINQ to get all the Items that have a particular SubItem?