开发者

How do I access singleton/initialization data from a WCF Service?

开发者 https://www.devze.com 2023-02-22 06:50 出处:网络
I have created a Windows Service + WCF Service project a la MSDN\'s How to: Host a WCF Service in a Managed Windows Service.

I have created a Windows Service + WCF Service project a la MSDN's How to: Host a WCF Service in a Managed Windows Service.

In my situation, though, I have a timer initialized by the Windows Service's OnStart. I want the WCF service to be able to access, query, and interface with the Windows Service's data, for instance, to start and stop the timer.

I don't want to create the timer in the W开发者_StackOverflowCF service class so that the WCF class's lifetime can be PerCall (right? or should I just change that?). How can the WCF class find the timer object?


Quite honestly: it cannot.

Your Windows Service will contain a ServiceHost, which is the hosting environment for the WCF service calls. Inside a WCF service call, you can access its .Host - e.g. the instance of ServiceHost that's running inside your Windows NT Service - but you cannot reach "further out" that that.

To access the host for a given service method, use this line of code:

ServiceHost yourHost = OperationContext.Current.Host;

So what you need to look into is a way to make that timer available on the service host object - e.g. by creating your own custom ServiceHost descendant which has access to / contains that timer in question.

I don't think there's any other way to "reach out" from within a WCF service call.


I always recommend any advice that @marc_s provides. In this case, though, count me confused.

If the timer is a member of your Windows service, then you have a couple of options. You could add a public static method to your Windows service class that returns the Windows service instance, or you could add some public static methods to your Windows service class that provides an interface to the timer directly.

Be aware that since the WCF service call runs in its own thread, you'll want to synchronize access to the timer so that you don't have multiple WCF service calls trying to access it at the same time.

Also, the assembly containing your WCF service will have to reference the Windows service assembly in order for this to work. That may present some issues that have to be worked around.

In short, I think this can be done using static methods/instances. Just make sure you address the multi-threading issues.


You can do this with ECollective - ECollective hosts WCF services as well as timer services in the same container, so you can communicate between the two.

0

精彩评论

暂无评论...
验证码 换一张
取 消