tags:

views:

38

answers:

2

I need to share my application statistics via a wcf service. I'm able to self-host my wcf service but ServiceHost object constructor is default leaving me to unable to initialize other member variables of my service.

Sample:

public interface IService
{
   //some operations here 
}

public class Service : IService
{
  object myObject;
  //implementation of IService
}

myObject is my console application object ( List ) and I wanted to make my service to be able to look into it. Is there any way I can reference it on my WCF Service?

A: 

You could use this approach to the get handle on your ServiceHost instance in your service implementation code:

ServiceHostBase base = OperationContext.Current.Host;

Now you have a link to to your service host - anything publicly available and accessible can be read (preferably not set, since there might be many service class instances running).

marc_s
Easy to use but difficult to unit test.
Darin Dimitrov
A: 

You could provide an instance of the service yourself by creating a custom IInstanceProvider.

Darin Dimitrov
I'll try the IInstanceProvider. Do I need to add something on my app.config file? The sample on the link uses a class that implements IContractBehaviorAttribute, IContractBehavior but it does not show how it was instantiated. I'm hosting my service like this:ServiceHost dccaService = new ServiceHost(typeof(CreditControl.SOA.SOAP.Service));dccaService.Open();
powerbox
An `IServiceBehavior` is needed in order to register the custom instance provider. Then you could also have a custom `ServiceHost`. Here's another link that demonstrates the whole process: http://initializecomponent.blogspot.com/2008/06/integrating-unity-with-wcf.html
Darin Dimitrov
I've created a class that implements IServiceBehavior, a class that implements IInstanceProvider and a class that implements IContractBehaviorAttribute and IContractBehavior. The creation of my service will be the same just like I posted above. I'm expecting my class the implements IServiceBehavior will be available on SvcConfigEditor.exe under extension when I navigate my dll file but it is not. Am I doing it correctly?
powerbox
Thanks again Darin got it working.
powerbox