简体   繁体   English

乐观并发返回值

[英]Optimistic concurrency returning a value

I have an optimistic concurrency method from which I need to return a value. 我有一个乐观的并发方法,我需要从中返回一个值。 I am getting an error indicating the return variable is not in scope. 我收到一条错误消息,指示返回变量不在范围内。

private static string GenerateCustomerId(string contextPath)
    {
        var retryMaxCount = 3;             // maximum number of attempts
        var cycles = 0;                    // current attempt
        Exception exception = null;        // inner exception storage
        while (cycles++ < retryMaxCount)   // cycle control
        {
            try
            {
                Content  sequenceContent = Content.Load(contextPath);

                int currentSequence;
                int.TryParse(sequenceContent["LastSequenceNo"].ToString(), out currentSequence);
                currentSequence++;

                string currentDate = DateTime.Now.ToString("ddMMyyyy");

                string customerID = string.Format("{0}{1}", currentDate, currentSequence);

                //Save back to content with new update
                sequenceContent["LastSequenceNo"] =  currentSequence.ToString();
                sequenceContent["LastCustomerID"] =  customerID;
                sequenceContent.Save();


            }
            catch (NodeIsOutOfDateException e)
            {
                exception = e; // storing the exception temporarily
            }

            return customerID; //"**Customer ID does not exist in current context**"
        }

        // rethrow if needed
        if (exception != null)
            throw new ApplicationException("Node is out of date after 3 attempts.", exception);

    }

How can I return the value of CustomerID? 如何返回CustomerID的值?

Just move the return statement into the try block - and then add an extra throw statement at the very end of the method; 只需将return语句移到try块中-然后在方法的最后添加一个额外的throw语句; if you ever reach the end of the method without an exception, that indicates something very strange going on. 如果您例外地到达方法的末尾,则表明发生了非常奇怪的事情。 Or you could just make the final throw unconditional, of course: 或者,您当然可以无条件地使最后throw

private static string GenerateCustomerId(string contextPath)
{
    var retryMaxCount = 3;             // maximum number of attempts
    Exception exception = null;        // inner exception storage
    for (int cycles = 0; cycles < retryMaxCount; cycles++)
    {
        try
        {
            ...
            // If we get to the end of the try block, we're fine
            return customerID;
        }
        catch (NodeIsOutOfDateException e)
        {
            exception = e; // storing the exception temporarily
        }
    }

    throw new ApplicationException(
       "Node is out of date after " + retryMaxCount + " attempts.", exception);
}

As an aside, I'd personally avoid ApplicationException - I'd either just rethrow the original exception, or create a dedicated RetryCountExceeded exception or something similar. RetryCountExceeded ,我个人避免使用ApplicationException要么只是抛出原始异常,要么创建一个专用的RetryCountExceeded异常或类似的东西。 ApplicationException was basically a mistake on Microsoft's part, IMO. 从本质上讲, ApplicationException是微软IMO的一个错误。

(Also note that I've converted your while loop into a for loop for simplicity. I would certainly find the for loop easier to read and understand, and I suspect most other developers would feel the same way. I'd consider making retryMaxCount a constant in your class rather than a local variable, too.) (还要注意,为了简单起见,我已经将while循环转换为for循环。我当然会发现for循环更易于阅读和理解,我怀疑大多数其他开发人员也会有同样的感觉。我会考虑将retryMaxCount a类中的常量而不是局部变量。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM