.NET Standard是否在其支持的每个平台上标准化HResult值? [英] Does .NET Standard normalize HResult values across every platform it supports?

查看:103
本文介绍了.NET Standard是否在其支持的每个平台上标准化HResult值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个创建随机文件的简单函数。为了确保线程安全,它将在重试循环中创建该文件,如果该文件存在,它将再次尝试。

I am creating a simple function that creates a random file. To be thread safe, it creates the file in a retry loop and if the file exists it tries again.

while (true)
{
    fileName = NewTempFileName(prefix, suffix, directory);

    if (File.Exists(fileName))
    {
        continue;
    }

    try
    {
        // Create the file, and close it immediately
        using (var stream = new FileStream(fileName, FileMode.CreateNew, FileAccess.Write, FileShare.Read))
        {
            break;
        }
    }
    catch (IOException e)
    {
        // If the error was because the file exists, try again
        if ((e.HResult & 0xFFFF) == 0x00000050)
        {
            continue;
        }

        // else rethrow it
        throw;
    }
}

根据MSDN,HResult值源自COM这似乎表明它仅适用于Windows,并且专门将它们列出为 Win32代码 。但这位于针对.NET Standard的库中,理想情况下,它应在每个平台.NET Standard都支持

According to MSDN, the HResult value is derived from COM which would seem to indicate it will only work on Windows, and it specifically lists them as "Win32 codes". But this is in a library which targets .NET Standard and ideally it should work on every platform .NET Standard supports.

我想知道的是,我是否可以依靠上面使用HResult值进行交叉处理的方法?平台? 文档不是

What I am wondering is whether I can rely on the above approach that uses the value from HResult to be cross-platform? The documentation is not clear on this point.

否则,如何确定在其他平台上期望的HResult值?

If not, how do I determine what HResult values to expect on other platforms?


注意::还有一个类似的问题 .NET是否定义了常见的HRESULT值?,但是在.NET Standard(以及对.NET的跨平台支持)存在之前就被问到了,所以我不能依靠这个答案。

NOTE: There is a similar question Does .NET define common HRESULT values?, but it was asked before .NET Standard (and cross-platform support for .NET) existed, so I cannot rely on that answer for this purpose.

目前,我们的代码库仅使用:

For now, our codebase only uses:


  1. 0x00000020 -ERROR_SHARING_VIOLATION

  2. 0x00000021-ERROR_LOCK_VIOLATION

  3. 0x00000050-ERROR_FILE_EXISTS

我们的目标是.NET Standard 1.5。

We are targeting .NET Standard 1.5.


注意:接受的答案确实满足了我的要求,我有一个后续问题如何使捕获通用IOException可靠地跨平台移植?

NOTE: While the accepted answer does satisfy what I asked here, I have a follow-up question How do I make catching generic IOExceptions reliably portable across platforms?


推荐答案

Exception.HResult值未跨平台标准化。

Exception.HResult values are not standardized across platforms.

对于I / O错误,.NET Core将返回特定于平台的错误代码作为HResult。您的示例中已经存在的文件HResult属性的值在Linux上将为17,对于其他Unix系统,该值可能会有所不同。

For I/O errors, .NET Core will return platform specific error code as HResult. The value of HResult property for file already exist in your example is going be 17 on Linux, and it may be different value for other Unix systems.

映射的相关代码Unix上的IO错误是在这里: https://github.com/dotnet/corefx/blob/master/src/Common/src/Interop/Unix/Interop.IOErrors.cs

The relevant code that maps IO errors to exceptions on Unix is here: https://github.com/dotnet/corefx/blob/master/src/Common/src/Interop/Unix/Interop.IOErrors.cs

这篇关于.NET Standard是否在其支持的每个平台上标准化HResult值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆