badlander
badlander

Reputation: 41

Multiple instances of Service across AppDomains

So I'm trying to set up a logger in an application with multiple AppDomains. I want to create one central store in memory for all logged messages to go into. I've decided to go with a WCF named pipe binding, and I've created a log4net appender which creates a channel and sends all log events to the main service.

The problem is that I seem to get a new copy of the service in each AppDomain, and thus my single log ends up being multiple logs.

The setup is I have a service contract interface, ILogSink, and a singleton (actual singleton, and InstanceContextMode set to Single in Service Behavior attribute) service implementation ResultLog which has a collection for the logged messages. In my parent AppDomain I instantiate ServiceHost with ResultLog, and Open the service. Then I attach an Appender WcfResultAppender, which has a proxy copy of ILogSink created via a ChannelFactory CreateChannel call (to the service host address). On startup, each child AppDomain creates its own copy of WcfResultAppender.

I'm very new to WCF, what am I missing?

EDIT: I still haven't received any good responses to this - I've put the problem aside for now, but it is a pressing need. It seems as if I'm getting a different copy of my service class in each app domain, and for some reason the data is being left in those objects. I expected that there was something in the background that used the binding to resolve with the service created in the main appdomain and pass the data in via that, but something else is happening. I have read countless explanations and examples of WCF - one thing I noticed was none of the examples provide services which retain data. However I still can find nothing that contradicts or confirms my expectations.

Upvotes: 3

Views: 655

Answers (2)

Ilya Builuk
Ilya Builuk

Reputation: 2179

Consider the usage of Message Queueing as storage of your log messages

Upvotes: 0

Rich O'Kelly
Rich O'Kelly

Reputation: 41767

Each AppDomain is an isolated environment, they have no direct interaction with each other.

Consider exposing your service via IIS or WAS which will run it in its own (single) AppDomain.

See http://msdn.microsoft.com/en-us/library/ms733766.aspx for details of how to host a WCF service in IIS.

See http://msdn.microsoft.com/en-us/library/ms733109.aspx for details of how to host a WCF service in WAS.

Hope this helps.

Upvotes: 1

Related Questions