Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a Windows application and want to self-host a WCF in it. This MSDN article walks you through how to self-host WCF in a console. Jason Henderson's article demonstrates how to call the service. But the problem is, I don't want to host my service in another Windows process. I want to host it in my client application.

Here is my workaround:

  1. CtrlF5 to run the service
  2. Add service reference to my client application

Then I can start my service in my client like this:

static void Main()
{
    ServiceHost host = new ServiceHost(typeof(MyService));
    host.Open();
    Application.Run(new Form1());
    host.Close();
}

It works, but I wonder if there are any simpler ways to do this.

share|improve this question

closed as not a real question by svick, Michael K Jun 22 '12 at 22:14

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
That looks pretty simple to me... what exactly do you expect us to review? – Jeff Mercado May 31 '12 at 5:59
    
i'm looking for a better solution, not my ugly workaround :) – walterhuang May 31 '12 at 14:01
    
I vote to reopen and accept the answer below. OP asks for a review and he got a good one. – Casper Leon Nielsen Jan 10 '13 at 0:24
up vote 3 down vote accepted

You can also start host in another thread:

Task.Factory.StartNew(() =>
    {
            ServiceHost host = new ServiceHost(typeof(MyService));
            host.Open();
    };

(or using classic Thread and ThreadStart).

share|improve this answer
1  
You should consider marking this Task with the TaskCreationOptions.LongRunning option – MrWombat Jul 27 '15 at 6:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.