G
Giulio Petrucci
Hi there,
I'm facing a weird problem in these days. First of all, let me describe
my scenario. I have a client application asking for some services to be
performed by a remote service, using a web service interface. Performing
such operations might take a long time, so the client application does
some "polling", asiking the server for the remote service status and
"consuming" partial results. Taking a look to the code-behin of my web
service, I have a ProcessAgent.cs class, which implements the process
itself. All the process agent instances are grouped within a
ProcessAgentPool which is a *singleton*. I made it as a singleton in
order to have a *unique* instance (to avoid the web service being
stateless ;-) ). The ProcessAgent class looks like (only declarations):
public class ProcessAgent
{
internal ProcessAgent(); //.ctor();
public string ProcessID { get; } //returns the process ID;
public void StartProcess(); //starts the process;
public int Status { get; } //process status, it can be Unstarted,
InProgress, Complete;
public string ConsumeResults(); //consumes the partial results;
}
The ProcessAgentPool looks like (only declarations. It's nothing but an
*ordinary* Singleton):
public class ProcessAgentPool
{
private static ProcessAgentPool _me = new ProcessAgentPool();
//*unique* instance!!!
private ProcessAgentPool(); //private .ctor();
public static ProcessAgentPool GetInstance(); //this method should
return the *unique*
//ProcessAgentPool instance;
public GetProcessAgent(string processID); //returns the process with
the given ID;
public ProcessAgent CreateProcess(); //returns a new ProcessAgent instance;
}
Pretty simple, isn't it?
Finally, the web service interface looks like:
public class Service : System.Web.Services.WebService
{
internal ProcessAgentPool pool;
public Service()
{
pool = ProcessAgentPool
}
[WebMethod]
public string StartProcess()
{
ProcessAgent p = pool.CreateProcess():
return p.ProcessID;
}
[WebMethod]
public int GetStatus(string processID)
{
ProcessAgent p = pool.GetProcessAgent(processID);
if (p!=null)
{
return p.Status;
}
else {
return -1;
}
}
[WebMethod]
public string GetResults(string processID)
{
ProcessAgent p = pool.GetProcessAgent(processID);
if (p!=null)
{
return p.ConsumeResults();
}
else
{
return "";
}
}
}
As I wrote before, I make all this stuff up in order to have a *unique*
pool instance to keep all the processes going on. I created a small
application to emulate the scenario and it works fine as the HashCode of
the "pool" object is always the same! "Cool!", I said. But I spoke too
early: I tried to implement it in the alfa version of my real
application and something went wrong (Murphy rules!!!). The weird thing
is that *different instances* of my singleton ProcessAgentPool class are
actually instantiated!
So, let's summarize. I have a client application asking for some
long-time-taking operations to be performed by a remote machine, using a
web service (--> stateless). Those processes are "wrapped" in a
ProcessAgent class, all the ProcessAgent class instances are grouped
together in a ProcessAgentPool instance. I need this pool to be *unique*
but it doesn't happens and I can't get *why*, as it works fine in a
sample application I used to study the problem.
Any suggestion? Any alternative way to solve my problem?
Thanks in advance,
Giulio
I'm facing a weird problem in these days. First of all, let me describe
my scenario. I have a client application asking for some services to be
performed by a remote service, using a web service interface. Performing
such operations might take a long time, so the client application does
some "polling", asiking the server for the remote service status and
"consuming" partial results. Taking a look to the code-behin of my web
service, I have a ProcessAgent.cs class, which implements the process
itself. All the process agent instances are grouped within a
ProcessAgentPool which is a *singleton*. I made it as a singleton in
order to have a *unique* instance (to avoid the web service being
stateless ;-) ). The ProcessAgent class looks like (only declarations):
public class ProcessAgent
{
internal ProcessAgent(); //.ctor();
public string ProcessID { get; } //returns the process ID;
public void StartProcess(); //starts the process;
public int Status { get; } //process status, it can be Unstarted,
InProgress, Complete;
public string ConsumeResults(); //consumes the partial results;
}
The ProcessAgentPool looks like (only declarations. It's nothing but an
*ordinary* Singleton):
public class ProcessAgentPool
{
private static ProcessAgentPool _me = new ProcessAgentPool();
//*unique* instance!!!
private ProcessAgentPool(); //private .ctor();
public static ProcessAgentPool GetInstance(); //this method should
return the *unique*
//ProcessAgentPool instance;
public GetProcessAgent(string processID); //returns the process with
the given ID;
public ProcessAgent CreateProcess(); //returns a new ProcessAgent instance;
}
Pretty simple, isn't it?
Finally, the web service interface looks like:
public class Service : System.Web.Services.WebService
{
internal ProcessAgentPool pool;
public Service()
{
pool = ProcessAgentPool
}
[WebMethod]
public string StartProcess()
{
ProcessAgent p = pool.CreateProcess():
return p.ProcessID;
}
[WebMethod]
public int GetStatus(string processID)
{
ProcessAgent p = pool.GetProcessAgent(processID);
if (p!=null)
{
return p.Status;
}
else {
return -1;
}
}
[WebMethod]
public string GetResults(string processID)
{
ProcessAgent p = pool.GetProcessAgent(processID);
if (p!=null)
{
return p.ConsumeResults();
}
else
{
return "";
}
}
}
As I wrote before, I make all this stuff up in order to have a *unique*
pool instance to keep all the processes going on. I created a small
application to emulate the scenario and it works fine as the HashCode of
the "pool" object is always the same! "Cool!", I said. But I spoke too
early: I tried to implement it in the alfa version of my real
application and something went wrong (Murphy rules!!!). The weird thing
is that *different instances* of my singleton ProcessAgentPool class are
actually instantiated!
So, let's summarize. I have a client application asking for some
long-time-taking operations to be performed by a remote machine, using a
web service (--> stateless). Those processes are "wrapped" in a
ProcessAgent class, all the ProcessAgent class instances are grouped
together in a ProcessAgentPool instance. I need this pool to be *unique*
but it doesn't happens and I can't get *why*, as it works fine in a
sample application I used to study the problem.
Any suggestion? Any alternative way to solve my problem?
Thanks in advance,
Giulio