I'm developing a WCF service that using MS CRM Services. I need to initialize service instance in multiple places and this take long time to init. I solved this problem with implementing singleton pattern as below.
public sealed class MSCRM
{
private static readonly MSCRM instance = new MSCRM();
private static readonly IOrganizationService service =GetOrgService(true);
static MSCRM() { }
private MSCRM() { }
private static MSCRM Instance { get { return instance; } }
public static IOrganizationService Service { get { return service;} }
private static readonly object LockThread = new object();
private static IOrganizationService GetOrgService(bool admin = false, string callerId = null)
{
}
}
But I need to pass parameters that in my GetOrgService method. How can I do this ?
EDIT: I changed my code and added public GetService method. But this time when I call my service from multiple clients at the same time, service throws "cannot access a disposed object" exception. How can I make my IOrganizationService property thread-safe and singleton.
public sealed class MSCRM
{
private static readonly MSCRM instance = new MSCRM();
private static IOrganizationService service;
static MSCRM() { }
private MSCRM() { }
public static MSCRM Instance { get { return instance; } }
public IOrganizationService GetOrgService(bool admin = false, string callerId = null)
{
return service ?? (service = GetService(admin, callerId));
}
private static IOrganizationService GetService(bool admin = false, string callerId = null)
{
}
}