Search code examples
wcfinstanceservicehost

WCF: How to get the reference of created instance from ServiceHost (WCF 4.5)


In my server side console program, multiple ServiceHost objects are created and working in PerSession mode, not singleton. Therefore, a new server side object will be created when a new session started.

The problem is: How can I know which is the right ServiceHost object linked with the new created server side object? I do need this link information for further operations.

Can I get the ServiceHost information from the new created server side object? By converting it into another interface or class? Or, can I hook the instance creating process of ServiceHost?

Any idea?

Thanks a lot.


Solution

  • Try the OperationContext - it has a .Host property:

    var host = OperationContext.Current.Host;
    

    See the MSDN documentation for more details.