I'm wondering if I can access a static variable in a WCF service for status updates and how to do this if you can?
I have a long process that updates 1000s of products. This process is fired off from an asp.net page and the user wants status updates from the service as the process runs, i.e. 'processing product 123'. I have the service and I have a timer control inside an update panel that I want to use to poll the service for status updates. I was thinking as the timer control posts back, I'd query the service using a GetStatus method to get the current status but I think the status message would always be the same because a new instance of the service would be created on Page_Load. So, how can I get a running status variable from a WCF service to an asp.net page?
protected void Timer1_Tick(object sender, EventArgs e)
{
if (statusMessage == null)
{
// feed operation finished or timer is running invalid
Timer1.Enabled = false;
}
try
{
UpdateStatusDisplay();
}
catch (Exception exp)
{
....
}
}
In my WCF service
public string Status = "";
public void BeginImport(ImportOptions options)
{
_options = options;
string conString = ConfigurationManager.ConnectionStrings["String1"].ConnectionString;
using (var con = new SqlConnection(conString))
{
con.Open();
var products = con.Query("SELECT * FROM PRODUCTS");
foreach (var product in products)
{
Status = "Processing product " + product.ProductName);
}
}
}
public string GetStatus()
{
return Status;
}
EDIT: I just read about [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)], is this all I'd need to do?