Displaying Xaml document in SSRS

3 posts, 0 answers
  1. Edward
    Edward avatar
    83 posts
    Member since:
    Feb 2011

    Posted 05 Jun 2015 Link to this post

    So I have been using the richtexteditor using Xaml.  Recently I need to create a SSRS report (SQL Server reporting services) that display those data.  I created an external library using the WPF version of the control in order to convert the data from Xaml format into plain text.

    Except when I try to use this in SSRS I get this error:

    The calling thread must be STA, because many UI components require this.  

    using System;

    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Telerik.Windows.Documents;
    using Telerik.Windows.Documents.FormatProviders;
    using Telerik.Windows.Documents.FormatProviders.Html;
    using Telerik.Windows.Documents.FormatProviders.Xaml;
    using Telerik.Windows.Documents.FormatProviders.Txt;
    using System.Security;
    using System.Threading;

    [assembly: AllowPartiallyTrustedCallers]
    namespace TelerikConvertor
    {   
        public class TelerikConvertorClass
        {
            public string Xaml2Text(string XamlText)
            {
                try {
                if (XamlText == null || !XamlText.StartsWith("<t:Ra"))
                    return XamlText;
                XamlFormatProvider x = new XamlFormatProvider();
                TxtFormatProvider t = new TxtFormatProvider();
                return t.Export(x.Import(XamlText));
                }
                catch (Exception e)
                {
                    return e.Message;
                }
            }

            public string HelloWorld()
            {
                return "hello world";
            }

        }
    }

  2. Edward
    Edward avatar
    83 posts
    Member since:
    Feb 2011

    Posted 05 Jun 2015 Link to this post

    I got the same error if I try to do it like this:

     public string Xaml2Text2(string t)
            {
                try
                {
                    var thread = new Thread(Xaml2TextThread);
                    var threadData = new ConvertThreadData { InputText = t};
                    thread.SetApartmentState(ApartmentState.STA);
                    thread.Start(threadData);
                    thread.Join();
                    return threadData.OutputText;
                } catch (Exception e) {
                    return e.Message;
                }
            }

     private void Xaml2TextThread(object x)
            {
                var threadData = x as ConvertThreadData;

                if (threadData.InputText == null || !threadData.InputText.StartsWith("<!DOC"))
                    threadData.OutputText = threadData.InputText;
                else
                {
                    XamlFormatProvider h = new XamlFormatProvider();
                    TxtFormatProvider t = new TxtFormatProvider();
                    threadData.OutputText = t.Export(h.Import(threadData.InputText));
                }
            }

    private class ConvertThreadData
            {
                public string InputText { get; set; }
                public string OutputText { get; set; }
            }

  3. DevCraft banner
  4. Edward
    Edward avatar
    83 posts
    Member since:
    Feb 2011

    Posted 05 Jun 2015 Link to this post

    Please disregard this, I reboot my computer and it somehow worked.
Back to Top