locked
Plugin is not getting fired in Sandbox for OnPremise environment. RRS feed

  • Question

  • Hello Guys,

               I have one plugin which is generating PDF file from an Email.This plugin using third party dll "Itextshartp.dll" for the functionality.We have on-premise instance of crm and we are registering the plugin on the same with Post Operation and Synchronous way.

              When I register and fires this plugin on "none" mode it works fine but it doesnt work for the "Sandbox" mode and generate the error "System.TypeInitializationException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #F680793B".

    here is the code whihc I have written in my plugin :

                                                                                                                    

    namespace EmialToPDF
    {
        public class EmailToPDFClass : IPlugin
        {
            static EmailToPDFClass()
            {
                //throw new ArgumentNullException("localContext");

                        //here is the error occures
    AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(OnResolveAssembly);


                //AppDomain.CurrentDomain.AssemblyResolve += OnResolveAssembly;
            }
            public void Execute(IServiceProvider serviceProvider)
            {

                ITracingService tracingService =
                    (ITracingService)serviceProvider.GetService(typeof(ITracingService));

                // Obtain the execution context from the service provider.
                IPluginExecutionContext context = (IPluginExecutionContext)
                    serviceProvider.GetService(typeof(IPluginExecutionContext));

                // The InputParameters collection contains all the data passed in the message request.
                if (context.InputParameters.Contains("Target") &&
                    context.InputParameters["Target"] is Entity)
                {
                    // Obtain the target entity from the input parameters.
                    Entity entity = (Entity)context.InputParameters["Target"];

                    // Verify that the target entity represents an account.
                    // If not, this plug-in was not registered correctly.
                    if (entity.LogicalName != "email")
                        return;


                    throw new InvalidPluginExecutionException("Hello");

                    try
                    {
                        // Obtain the organization service reference.
                        IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                        IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                        var emailEntity = ((Entity)context.InputParameters["Target"]).ToEntity<Email>();

                        MemoryStream memStream = new MemoryStream();
                        var doc = new Document();

                        var writer = PdfWriter.GetInstance(doc, memStream);
                        doc.Open();
                        PdfPTable table = new PdfPTable(2);

                        table.DefaultCell.Border = 0;


                        float[] widths = new float[] { 1f, 3f };
                        table.SetWidths(widths);

                        table.AddCell(GetPdfCell("From :"));
                        table.AddCell(GetPdfCell(emailEntity.Sender));

                        table.AddCell(GetPdfCell("To :"));
                        table.AddCell(GetPdfCell(emailEntity.ToRecipients));

                        table.AddCell(GetPdfCell("Cc :"));

                        foreach (ActivityParty item in emailEntity.Cc)
                        {
                            table.AddCell(GetPdfCell(item.AddressUsed));
                        }

                        table.AddCell(GetPdfCell("Bcc :"));
                        table.AddCell(GetPdfCell(" "));

                        table.AddCell(GetPdfCell("Subject :"));
                        table.AddCell(GetPdfCell(emailEntity.Subject));

                        var BlankCell = GetPdfCell(" ");
                        BlankCell.Colspan = 2;
                        table.AddCell(BlankCell);

                        Paragraph para = new Paragraph();
                        StringReader reader = new StringReader(emailEntity.Description);

                        table.AddCell(GetPdfCell(emailEntity.Description));

                        doc.Add(table);

                        StyleSheet styles = new StyleSheet();
                        styles.LoadTagStyle("th", "size", "9px");
                        styles.LoadTagStyle("th", "face", "Calibri");
                        styles.LoadTagStyle("span", "size", "9px");
                        styles.LoadTagStyle("span", "face", "Calibri");
                        styles.LoadTagStyle("td", "size", "9px");
                        styles.LoadTagStyle("td", "face", "Calibri");

                        using (var htmlWorker = new iTextSharp.text.html.simpleparser.HTMLWorker(doc))
                        {
                            //HTMLWorker doesn't read a string directly but instead needs a TextReader (which StringReader subclasses)
                            using (var sr = reader)
                            {
                                //Parse the HTML
                                htmlWorker.SetStyleSheet(styles);
                                htmlWorker.Parse(sr);
                            }
                        }

                        doc.Close();

                        //Attachment Code
                        var attachment = new ActivityMimeAttachment();
                        attachment.FileName = "Sample.pdf";
                        attachment.Body = Convert.ToBase64String(memStream.ToArray());
                        attachment.MimeType = "text/plain";
                        attachment.AttachmentNumber = 1;
                        attachment.ObjectTypeCode = "email";
                        var emailId = new Guid(emailEntity.ActivityId.ToString());
                        attachment.ObjectId = new EntityReference("email", emailId);

                        service.Create(attachment);

                    }
                    catch (Exception ex)
                    {
                        tracingService.Trace("FollowupPlugin: {0}", ex.ToString());
                        throw;
                    }
                }
            }

            private static Assembly OnResolveAssembly(object sender, ResolveEventArgs args)
            {

                Assembly executingAssembly = Assembly.GetExecutingAssembly();
                AssemblyName assemblyName = new AssemblyName(args.Name);
                String path = assemblyName.Name + ".dll";

                if (assemblyName.CultureInfo.Equals(CultureInfo.InvariantCulture) == false)
                {
                    path = String.Format(@"{0}\{1}", assemblyName.CultureInfo, path);
                }

                using (Stream stream = executingAssembly.GetManifestResourceStream(path))
                {
                    if (stream == null)
                        return null;
                    byte[] assemblyRawBytes = new byte[stream.Length];
                    stream.Read(assemblyRawBytes, 0, assemblyRawBytes.Length);
                    return Assembly.Load(assemblyRawBytes);
                }
            }


            private static PdfPCell GetPdfCell(String displayText)
            {
                var font = FontFactory.GetFont("Calibri", 9);
                var cell = new PdfPCell(new Phrase(displayText, font));
                cell.Border = 0;
                return cell;
            }

        }


    }

    The error occures on bold line.

    Any help would be appriciable.Please help ASAP

                


    Prashant Wani ,Software Engineer-II,Infinx Services Pvt.Ltd.

    Friday, November 21, 2014 12:07 PM

All replies

  • Hi Prashant,

    Check your trace logs for exact error and post it here.


    Regards, Saad

    Friday, November 21, 2014 1:19 PM
  • Hi,

    Sandbox has restirctions restrictions like below.

    • Only the HTTP and HTTPS protocols are allowed.
    • Access to localhost (loopback) is not permitted.
    • IP addresses cannot be used. You must use a named web address that requires DNS name resolution.
    • Anonymous authentication is supported and recommended. There is no provision for prompting the logged on user for credentials or saving those credentials.

    As you see it is running in "NONE" , this is causing because of the external dll.

    Posting the exact error may help to answer your question.

    Thank you,


    Sreeni Pavalla

    Tuesday, November 25, 2014 9:22 AM