xmlserializer_蛮力检查以确保可以访问TEMP目录,以便使用XmlSerializer

xmlserializer

xmlserializer

If you want to use the XmlSerilializer, you'll (ASPNET user) need write access to the Windows/Temp folder. Otherwise you may see this as the temporary assembly fails to be saved:

如果要使用XmlSerilializer,则(ASPNET用户)需要对Windows / Temp文件夹的写权限。 否则,由于临时程序集无法保存,您可能会看到以下信息:

File or assembly name zmp0husw.dll, or one of its dependencies, was
not found.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.
Exception Details: System.IO.FileNotFoundException: File or assembly
name zmp0husw.dll, or one of its dependencies, was not found.

文件或程序集名称zmp0husw.dll或其依赖项之一为未找到。 说明:在执行期间发生未处理的异常当前的Web请求。 请查看堆栈跟踪以获取更多信息有关错误及其在代码中起源的信息。 异常详细信息:System.IO.FileNotFoundException:文件或程序集找不到名称zmp0husw.dll或其依赖项之一。

Internally, there are Security Demands, to see if you have the "right" from a Code Access Security point of view, but noone actually CHECKS to see if you have the ACL rights.

在内部,存在安全需求,从代码访问安全的角度来看您是否拥有“权利”,但实际上没有人检查您是否具有ACL权利。

So, here's a brute force way to find out, once per AppDomain, to check if you have access. I reflectored into XmlSerializer to find out what they were doing to find our what path to write to. They were using GetTempPath from kernel32.dll, so we could PInvoke as well. (Update: However, Kevin Dente points out that Path.GetTempPath() will do the PInvoke for you. I was mirroring XmlSerializer's code, but as long as we do the same thing in essense, we're OK. Edits below, line numbers changed. Thanks Kevin!)

因此,这是一种暴力手段,每个AppDomain一次查找是否有访问权限。 我反思了XmlSerializer,以了解他们在做什么,以找到我们要写入的路径。 他们使用的来自kernel32.dll的GetTempPath ,因此我们也可以PInvoke。 (更新:但是,凯文·丹特( Kevin Dente)指出Path.GetTempPath()将为您执行PInvoke。我正在镜像XmlSerializer的代码,但是只要我们在essense中做同样的事情,就可以了。改变了,谢谢凯文!

    1 public sealed class SomeUtilityThingieClass
2 {
3 const string ErrorMessage = "We neede write access to: {0}";
6
7 private static bool tempFileAccess = false;
8 private static object tempFileAccessLock = new object();
9
10 public static bool EnsureTempFileAccess()
11 {
12 if (tempFileAccess == true)
13 {
14 return true;
15 }
16
17 if(tempFileAccess == false)
18 {
19 lock(tempFileAccessLock)
20 {
21 if(tempFileAccess == false)
22 {
29 string tempFile = Path.Combine(Path.GetTempPath(),"WriteTest.txt");
30 try
31 {
32 using(StreamWriter file = File.CreateText(tempFile))
33 {
34 file.Write("This is a test to see if we can write
to this TEMP folder and consequently make XmlSerializer
assemblies without trouble.");
35 }
36 }
37 catch(System.IO.IOException ex)
38 {
39 throw new System.IO.IOException(
string.Format(ErrorMessage,tempFullPath),ex);
40 }
41 catch(UnauthorizedAccessException ex)
42 {
43 throw new UnauthorizedAccessException(
string.Format(ErrorMessage,tempFullPath),ex);
44 }
45
46 if (File.Exists(tempFile))
47 {
48 File.Delete(tempFile);
49 }
50 tempFileAccess = true;
51 }
52 }
53 }
54 return tempFileAccess;
55 }
56 }

Once the write has worked once, we catch the success in a static and return that. If multiple threads get in here together, only one will make it past the lock. The others will wait. After we learn of success or failure from the first thread, the threads that were waiting for the lock will check the (now change) tempFileAccess boolean, and find it changed. The file write will happen only once per AppDomain. Rather than calling "throw;" or not catching the exceptions at all, I add a little extra polite message and wrap and throw. They won't know the line number that went wrong, but they WILL get a nice message.

一旦写入工作了一次,我们就以静态方式捕获成功并将其返回。 如果有多个线程一起进入,则只有一个线程会通过锁。 其他人将等待。 从第一个线程获悉成功或失败之后,正在等待锁定的线程将检查(现在已更改)tempFileAccess布尔值,并发现其已更改。 每个AppDomain仅一次写入文件。 而不是称“投掷”; 还是根本没有捕获到异常,我添加了一些额外的礼貌信息并包装并抛出。 他们不会知道出错的行号,但是他们会得到一个很好的消息。

The most interesting stuff to the beginner is the classic check/lock/doublecheck threadsafety. Note also that we have an explicit object tempFileAccessLock that exists ONLY for the purposes of locking.

对于初学者来说,最有趣的东西是经典的check / lock / doublecheck线程安全性。 还要注意,我们有一个明确的对象tempFileAccessLock,该对象仅出于锁定目的而存在。

翻译自: https://www.hanselman.com/blog/brute-force-check-to-ensure-access-to-the-temp-directory-in-order-to-use-the-xmlserializer

xmlserializer

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值