How to access the ServiceHost object from the WCF service

  • Thread starter Thread starter Chris Bordeman
  • Start date Start date
C

Chris Bordeman

Hi all.

I created a custom ServiceHost object and did a bunch of initialization
there. Now I need to access an object in it from the actual WCF service
class. How can this be done?

Thanks!!!
 
Chris,

You shouldn't be accessing the ServiceHost directly. Usually, through
the OperationContext, you can get the information that you need.

What is it that you are trying to get?
 
Chris,

Use the OperationContext from within a service operation to access the
current operation execution environment. To access the ServiceHost
use OperationContext.Current.Host.

Regards,
James
 
I guess, kind of a newbie tho, how do I access OperationContext?

Nicholas Paldino said:
Chris,

You shouldn't be accessing the ServiceHost directly. Usually, through
the OperationContext, you can get the information that you need.

What is it that you are trying to get?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

"Chris Bordeman"
Hi all.

I created a custom ServiceHost object and did a bunch of initialization
there. Now I need to access an object in it from the actual WCF service
class. How can this be done?

Thanks!!!
 
Nick,

This is off topic, but what kind of design pattern is
OperationContext?

I don't understand how OperationContext.Current can store a static
reference to the current context and still work on multiple threads.
Is something in WCF setting the context before calling an operation?

Thanks,
James
 
Chris,

James has posted how to access the ServiceHost from the
OperationContext.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

"Chris Bordeman"
I guess, kind of a newbie tho, how do I access OperationContext?

Nicholas Paldino said:
Chris,

You shouldn't be accessing the ServiceHost directly. Usually, through
the OperationContext, you can get the information that you need.

What is it that you are trying to get?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

"Chris Bordeman"
Hi all.

I created a custom ServiceHost object and did a bunch of initialization
there. Now I need to access an object in it from the actual WCF service
class. How can this be done?

Thanks!!!
 
James,

I don't know what kind of design pattern it is. I would say the closest
thing is a singleton, but it's a singleton in a VERY limited context (single
call on the single thread).

WCF does set the context before calling an operation that is exposed.
What it does is set a variable that is marked with the ThreadStatic
attribute. Either that, or the value is stored in thread-local storage.
This is what enables different threads to access a static property and get
different OperationContext instances.
 
Well, what I'm doing is loading some extra information, setting up some
AppDomains that the service needs to later access. I need this to happen
when the service first initializes. And I'm using IIS for now. According
to http://blogs.msdn.com/wenlong/archive/2006/01/11/511514.aspx I can do
initialization like this 3 ways, but a derived ServiceHost class seems the
cleanest.

It occurs to me I can put a static member in my derived ServiceHost class.
Would there be any drawback to doing it this way?

Nicholas Paldino said:
Chris,

You shouldn't be accessing the ServiceHost directly. Usually, through
the OperationContext, you can get the information that you need.

What is it that you are trying to get?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

"Chris Bordeman"
Hi all.

I created a custom ServiceHost object and did a bunch of initialization
there. Now I need to access an object in it from the actual WCF service
class. How can this be done?

Thanks!!!
 
Back
Top