This is a migrated thread and some comments may be shown as answers.

Displaying Xaml document in SSRS

2 Answers 9 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Edward
Top achievements
Rank 1
Edward asked on 05 Jun 2015, 03:07 PM

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 Answers, 1 is accepted

Sort by
0
Edward
Top achievements
Rank 1
answered on 05 Jun 2015, 03:09 PM

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; }
        }

0
Edward
Top achievements
Rank 1
answered on 05 Jun 2015, 08:15 PM
Please disregard this, I reboot my computer and it somehow worked.
Tags
RichTextBox
Asked by
Edward
Top achievements
Rank 1
Answers by
Edward
Top achievements
Rank 1
Share this question
or