Click to See Complete Forum and Search --> : WCF Singleton question
Grofit
June 30th, 2009, 06:36 AM
Hey,
I have a number of services within the same project, and i have some data that i would like to be shared in one place, so all the services in the project can access it. Im not really sure if it is possible but can i just simply do something like define a static object in the singleton service then from another service just do something like:
ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
class MySingleton : IMyContract
{
static MyClass m_Object;
public MySingleton( )
{
m_Object = new MyClass();
}
}
class MyPerCall : IMyOtherContract
{
public int GetSomeData()
{
return MySingleton.m_Object.SomeIntProperty;
}
}
I dont mind using named pipes or whatever if needed, i just want the fastest way of being able to have a few persistant objects that are partially accessible to other services.
Grofit
July 1st, 2009, 03:12 AM
Quick bump, just so i can get any useful intel before i try implementing something like this later one...
dannystommen
July 1st, 2009, 03:34 AM
When you search msdn on singleton you will find the next example
http://msdn.microsoft.com/en-us/library/ms998558.aspx
Implementing this in you case would mean
public class MySingleton : IMyContract
{
private static MySingleton instance;
private static readonly object _lock = new object();
public static MySingleton Instance
{
get
{
lock(_lock){
if (instance == null)
{
instance = new MySingleton ();
}
return instance;
}
}
}
}
boudino
July 1st, 2009, 04:04 AM
1. I would prefer to use WCF's itself to manage lifecycle services, including singleton.
2. I would delegate the share data other than service implementatin class, which can be singleton.
3. I would think about using RDBM for storing shared data.
Grofit
July 1st, 2009, 04:49 AM
Ah all good stuff guys thanks alot... i basically have some server logic already, but it has hooks where data needs to be fed in and gets fed out, so i want to basically wrap it in a WCF singelton that stores the main object as a wrapper, then have lots of smaller WCF services to make use of each chunk of relevent data in the object...
So an example would be something like pulling a name and an ID from the object to use in a service that displays other stuff from the DB pertaining to the ID... if that makes sense...
One other thing that im just curious about:
When you search msdn on singleton you will find the next example
http://msdn.microsoft.com/en-us/library/ms998558.aspx
Implementing this in you case would mean
public class MySingleton : IMyContract
{
private static MySingleton instance;
private static readonly object _lock = new object();
public static MySingleton Instance
{
get
{
lock(_lock){
if (instance == null)
{
instance = new MySingleton ();
}
return instance;
}
}
}
}
In this example wouldnt the WCF service create its own instance when its called, so when you try to get the instance you would have 2 singletons going? or can you just do something like:
public class MySingleton : IMyContract
{
private static readonly object _lock = new object();
public static MySingleton Instance
{
get { return this; }
}
}
dannystommen
July 1st, 2009, 05:06 AM
In this example wouldnt the WCF service create its own instance when its called, so when you try to get the instance you would have 2 singletons going? or can you just do something like:
public class MySingleton : IMyContract
{
private static readonly object _lock = new object();
public static MySingleton Instance
{
get { return this; }
}
}
[/QUOTE]
Have you tried to compile it? You will get an error : "Keyword 'this' is not valid in a static property, static method, or static field initializer"
Grofit
July 1st, 2009, 05:18 AM
Have you tried to compile it? You will get an error : "Keyword 'this' is not valid in a static property, static method, or static field initializer"
hahah i cant believe i didnt realise that when typing, could you do something like OperationContext.CurrentHost or something like that? (i cant remember exact syntax). I dont have anything against changing the constructor to assign to the _instance if needed...
Although saying that im just assuming that it has made 2 singleton instances in your previous example, one for the WCF service and one for the _instance, so if im wrong then there is no problem...
Also im not at my dev computer atm im at work, i just like to think ahead so whatever knowledge i get from you guys i will be using when i try to implement later.
Arjay
July 2nd, 2009, 03:36 AM
I'd separate the WCF class implementation with the underlying functionality implementation.
Just create the WCF Service as you've mentioned...
[ServiceBehavior( InstanceContextMode = InstanceContextMode.Single )]
and then have its methods call into the static singleton class as Danny described. What that class does internally is up to you (internally it can call another WCF class).
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.