WCF stand-alone service: how to access the object (s) executing a service contract from a hosting application?

I myself accept the WCF service in the WPF client. I want to show the data received by the service in the user interface. Every time some data is received, the user interface must be updated.

The code in "App.xaml.cs" looks like

private ServiceHost _host = new ServiceHost(typeof(MyService)); private void Application_Startup(object sender, StartupEventArgs e) { _host.Open(); } private void Application_Exit(object sender, ExitEventArgs e) { _host.Close(); } 

How can I get an instance of an object that implements a service contract from a WPF hosting application?


Thanks to everyone for the answers.

I have not seen that the ServiceHost constructor allows you to pass an instance of a service instead of its type .

So now I do the following:

  • Use ObservableCollection in service implementation
  • Configure the service as a singleton (see theburningmonk comment)
  • Snap to ObservableCollection in my WPF application
  • Get a service instance using the databinding DataContext property
  • Pass it to the ServiceHost constructor

Result: every update in the singleton WCF service is reflected in the user interface.

FROM

+6
c # data-binding wpf wcf
source share
2 answers

As marc_s notes, you create the WCF PerCall / PerSession service, and each request / first request of each session is created by a new instance.

You can build such plumbing around it so that the instance can notify the service host when it receives a new request, but this will not be an easy exercise, and you need to remember about a potential memory leak if you decide to use events for this - without implementation the weak event pattern, your WCF service instances may be left out, because event handlers still contain a link to them IF you do not want to notify the host that you are unsuccessful when hosting WCF service instances.

Instead, there are two ideas that could help you achieve your goal:

Use the Single InstanceContextMode if your service can be made singleton, in which case you will create a new instance that implements your service contract and places it:

 // instance will be your WCF service instance private ServiceHost _host = new ServiceHost(instance); 

this way you will gain access to the instance that will receive client requests.

Alternatively, you could have all hosted instances as dummy β€œfacades” that share a static class that actually handles requests:

 [ServiceContract] interface IMyService { ... } interface IMyServiceFascade : IMyService { ... } // dummy fascade [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall] public class MyServiceFascade : IMyServiceFascade { private static IMyService _serviceInstance = new MyService(); public static IMyService ServiceInstance { get { return _serviceInstance; } } public int MyMethod() { return _serviceInstance.MyMethod(); } ... } // the logic class that does the work public class MyService : IMyService { ... } // then host the fascade var host = new ServiceHost(typeof(MyServiceFascade)); // but you can still access the actual service class var serviceInstance = MyServiceFascade.ServiceInstance; 

I would say that you should go with the first approach, if possible, make life easier!

+12
source share

There is no instance of an object of type MyService that is available - by default, WCF uses a model for each call, for example. for each incoming request, a new instance of MyService will be created, used to process this request, and then released.

Therefore, if there is not even one running request, most likely it is not any instance of the service.

What exactly do you want to achieve with this? Perhaps you need to rethink your approach and move from an instance of a service class - it has a .Host property that refers to the host that hosts this particular service instance.

+1
source share

All Articles