Error: Type '' is not marked as serializable in Asp.Net Core

Dung Do Tien Mar 02 2021 85

I have a project with Asp.Net core 3.1. I created a View component to display some data into my view but I got an error: Type 'CMS.Models.SalonPromo' in Assembly 'CMS.Plugin.Product, Version=1.0.0.1, Culture=neutral, PublicKeyToken=null' is not marked as serializable.

This is the detail of that error:

System.Runtime.Serialization.SerializationException: Type 'CMS.Domains.Models.SalonPromo' in Assembly 'CMS.Plugin.Product, Version=1.0.0.1, Culture=neutral, PublicKeyToken=null' is not marked as serializable.
   at System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(Type type)
   at System.Runtime.Serialization.FormatterServices.<>c.<GetSerializableMembers>b__5_0(MemberHolder mh)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context)
   at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo()
   at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder)
   at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder)
   at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, BinaryFormatterWriter serWriter, Boolean fCheck)
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Boolean check)
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph)
   at DVG.Autoportal.Chobrod.Core.Infrastructure.Caching.RedisCached.ZipToBytes[T](T item, String key) in C:\CMS.Core\Infrastructure\Caching\RedisCached.cs:line 647

And this is my model:

public class SalonPromo
{
    public int SalonId { get; set; }
    public int PositionId { get; set; }
    public int[] ProductIds { get; set; }
    public string Banner { get; set; }
    public string Description { get; set; }
}

And this is my ViewComponent code behind:

public async Task<IViewComponentResult> InvokeAsync(int position)
{
    try
    {
        var salonPromo = await _cacheAppService.ExecuteAsync(() => _salonService.GetSalonPromoByPosition(position), StaticVariable.CacheDataExpireShortTime);
        return View(salonPromo);
    }
    catch (Exception ex)
    {
        _logger.LogError(ex, ex.Message);
        return View(new SalonPromo());
    }
}

Thanks for any suggestions.

Have 1 answer(s) found.
  • m

    majid gholipour Mar 02 2021

    You have to add a [Serializable] filter attribute to your SalonPromo model which you want to serialize.

    [Serializable]
    public class SalonPromo
    {
        public int SalonId { get; set; }
        public int PositionId { get; set; }
        public int[] ProductIds { get; set; }
        public string Banner { get; set; }
        public string Description { get; set; }
    }

    I hope it helpful for you.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close