469,366 Members | 2,243 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,366 developers. It's quick & easy.

How to access the ServiceHost object from the WCF service

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!!!
Aug 15 '07 #1
10 18539
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]
- mv*@spam.guard.caspershouse.com

"Chris Bordeman"
<RE*********************************************** ***@hhoottmmaaiill.com>
wrote in message news:%2******************@TK2MSFTNGP03.phx.gbl...
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!!!


Aug 15 '07 #2
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
Aug 15 '07 #3
I guess, kind of a newbie tho, how do I access OperationContext?

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:eV**************@TK2MSFTNGP02.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Chris Bordeman"
<RE*********************************************** ***@hhoottmmaaiill.com>
wrote in message news:%2******************@TK2MSFTNGP03.phx.gbl...
>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!!!



Aug 15 '07 #4
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

Aug 15 '07 #5
http://msdn2.microsoft.com/en-us/lib...oncontext.aspx

Its like a singleton so just type OperationContext.Current when you
need it.

Aug 15 '07 #6
Chris,

James has posted how to access the ServiceHost from the
OperationContext.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Chris Bordeman"
<RE*********************************************** ***@hhoottmmaaiill.com>
wrote in message news:%2****************@TK2MSFTNGP06.phx.gbl...
>I guess, kind of a newbie tho, how do I access OperationContext?

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:eV**************@TK2MSFTNGP02.phx.gbl...
>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]
- mv*@spam.guard.caspershouse.com

"Chris Bordeman"
<RE********************************************** ****@hhoottmmaaiill.com>
wrote in message news:%2******************@TK2MSFTNGP03.phx.gbl...
>>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!!!




Aug 15 '07 #7
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.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"james" <ja********@gmail.comwrote in message
news:11**********************@w3g2000hsg.googlegro ups.com...
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

Aug 15 '07 #8
That makes sense -- I had forgotten about ThreadStatic.

Thanks Nicholas

-James

Aug 15 '07 #9
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/archiv...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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:eV**************@TK2MSFTNGP02.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Chris Bordeman"
<RE*********************************************** ***@hhoottmmaaiill.com>
wrote in message news:%2******************@TK2MSFTNGP03.phx.gbl...
>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!!!



Aug 15 '07 #10
Gonna try it, thanks.

"james" <ja********@gmail.comwrote in message
news:11**********************@b79g2000hse.googlegr oups.com...
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

Aug 15 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Doug Bailey | last post: by
4 posts views Thread by Doru Roman | last post: by
2 posts views Thread by Jayender | last post: by
reply views Thread by Chris Bordeman | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.