Let’s assume I have a windows forms application that should be remote-controlled or influenced from a remote location with .net.
As far as I’ve googled, hosting a WCF service inside that application would be the way to go. I’ve successfully added a WCF service to the application and can start it with
ServiceHost host = new ServiceHost(typeof(Service1));
host.Open();
What would be a good way to get references to classes from the rest of the running application?
I guess there are only these 2 ways:
- Use of static properties to call other methods / set properties from within the service method.
- Assign references to the service class? (How do I assign values to the service hosted within the service host? )
What is considered a good practice or rather which way has worked for you in the past?
Update based on comment
I’m guess I’m looking for something like this:
ServiceHost host = new ServiceHost(typeof(Service1));
host.Open();
(Service1)host.MyProperty = "asd";
I can’t seem to find how to cast the ServiceHost (or a property of it) to an instance of Service1. That would probably solve all my problems 😉
You can’t really do what you added based on the comment:
Because at that point no instances of the class
Service1have been created yet. And they’ll only be created when a new request arrives for the service.One alternative is to use a custom instance provider (shown in the code below), where you have a reference to the service instance before it’s used by the WCF runtime. You can read more about instance providers at http://blogs.msdn.com/b/carlosfigueira/archive/2011/05/31/wcf-extensibility-iinstanceprovider.aspx.