如何解决Dynamics CRM插件System.Security.Permissions.FileIOPermission错误

时间:2016-09-06 10:59:28

标签: c# plugins dynamics-crm crm dynamics-crm-2013

业务流程错误 System.Security.SecurityException:请求类型' System.Security.Permissions.FileIOPermission ,mscorlib,Version = 4.0.0.0,Culture = neutral,公钥= b77a5c561934e089'失败。 在System.Security.CodeAccessSecurityEngine.Check(对象需求,StackCrawlMark& stackMark,Boolean isPermSet) 在System.Security.CodeAccessPermission.Demand() at System.IO.FileStream.Init(String path,FileMode mode,FileAccess access,Int32 rights,Boolean useRights,FileShare share,Int32 bufferSize,FileOptions options,SECURITY_ATTRIBUTES secAttrs,String msgPath,Boolean bFromProxy,Boolean useLongPath,Boolean checkHost) at System.IO.FileStream..ctor(String path,FileMode mode,FileAccess access,FileShare share,Int32 bufferSize,FileOptions options,String msgPath,Boolean bFromProxy,Boolean useLongPath,Boolean checkHost) at System.IO.File.InternalWriteAllBytes(String path,Byte [] bytes,Boolean checkHost) 在RetrieveAttachments.RetrieveClass.Execute(IServiceProvider serviceProvider) 失败的行动是: 需求 失败的第一个权限的类型是: System.Security.Permissions.FileIOPermission 失败的程序集区域是: 我的电脑

我还在插件AssemblyInfo.cs文件中添加了以下方法   [assembly:System.Security.AllowPartiallyTrustedCallers] 但它提出同样的错误。

QueryExpression notes = new QueryExpression { EntityName = "annotation", ColumnSet = new ColumnSet("filename", "subject", "annotationid", "documentbody","mimetype") };
notes.Criteria.AddCondition("annotationid", ConditionOperator.Equal, annotationid);
     EntityCollection NotesRetrieve = service.RetrieveMultiple(notes);
     if (NotesRetrieve != null && NotesRetrieve.Entities.Count > 0)
     {
      foreach (var note in NotesRetrieve.Entities)
      {
       string fileName = note.GetAttributeValue<string>("filename");
       string cleanFileName = string.Empty;
       foreach (var chr in fileName.ToCharArray().ToList())
       {
        if(!Path.GetInvalidFileNameChars().Contains(chr)) cleanFileName = cleanFileName + chr; 
       }
     FileIOPermission f = new FileIOPermission(FileIOPermissionAccess.Write, @"D:\note");
     string fileLocation = Path.Combine(@"D:\note", cleanFileName);
     byte[] fileContent = Convert.FromBase64String(NotesRetrieve.Entities[0].Attributes["documentbody"].ToString());
     System.IO.File.WriteAllBytes(fileLocation, fileContent);
    }
   }

2 个答案:

答案 0 :(得分:3)

禁止从沙盒插件中读取文件。尝试将隔离模式更改为无。

答案 1 :(得分:0)

如果您的插件已在沙盒中注册,那么这可能就是问题所在。尝试在沙箱之外注册它。 Here您可以找到有关信任级别的信息