Telerik.Reporting.Report

1 Answer 6 Views
.NET Core DataSources Serialization
TRAN
Top achievements
Rank 1
TRAN asked on 01 Jul 2025, 03:47 PM

Hi team, please help to check this error

Telerik.Reporting.Serialization.SerializerExcepion
  HResult=0x80131500
  Message=The XML serializer cannot resolve type with name: System.ComponentModel.ISite
  Source=Telerik.Reporting
  StackTrace:
   at Telerik.Reporting.Serialization.ObjectReader.ReadValue(Object obj, PropertyDescriptor prop)
   at Telerik.Reporting.Serialization.ObjectReader.ReadAttributes(Object obj, PropertyDescriptorCollection props)
   at Telerik.Reporting.Serialization.ObjectReader.ReadProperties(Object obj)
   at Telerik.Reporting.Serialization.ObjectReader.ReadObject(Type type)
   at Telerik.Reporting.Serialization.ObjectReader.ReadXmlElement(String name)
   at Telerik.Reporting.Serialization.ObjectReader.ReadCollection(Object collection)
   at Telerik.Reporting.Serialization.ObjectReader.ReadProperties(Object obj)
   at Telerik.Reporting.Serialization.ObjectReader.ReadObject(Type type)
   at Telerik.Reporting.Serialization.ObjectReader.ReadXmlElement(String name)
   at Telerik.Reporting.Serialization.ObjectReader.Deserialize(IResourceHandler handler)
   at Telerik.Reporting.JsonSerialization.JsonSerializer.Deserialize(String value)
   at Telerik.Reporting.JsonSerialization.JsonSerializer.Deserialize(StreamReader reader)
   at Telerik.Reporting.JsonSerialization.JsonSerializer.Deserialize(Stream stream)
   at Telerik.Reporting.JsonSerialization.ReportJsonSerializer.Deserialize(Stream stream)
   at MOS.ReportWriter.Web.Core.TelerikExtensions.ReportHelper.LoadReportFromDataWithDatasources(String reportData, List`1 webServiceDataSources) in C:\working\sourcecode\hrs-report\MOS.ReportWriter.Web\Core\TelerikExtensions\ReportHelper.cs:line 110

  This exception was originally thrown at this call stack:
    [External Code]

Inner Exception 1:
InvalidCastException: Invalid cast from 'System.String' to 'System.ComponentModel.ISite'.

When i trying to modify the WebServiceDatasource in the report document with code below


public static Telerik.Reporting.Report LoadReportFromDataWithDatasources(string reportData, List<CustomeWebServiceDataSource> webServiceDataSources)
{
    try
    {
        using MemoryStream stream = new(Convert.FromBase64String(reportData));
        var reportPackager = new ReportPackager();
        Telerik.Reporting.Report reportDocument = (Telerik.Reporting.Report)reportPackager.UnpackageDocument(stream);

        // Filter only WebServiceDataSources
        var originalDataSources = reportDocument.GetDataSources();
        var webServiceSources = originalDataSources.OfType<WebServiceDataSource>().ToList();

        // Match and update existing WebServiceDataSources
        var matchedDataSources = webServiceSources
            .Where(ws => webServiceDataSources.Any(x => x.ServiceUrl == ws.ServiceUrl))
            .ToList();

        var unmatchedCustomSources = webServiceDataSources
            .Where(x => !webServiceSources.Any(ws => ws.ServiceUrl == x.ServiceUrl))
            .ToList();

        var rebuiltDataSources = new List<object>();

        foreach (var ws in matchedDataSources)
        {
            var matchingCustom = webServiceDataSources.FirstOrDefault(x => x.ServiceUrl == ws.ServiceUrl);
            var paramValues = JsonConvert.DeserializeObject<Dictionary<string, object>>(matchingCustom?.ParameterValues);

            var updatedWs = new CustomeWebServiceDataSource
            {
                Name = ws.Name,
                DataEncoding = ws.DataEncoding.CodePage,
                Method = ws.Method.ToString(),
                Parameters = ws.Parameters.Select(p => new DataSourceParameter
                {
                    Name = p.Name,
                    Value = new DataSourceParameterValue { Value = p.Value?.ToString() },
                    WebServiceParameterType = p.WebServiceParameterType.ToString()
                }).ToList(),
                ServiceUrl = ws.ServiceUrl,
                DataSelector = ws.DataSelector
            };

            updatedWs.SetParameterValues(paramValues);
            rebuiltDataSources.Add(updatedWs);
        }

        // Add new unmatched custom sources
        rebuiltDataSources.AddRange(unmatchedCustomSources);

        // Preserve non-WebServiceDataSources
        var otherDataSources = originalDataSources.Where(ds => ds is not WebServiceDataSource);
        rebuiltDataSources.AddRange(otherDataSources);

        // Serialize updated report and inject data sources
        var reportContent = SerializeReportToJson(reportDocument);
        var reportJson = JsonConvert.DeserializeObject<JToken>(reportContent);

        // Use full polymorphic list
        reportJson["DataSources"] = JsonConvert.DeserializeObject<JArray>(JsonConvert.SerializeObject(rebuiltDataSources));
        reportContent = JsonConvert.SerializeObject(reportJson);

        using MemoryStream finalStream = new(System.Text.Encoding.UTF8.GetBytes(reportContent));
        var serializer = new ReportJsonSerializer();
        return (Telerik.Reporting.Report)serializer.Deserialize(finalStream);
    }
    catch
    {
        return null;
    }
}
the error occured at 
return (Telerik.Reporting.Report)serializer.Deserialize(finalStream);

1 Answer, 1 is accepted

Sort by
0
Todor
Telerik team
answered on 03 Jul 2025, 08:57 AM

Hi Tran,

I already replied to your Support Ticket raising the same issue. I will also share my thoughts in this public thread, so other Teleirk Reporting users may benefit from it.

Based on the code snippet, I assume you use our class ReportJsonSerializer for serializing your reports.

It seems you modify some WebServiceDataSources in your report to a custom type, CustomeWebServiceDataSource.

Note that our serialization functionality may not be able to serialize some of the custom properties, hence the problem. Our report components and items do not use the 'System.ComponentModel.ISite' type, and I guess that it comes from your custom type. The inner exception states that a String cannot be cast to 'System.ComponentModel.ISite'.

Here is our code that throws:

void ReadValue(object obj, PropertyDescriptor prop)
{
    this.ResourceHandler.NamingContext.StartProperty(prop.Name);

    object value = null;

    var xml = this.GetNodeValue();
    if (xml == this.Settings.NullString)
    {
        value = null;
    }
    else
    {
        var converter = this.GetTypeConverter(prop);

        if (null != converter)
        {
            value = this.ConvertFromString(
                converter,
                this.CreateTypeDescriptorContext(obj, prop),
                xml);
        }
        else
        {
            try
            {
                // Try converts string value to one of the primitive types.
                if (prop.PropertyType == typeof(Type))
                {
                    value = Type.GetType(xml);
                }
                else
                {
                    value = Convert.ChangeType(xml, prop.PropertyType, this.Culture);
                }
            }
            catch (Exception ex)
            {
                throw new SerializerExcepion(
                    String.Format("The XML serializer cannot resolve type with name: {0}", prop.PropertyType),
                    ex);
            }
        }

    }

    prop.SetValue(obj, value);

    this.ResourceHandler.NamingContext.EndProperty();
}

void ReadAttributes(object obj, PropertyDescriptorCollection props)
{
    if (this.reader.MoveToFirstAttribute())
    {
        do
        {
            var readerName = this.GetPropertyName(this.reader.Name);
            var resolvedName = ResolveElementName(readerName);

            var prop = props[resolvedName];
            if (prop != null)
            {
                this.ReadValue(obj, prop);
            }
            else if (this.reader.LookupNamespace(resolvedName) != null)
            {
                // check if attribute is namespace attribute
                continue;
            }
            else
            {
                var msg = string.Format("Attribute {0} not found as a property of the object", this.reader.Name);
                Debug.WriteLine(msg);
            }
        }
        while (this.reader.MoveToNextAttribute());
    }
    this.reader.MoveToElement();
}

I have highlighted the supposed workflow leading to the problem.

As a workaround, you may try marking the custom properties with type ISite and other custom types as non-serializable or removing them, as they cannot be used by the Reporting Engine.

Regards,
Todor
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
.NET Core DataSources Serialization
Asked by
TRAN
Top achievements
Rank 1
Answers by
Todor
Telerik team
Share this question
or