Shiraz Bhaiji
Shiraz Bhaiji

Reputation: 65461

Running a WCF listener in a WPF application

We would like to be able to communicate with a WPF application from the server.

Is it possible to have a WCF listener / service in a WPF application? And then call this service to open a screen in the WPF application?

Upvotes: 1

Views: 2344

Answers (2)

Merlyn Morgan-Graham
Merlyn Morgan-Graham

Reputation: 59151

Is it possible to have a WCF listener / service in a WPF application

It is fairly simple to create a WCF service listener/server anywhere you want.

var servicehost = new ServiceHost(typeof(SomeService))
servicehost.Open();

One problem is that you have to have enough permissions to have your host be visible. You might have to elevate your application, and would definitely have to make sure the firewall (software/hardware) allows traffic to reach it.

This link seems to cover network setup for WCF MSDN samples, and applies both to IIS hosting, as well as your case, non-IIS hosted WCF:

http://msdn.microsoft.com/en-us/library/ms751527(v=vs.90).aspx

Also, you may run into threading complications, though you'll run into these in any case where you are trying to update the UI from a background thread. If you have problems with this, look into the Dispatcher:

http://msdn.microsoft.com/en-us/magazine/cc163328.aspx

After that, it is up to you to create a client/server design that ensures that your service is created and listening at the right times, torn down at the right times (since ServiceHost is IDisposable), and that it handles state correctly (in case operations get called at times you aren't expecting - there are always bugs in any piece of software).

And then call this service to open a screen in the WPF application

WPF creates code that you can invoke more or less the same way you would in WinForms. You can still do a new MainWindow().Show() call, for example. So simply add such code to your service implementation.

Upvotes: 3

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364389

Yes you can host WCF service in both WinForms and WFP applications - MSDN contains some sample. Depending on the way how you host the service you have to deal with UI interaction differently - there is difference between hosting in UI and other thread because other threads cannot access UI controls directly.

Upvotes: 1

Related Questions