I'm trying to create a singleton object which should be in use for each call to my server. But for some reason the static object is keep renew for each call to the Web API. Is there any way to stop it? What can I do?
Thanks for the answers, Here is some code:
The Singleton:
private static CoreEngine _instance;
public static CoreEngine Instance
{
get { return _instance ?? (_instance = new CoreEngine()); }
}
private CoreEngine(){}
The WebAPI method:
CoreEngine _coreEngine = CoreEngine.Instance;
[System.Web.Http.Route("Compare")]
public void PostCompare([FromUri]string pluginName, [FromUri]string file)
{
var plugin = _coreEngine.GetPlugin(pluginName);
if (plugin == null)
return;
plugin.Compare(file);
And a simple client:
public void Post(string uri, Dictionary<string,string> postDic)
{
using (var client = new HttpClient())
{
var postUri = FromDictionaryToUriString(uri, postDic);
var response = client.PostAsync(postUri, null).Result;
}
}
Its only one thread and sync.
I tried to repeate the same POST
method for two times and in the first time I had the first instance of the CoreEngine
while in the secound call, The CoreEngine
has been disposed and the instance were re-created.