Reputation: 21198
I created a WCF service library. Then I created a Windows service to host this WCF service. Now I want to call a function defined in WCF service library within same window service.
protected override void OnStart(string[] args)
{
if (serviceHost != null)
serviceHost.Close();
// Create a ServiceHost for the Registration type and
// provide the base address.
serviceHost = new System.ServiceModel.ServiceHost(typeof(Registration));
// Open the ServiceHostBase to create listeners and start
// listening for messages.
serviceHost.Open();
Registration r = new Registration();
System.Threading.Thread t = new System.Threading.Thread(r.ReadAttempt);
Object passParameterToCallback = null;
t.IsBackground = false;
t.Start(passParameterToCallback);
}
Upvotes: 2
Views: 827
Reputation: 43718
If you just need to call a method on the same class, then you are already doing that in your code by just creating a new Registration()
and calling a method on it. There is no reason to call through WCF.
If for some reason you do want to call your service through WCF to yourself, then you should construct a client instead of the actual class.
If you need to call the same instance of your WCF service object, then you will have to have your WCF service set to Single, and then call your WCF client.
Or use this constructor for ServiceHost
that takes in a singleton object, then call methods on the same object instance, or then use the ServiceHost.SingletonInstance
property to get back that instance that was passed to the constructor.
Upvotes: 2
Reputation: 308
ServiceHost class should also be able to take in an singleton instance of the service instead of a type, according to this: http://msdn.microsoft.com/en-us/library/ms585487.aspx
So, try instantiating the class of type Registration, and pass that to ServiceHost. This only works like I said though, with a singleton type WCF service, or more specifically one where you have InstanceContextMode set to Single.
That way you should be left with a reference to the class, and should be able to access it fine.
It might be worth thinking of it all in another way though. If your problems is just notifying the windows service about things happening in the WCF service, how about defining an event inside your WCF service your windows service could subscribe to and receive events from your WCF service in that way?
If it is logic that you need to use inside your WCF service, how about extracting that logic to an external class or making it static, so you can use it anywhere.
Good luck
Upvotes: 1