[C#] .NET Core/Standard 1.X 項目中如何使用XmlIgnoreAttribute等標準範圍外的內容,兼談如何解決“violation of security transparency rules failed”(違反安全透明規則失敗)異常

来源:http://www.cnblogs.com/zyl910/archive/2017/10/04/dotnet_standard_use_xmlignoreattribute.html
-Advertisement-
Play Games

作者: "zyl910" 一、緣由 XML序列化是一個很常用的功能,但對於.NET Core/Standard,其直到2.0版才內置支持XML序列化。具體來說, .NET Core 2.0 或 .NET Standard 2.0 才有 "XmlIgnoreAttribute" 類,而1.X版(.NE ...


作者: zyl910

一、緣由

XML序列化是一個很常用的功能,但對於.NET Core/Standard,其直到2.0版才內置支持XML序列化。具體來說, .NET Core 2.0 或 .NET Standard 2.0 才有 XmlIgnoreAttribute類,而1.X版(.NET Core 1.0~1.1 或 .NET Standard 1.0~1.6)版沒有。
這一點可以在官網的API參考頁面(.NET API Browser)驗證,若以 .NET Core/Standard 1.X版查看 XmlIgnoreAttribute 類,會被回退到 .NET Framework 4.7。這就表示XML序列化功能是在.NET Core/Standard 1.X版標準範圍外的。

可是XML序列化是很常用的功能,特別是XmlIgnoreAttribute很常用。有什麼辦法可以使 .NET Core/Standard 1.X 項目中 能使用它呢?

二、在 .NET Core 項目中使用NuGet的包,工作正常

查了一下,發現NuGet上有一個叫 System.Xml.XmlSerializer 的包。
把該包加入 .NET Core 項目,果然能正常使用XML序列化功能了。

三、在 .NET Standard 項目中使用NuGet的包,遇到“violation of security transparency rules failed”異常

3.1 問題

既然.NET Core項目能工作,那麼應該也能用到.NET Standard項目中。
於是我嘗試了一下,在.NET Standard類庫項目中增加此NuGet包。編譯成功了,隨後在使用時發現問題——

  • 在.NET Framework項目引用它(.NET Standard類庫項目)時,能正常工作。
  • 但在.NET Core項目引用它時,運行時卻報告“violation of security transparency rules failed”(違反安全透明規則失敗)異常。
Unhandled Exception: System.InvalidOperationException: There was an error reflecting type 'ZylLib.UnionTypes.UnionShort'. ---> System.MethodAccessException: Attempt to access method System.Xml.Serialization.XmlIgnoreAttribute..ctor() in violation of security transparency rules failed.
  at System.RuntimeMethodHandle.CheckLinktimeDemands(IRuntimeMethodInfo method, RuntimeModule module, Boolean isDecoratedTargetSecurityTransparent)
  at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes, Boolean isDecoratedTargetSecurityTransparent)
  at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeFieldInfo field, RuntimeType caType)
  at System.Attribute.GetCustomAttributes(MemberInfo element, Boolean inherit)
  at System.Xml.Serialization.XmlAttributes..ctor(MemberInfo memberInfo)
  at System.Xml.Serialization.XmlReflectionImporter.GetAttributes(MemberInfo memberInfo)
  at System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping mapping, StructModel model, Boolean openModel, String typeName, RecursionLimiter limiter)
  at System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel model, String ns, Boolean openModel, XmlAttributes a, RecursionLimiter limiter)
  at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter)
  --- End of inner exception stack trace ---
  at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter)
  at System.Xml.Serialization.XmlReflectionImporter.ImportElement(TypeModel model, XmlRootAttribute root, String defaultNamespace, RecursionLimiter limiter)
  at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type type, XmlRootAttribute root, String defaultNamespace)
  at System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace)
  at ZylLib.UnionTypes.UnionTypesExample.TestShort(StringBuilder sb)
  at ZylLib.UnionTypes.UnionTypesExample.Output(StringBuilder sb)
  at ZylLib.UnionTypes.ConsoleExample.Program.Main(String[] args)

3.2 中途分析

調試時查了一下程式集的位置,發現所引用的是這個dll——

C:\Users\<用戶名>\.nuget\packages\System.Xml.XmlSerializer\4.0.10\lib\DNXCore50\System.Xml.XmlSerializer.dll

看了一下,該程式集沒有 AllowPartiallyTrustedCallers、SecurityTransparent 特性。導致所有代碼預設是 SecurityCritical(安全關鍵)的,透明方法無法調用。

但這一點太奇怪了,.NET Core項目也用的是這個dll啊。.NET Core項目直接使用XML序列化是能正常工作的。表示.NET Core是 SecurityCritical(安全關鍵)的啊。
而且從異常來看,是Attribute.GetCustomAttributes這個底層方法報錯的。
難道是“不能用NuGet包,只能用標準範圍內”的嗎?

3.3 最終解決

整理一下目前的調用關係——

  • XmlIgnoreAttribute 位於 NuGet包System.Xml.XmlSerializer。雖然MSDN上說該類是安全透明的,但NuGet包中的該類是 SecurityCritical(安全關鍵) 的。
  • .NET Standard項目中有一個 UnionShort 結構,它其中的欄位用到了 XmlIgnoreAttribute。因本程式集配置了AllowPartiallyTrustedCallers,故該結構是安全透明的。
  • .NET Core 項目引用了 .NET Standard項目中的UnionShort結構,但根據該結構構造XmlSerializer時,底層的 CustomAttribute.GetCustomAttributes 拋“violation of security transparency rules failed”異常了。雖然.NET Core 項目是SecurityCritical的。

難道是安全透明類(或結構)裡面不能引用SecurityCritical(安全關鍵)的特性?
編譯時沒報錯啊,創建該類、調用方法也正常啊。現在只是在序列化時使用了該特性。

於是將 .NET Standard項目程式集的 AllowPartiallyTrustedCallers 特性去掉。發現能正常運行了。看來真的是“安全透明類(或結構)裡面不能引用SecurityCritical(安全關鍵)的特性”。

去掉“AllowPartiallyTrustedCallers”不利於安全透明規則,於是將它恢復了。隨後嘗試給該結構體加上 SecuritySafeCriticalAttribute 特性。發現能正常運行了。

四、心得

經過這件事後,有了以下心得——

  1. 對於 .NET Framework 已有但 .NET Core/Standard 沒有的功能。可嘗試搜索NuGet包,很多功能其實已經有官方包了。
  2. 安全透明類(或結構)裡面不能引用SecurityCritical(安全關鍵)的特性。需給類(或結構)加上 SecuritySafeCriticalAttribute 特性。
  3. 使用NuGet包時容易遇到SecurityCritical(安全關鍵)的類,即使是官方的包也有時會有這種情況,故需註意加上 SecuritySafeCriticalAttribute 特性。

同理,遇到缺少 DataContractAttribute 特性時,可引用 NuGet上的 System.Runtime.Serialization.Primitives 包。測試通過。

參考文獻


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 一、基本的編譯與安裝 1、安裝依賴項 2、下載新版本,到官網複製下載鏈接 3、解壓 4、編譯安裝 編譯選項說明: --prefix=path 如果在編譯的不指定安裝位置,那麼預設的位置/usr/local/nginx目錄--sbin-path=path 設置nginx執行腳本的位置,這裡如果設置在p ...
  • scp 是secure copy的簡寫,用於在Linux下進行遠程拷貝文件的命令,和它類似的命令有cp,不過cp只是在本機進行拷貝不能跨伺服器,而且 scp傳輸是加密的。可能會稍微影響一下速度。當你伺服器硬碟變為只讀 read only system時,用scp可以幫你把文件移出來。另 外,scp還 ...
  • 第1章 軟體查詢 1.1 查詢軟體是否安裝 rpm -qa |grep cron 查詢是否安裝了這個軟體. [root@znix ~]# rpm -qa |grep cron crontabs-1.10-33.el6.noarch cronie-1.4.4-16.el6_8.2.x86_64 cro ...
  • C#操作字元串方法總結 staticvoid Main(string[] args){ string s =""; //(1)字元訪問(下標訪問s[i]) s ="ABCD"; Console.WriteLine(s[0]); // 輸出"A"; Console.WriteLine(s.Length ...
  • ajax請求出現500錯誤——但是想實現的功能是,把一個頁面分成了兩份,點擊右邊導航欄,利用ajax請求,請求數據,在右邊出現相應頁面,當時使用的是partialAction然後出現了這個500錯誤,主要就是在這個action上,一個頁面不能ajax請求另一個頁面。當時對實體集也不是很瞭解,對於自動 ...
  • 本文內容整理自http://blog.csdn.net/tennysonsky/article/details/45062079 C/S架構和B/S架構是兩種頗具影響力的軟體體繫結構。C/S是一種歷史悠久且技術非常成熟的架構;B/S是新生代架構,從C/S派生出來,有很多創新,在web信息時代虎虎生威 ...
  • 摘要: 在這篇文章中,我將在一個例子中實際地展示MVC。 場景 假設一個朋友決定舉辦一個新年晚會,她邀請我創建一個用來邀請朋友參加晚會的WEB程式。她提出了四個註意的需求: 一個首頁展示這個晚會 一個表單用來提交申請 驗證表單內容,成功後顯示謝謝頁面 完成後給主人發送申請郵件 添加Model類Gue ...
  • 本篇文章帶你一步一步創建一個簡單的ASP.NET MVC程式。 ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...