locked
SecurityException in sandbox plugin RRS feed

  • Question

  • Hi Everyone,

    I am trying to generate a PDF document and add it to an email message as attachment in a CRM Online deployment. I am using PDFSharp and MigraDoc libraries to achieve that (I used ILMerge to merge the libraries with my plugin assembly). Unfortunately I get a securityexception, and I can't seem to figure out why. Below is my error stacktrace:

    Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=7.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
    Detail: <OrganizationServiceFault xmlns="http://schemas.microsoft.com/xrm/2011/Contracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
      <ErrorCode>-2147220970</ErrorCode>
      <ErrorDetails xmlns:a="http://schemas.datacontract.org/2004/07/System.Collections.Generic">
        <KeyValuePairOfstringanyType>
          <a:key>CallStack</a:key>
          <a:value i:type="b:string" xmlns:b="http://www.w3.org/2001/XMLSchema">   at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark&amp; stackMark, Boolean isPermSet)
       at System.Security.CodeAccessPermission.Demand()
       at System.Drawing.Graphics.FromHwnd(IntPtr hwnd)
       at PdfSharp.Drawing.XGraphics..ctor(Graphics gfx, XSize size, XGraphicsUnit pageUnit, XPageDirection pageDirection)
       at PdfSharp.Drawing.XGraphics.CreateMeasureContext(XSize size, XGraphicsUnit pageUnit, XPageDirection pageDirection)
       at MigraDoc.Rendering.DocumentRenderer.PrepareDocument()
       at MigraDoc.Rendering.PdfDocumentRenderer.PrepareDocumentRenderer(Boolean prepareCompletely)
       at MigraDoc.Rendering.PdfDocumentRenderer.PrepareRenderPages()
       at MigraDoc.Rendering.PdfDocumentRenderer.RenderDocument()
       at CreateQuotePDF.QuotePDFCreator.Create()
       at MyCRMOrg.Package.Plugins.PostEmailCreate.ExecutePostEmailCreate(LocalPluginContext localContext)
       at MyCRMOrg.Package.Plugins.Plugin.Execute(IServiceProvider serviceProvider)
       at PluginProfiler.Library.PluginAppDomainProxy.ExecuteCore(Stopwatch watch, ProfilerExecutionReport report, Object instance, Object executionParameter)
       at PluginProfiler.Library.AppDomainProxy.Execute(ProfilerExecutionConfiguration configuration, ProfilerExecutionReport report)</a:value>
        </KeyValuePairOfstringanyType>
      </ErrorDetails>
      <Message>Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.</Message>
      <Timestamp>2015-05-28T06:55:56.1952325Z</Timestamp>
      <InnerFault i:nil="true" />
      <TraceText i:nil="true" />
    </OrganizationServiceFault>

    It seems the problem stems from a System.Drawing.Graphics call, but I didn't find any security limitations on sandbox plugin which might cause this issue.

    Do you have any ideas on it? I am also interested if anyone had a similar PDF creation scenario, and how did they manage it.

    Regards,

    Adam

    Thursday, May 28, 2015 7:27 AM

All replies

  • The source of the error is a request for the System.Security.Permissions.SecurityPermission code access permission, which is made from the System.Drawing.Graphics.FromHwnd method. Unfortunately there's not much you can do on this - Crm Online won't grant the System.Security.Permissions.SecurityPermission code access permission, and it looks like the methods System.Drawing.Graphics require this permission.

    You could try other methods in System.Drawing.Graphics to see if they don't need this permission, but that's probably unlikely


    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Thursday, May 28, 2015 11:14 AM
    Moderator