每个呼叫存储在.asmx Web服务中

时间:2013-06-19 16:03:10

标签: session asmx

我们有一个现有的.asmx Web服务,它使用静态集合来存储表单处理过程中遇到的错误。不幸的是,正如您可能已经猜到的那样,这不是线程安全的。因此,我们正在寻找一种线程安全的方法来做到这一点。我宁愿避免每次调用都来回传递一个错误对象。我目前正在重写它以使用HttpContext.Current.Items集合(Asp.Net兼容性已启用,Sessions也是如此),但是想知道是否有人能想出更好的方法来实现它?在我不知道的.asmx Web服务中是否存在某种类似会话状态的存储?

这是修复程序的当前实现。有没有更好的方法可以做到这一点?

public class ContextErrorCollection
{
    public static void AddToErrorCollection(string error)
    {
        var nsec = ErrorCollection;
        nsec.Add(error);
        ErrorCollection = nsec;
    }

    public static List<string> ErrorCollection
    {
        get
        {
            var nsec = HttpContext.Current.Items["ErrorCollection"] as NonStaticErrorCollection ?? new NonStaticErrorCollection();
            return nsec.ItemList;
        }

        set
        {
            var nsec = new NonStaticErrorCollection();
            foreach (string error in value)
            {
                nsec.ItemList.Add(error);
            }
            HttpContext.Current.Items["ErrorCollection"] = nsec;
        }

    }
}

public class NonStaticErrorCollection
{
    public string[] list()
    {
        return m_sList.ToArray();
    }

    public List<string> ItemList
    {
        get
        {
            if (m_sList == null) return new List<string>();

            return m_sList;
        }
    }

    private readonly List<string> m_sList = new List<string>();
}

//Calling Code
ContextErrorCollection.AddToErrorCollection(string.Format("Adding error number {0} to list...", i));

我知道依赖注入类型的解决方案更可取,但1)实际上没有足够的时间来实现它,2)这个应用程序将在大约6个月内重写。

提前致谢!

-Nathan

0 个答案:

没有答案