xmlserializer_更改XmlSerializer输出临时程序集的位置

xmlserializer

xmlserializer

With this tip, I couldn't find any documentation, so you're on your own. That means no help/support from me or anywhere. YMMV - Your Mileage May Vary. No warranties. Enjoy.

有了这个技巧,我找不到任何文档,所以您自己一个人。 这意味着我或任何地方都不会提供帮助/支持。 YMMV-您的里程可能会有所不同。 没有保证。 请享用。

Someone asked:

有人问:

"When using the XmlSerializer from ASP.NET there are permissions issues can can be solved by granting the user account read/write permissions on the %SYSTEMROOT%\Temp folder but I would much rather have the temporary files created in a different folder"

“从ASP.NET使用XmlSerializer时,可以通过向用户帐户授予%SYSTEMROOT%\ Temp文件夹的读/写权限来解决权限问题,但我宁愿在其他文件夹中创建临时文件”

So I poked around. If you want the XmlSerializer to output it's assemblies elsewhere, here's how. Note that I used the previous tip on How To Debug into a .NET XmlSeraizlizer Generated Assembly just to prove this was working. It's not needed for this tip!

所以我四处张望。 如果您希望XmlSerializer将其程序集输出到其他位置,请按以下步骤操作。 请注意,我使用了有关如何调试到.NET XmlSeraizlizer生成的程序集的上一个技巧,只是为了证明它是有效的。 本技巧不需要!

To start, let's see where the temporary files end up usually.

首先,让我们看看临时文件通常在哪里结束。

First, I'll add this to my web.config (or whatever.exe.config if you like). This is just to visualize and confirm. It's not needed for this tip.

首先,我将其添加到我的web.config(或者您愿意的任何exe.exe.config)中。 这只是为了可视化和确认。 本技巧不是必需的。

<configuration>
   <system.diagnostics>
      <switches>
         <add name="XmlSerialization.Compilation" value="1" />
      </switches>
   </system.diagnostics>
</configuration>

I'll debug my application, but I'll use SysInternal's Process Monitor and set the filter to only show processes whose name contains the string "WebDev" and see what files the VS WebDev Server writes to. If you're using IIS, you would search for W3WP or ASPNET_WP.

我将调试应用程序,但将使用SysInternal的Process Monitor ,并将过滤器设置为仅显示名称包含字符串“ WebDev”的进程,并查看VS WebDev Server写入哪些文件。 如果使用的是IIS,则将搜索W3WP或ASPNET_WP。

Here I can see it writing to C:\Users\Scott\AppData\Local\Temp. The file names are auto-generated...note their names, in my case 6txrbdy.0.*. To prove these are the real generated XmlSerializers, I'll put a breakpoint on my app just before I call CreateSerializer and then drag the .cs file over from the TEMP folder into VS.NET. Note that the .PDB will get loaded when I hit F11 to step into.

在这里,我可以看到它写入C:\ Users \ Scott \ AppData \ Local \ Temp。 文件名是自动生成的...在我的情况下,请注意文件名6txrbdy.0。* 。 为了证明这些是真正生成的XmlSerializer,我将在调用CreateSerializer之前在应用程序上放置一个断点,然后将.cs文件从TEMP文件夹拖到VS.NET中。 请注意,当我按F11进入时,.PDB将被加载。

Make note of the Call Stack. See the name of the assembly and the name of the .cs file? So, we're sure now where this XmlSerializer came from; it came from C:\Users\Scott\AppData\Local\Temp. If we were running IIS, it'd have been in %SYSTEMROOT%\Temp. The point being, it's automatic and it's temporary and your process needs WRITE access to that folder.

记下调用堆栈。 看到程序集的名称和.cs文件的名称吗? 因此,我们现在确定XmlSerializer的来源; 它来自C:\ Users \ Scott \ AppData \ Local \ Temp。 如果我们正在运行IIS,它将位于%SYSTEMROOT%\ Temp中。 关键是,它是自动的,是临时的,您的过程需要对该文件夹进行WRITE访问。

Now, poking around in Reflector with the not-used-often-enough Analyze method shows that XmlSerializerFactory.CreateSerializer eventually ends up in XmlSerializerCompilerParameters.Create which looks for a Configuration Section called “xmlSerializer” and a key called TempFilesLocation.

现在,使用不常用的Analyze方法在Reflector中四处浏览,显示XmlSerializerFactory.CreateSerializer最终以XmlSerializerCompilerParameters.Create结尾,并在其中查找名为“ xmlSerializer”的配置节和名为TempFilesLocation的键。

That actually means it's looking for a section called System.Xml.Serializer and an element called xmlSerializer and an attribute called tempFilesLocation.

这实际上意味着它正在寻找一个名为System.Xml.Serializer的部分和一个名为xmlSerializer的元素以及一个名为tempFilesLocation的属性。

I'll add this to my web.config now:

我现在将其添加到我的web.config中:

<system.xml.serialization> 
  <xmlSerializer tempFilesLocation="c:\\foo"/> 
</system.xml.serialization> 

...and create a c:\\foo directory. Make sure your hosting process has write access to the directory.

...并创建ac:\\ foo目录。 确保您的托管进程对目录具有写权限。

I'll run my app again, and check out that new folder.

我将再次运行我的应用,然后签出该新文件夹。

I've got newly generated XmlSerializer temporary assemblies in there. Undocumented, yes, but it does the job. Note, stuff like this can totally go away at any minute, so don't base your whole design on things like this. It's your tush, not mine, on the line.

我在那里有新生成的XmlSerializer临时程序集。 无证件,是的,但是确实可以。 注意,这样的事情在任何时候都可能完全消失,所以不要将整个设计都基于这样的事情。 这是您的要求,而不是我的要求。

翻译自: https://www.hanselman.com/blog/changing-where-xmlserializer-outputs-temporary-assemblies

xmlserializer

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值