3

I need initialize some structure in wcf service class as soon as possible after start of hosting service.

Now I host it in WinForms application and there I initialize this static structure.

ServiceHost host = new...
host.Open()...
new MyServiceClient().Initialize();

But I'm sure there is better way to do this. I know now it is inelegant solution...maybe little wcf-client running as windows process? (this client should be responsible only for calling this initializing method)

Saint
  • 5,397
  • 22
  • 63
  • 107
  • Question: why are you hosting this in a WinForms application instead of a console/Windows service? And what specifically do you want to do in your initialization? – Garrett Vlieger Jul 05 '12 at 17:01

1 Answers1

0

You can use the constructor of the service to do whatever initialization work is required.

// The service
public class MyService : IMyService {
    // Constructor
    public MyService() {
        // Initialize things here
    }
}

If the initialization work should only be performed once, you can make the constructor static. If the things being constructed are not static, you can make the service a singleton, depending on your performance needs. You can decorate the service with the following code to do that:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] 
Trey Combs
  • 710
  • 5
  • 10