开发者

WebService Member Variables and Thread Safety

I fear this is obvious, but would someone mind confirming that the private fields of WebServices are thread safe? Specifically,

1) In the code below, each thread will get a new instance of the ReportService class

protected void Page_Load(object sender, EventArgs e)
{
    // Client side scripting
    ScriptManager scriptManager = ScriptManager.GetCurrent(this);
    scriptManager.Services.Add(new ServiceReference("~/WebServices/ReportService.asmx"));
}

2) Because of (1), it is safe for each WebMethod in "ReportService" to manipulate and reference private fields as shown below:

[System.Web.Script.Services.ScriptService]
public class ReportService : : System.Web.Services.WebService
{
  private string _protocol = null;
  private string _reportType = null;

  [WebMethod]
  public string GetReport(string protocol, string reportType, string reportId)
  {
    _protocol = protocol;
    _reportType = reportType;
    return GetCustomReport(reportId);
  }
}

The methods above are dumbed down for the purposes of illustration. There is an example on MSDN using private fields, but the privat开发者_StackOverflow社区e field _xmlStr is not manipulated, leaving the question of thread safety ambiguous.

http://msdn.microsoft.com/en-us/library/bb398995.aspx

Thanks,

--Brett


Yes this is thread safe but I would advice you to avoid using private fields in a web service. Just add the protocol and reportType parameters to the GetCustomReport method. This way you no longer need these fields.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜