遇到 Web Server 中不能使用 XmlSerializer 的问题
刚刚解决了这个问题,查了N多的资料,终于被我解决了。
问题是如下的一段测试代码:
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
在本地直接调用的时候一切正常,但是放到WebServer下面就出错:
Exception: System.InvalidOperationException
Message: Unable to generate a temporary class (result=1).
error CS2001: Source file 'C:\WINNT\TEMP\awwihhwh.0.cs' could not be found
error CS2008: No inputs specified
Source: System.Xml
at System.Xml.Serialization.Compiler.Compile(Assembly parent, String ns, CompilerParameters parameters, Evidence evidence)
at System.Xml.Serialization.TempAssembly.GenerateAssembly(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace, Evidence evidence, CompilerParameters parameters, Assembly assembly, Hashtable assemblies)
at System.Xml.Serialization.TempAssembly..ctor(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace, String location, Evidence evidence)
at System.Xml.Serialization.XmlSerializer.GenerateTempAssembly(XmlMapping xmlMapping, Type type, String defaultNamespace)
at System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace)
at System.Xml.Serialization.XmlSerializer..ctor(Type type)
开始就意识到肯定是调用者 ttt() 的权限不够,于是开始找如何把权限提高。首先用这个方法来强制提高权限,结果不幸:
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
然后开始考虑使用自定义的策略文件,找到了Microsoft Windows SharePoint Services 与代码访问安全,仿照它做了一个策略文件,在我的Web.config中加了如下的一段:
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
然后把系统的 C:\WINNT\Microsoft.NET\Framework\v1.1.4322\CONFIG\web_lowtrust.config 复制过来改名aaa.config,并在其中添加了一下内容,部分如下:
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/dot.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/dot.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/dot.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/dot.gif)
![](/Images/OutliningIndicators/None.gif)
结果还是不行,错误的原因是无法给“我的.dll”设置权限(看来以后还要多多研究这个内容)。
无奈之中,再到google搜索,结果发现确实有一个bug报告http://support.microsoft.com/default.aspx?scid=kb;en-us;322886,可惜是.net 1.0 的,我用的是1.1。但是在这片文章http://support.microsoft.com/default.aspx?scid=317012中却明确要求ASP.net 正常运行需要有%installroot%\ASP.NET Temporary Files 和 %windir%\temp 两个目录的权限。
最后,检查两个目录的权限,发现前者有权限,而后者没有。于是设置 c:\winnt\temp 让 asp.net 有 Full Access权限。问题解决!但是这个是不是M$的安装程序的问题啊
??
整个过程历时从前天下午到现在。痛苦
另外,发现很多人都遇到相同的问题,有仁兄建议说重装.net和vs,幸好没听他的,否则不累死也要后悔死。
![老翅寒暑](https://images.cnblogs.com/cnblogs_com/BigTall/46728/o_qrcode-bigtall.png)
公众号:老翅寒暑