简体   繁体   中英

Self-hosted WCF service: How to access the object(s) implementing the service contract from the hosting application?

I am self-hosting a WCF service in a WPF client. I want to show the data the service receives in the user interface. Every time some data is received the user interface should 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 the object instance(s) implementing the service contract from the hosting WPF application?


Thanks everybody for the answers.

What I didn't see was that the constructor of ServiceHost allows to pass an of the service instead of its . 而不是其

So what I do now is:

  • Use an ObservableCollection in the service implementation
  • Configure the service to be a singleton (see theburningmonk's comment)
  • Bind to the ObservableCollection in my WPF application
  • Get an instance of the service using the databinding property DataContext
  • Pass it to the constructor of ServiceHost

Result: Every update in the singleton WCF service is reflected in the UI.

Happy!

As marc_s said, you're creating a PerCall/PerSession WCF service and a new instance is created on every request/first request of every session.

You could build some plumbing around it so that the instance can notify the service host when it receives a new request but it won't be an easy exercise and you need to be mindful of the potential for memory leak if you decide to go with using events to do this - without implementing the Weak Event Pattern your WCF service instances could be left hanging around as the event handlers still holds a reference to them UNLESS you remember to notify the host to unsubscribe when the WCF service instances are disposed.

Instead, here's two ideas which might make it easier for you to achieve your goal:

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

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

that way you will have access to the instance that will retrieve the client requests.

Alternatively, you could have all the hosted instances be dummy 'fascades' which share a static class that actually process the 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'd say you should go with the first approach if possible, makes life that bit easier!

There's not a single object instance of type MyService that is available - by default, WCF uses a per-call model, eg for each request that comes in, a new instance of MyService will be created, used to handle that request, and then freed.

Therefore, unless there's at least one running request, there most likely isn't any instance of the service around.

What exactly do you want to achieve with this?? Maybe you need to rethink your approach and go from the service class instance - it has a property .Host which links back to the host that is hosting that particular service instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM