dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.73k stars 3.18k forks source link

SqlServer NetTopologySuite throws Newtonsoft.Json.JsonSerializationException even though I did everything and every setup. #22090

Closed Farnam88 closed 2 years ago

Farnam88 commented 4 years ago

The Exception throws on insert Startup:

services.AddControllersWithViews(options =>
            {
                options.Filters.Add(new AbpAutoValidateAntiforgeryTokenAttribute());
            }).AddNewtonsoftJson(options =>
            {
                options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            });

Entity(I also used every other JSON setup and attributes)

 public class RequestShipment : BaseEntity<long>
    {
        #region + Properties

        public string OriginAddress { get; set; }

        [Column(TypeName = "geometry")]
        [Newtonsoft.Json.JsonProperty(ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
            ItemReferenceLoopHandling = ReferenceLoopHandling.Ignore)]
        public Point OriginLocation { get; set; }

        public string DestinationAddress { get; set; }

        [Column(TypeName = "geometry")]
        [Newtonsoft.Json.JsonProperty(ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
            ItemReferenceLoopHandling = ReferenceLoopHandling.Ignore)]
        public Point DestinationLocation { get; set; }

        public decimal TotalPrice { get; set; }
        public int WebsiteShipmentId { get; set; }
        public long RequestedServiceId { get; set; }
        public ShipmentRequestState ShipmentRequestState { get; set; }

        #endregion

        #region + Relations

        public virtual RequestedService RequestedService { get; set; }
        public virtual WebsiteShipment WebsiteShipment { get; set; }

        #endregion
    }

Fluent API

            builder.Property(a => a.OriginLocation).IsRequired().HasColumnType("geometry");
            builder.Property(a => a.DestinationLocation).IsRequired().HasColumnType("geometry");
private readonly GeometryFactory _geometryFactory;
public RequestShipmentService()
{
        _geometryFactory = NtsGeometryServices.Instance.CreateGeometryFactory(new PrecisionModel(PrecisionModels.Floating)
               ,4326);
}

 public async Task<ResultModel> CalculateShipmentPrice(
            RequestShipmentInputDto inputDto)
{
            try
            {

                var requestShipment = new RequestShipment
                {
                    OriginAddress = inputDto.Origin.Address,
                    OriginLocation =
                        _geometryFactory.CreatePoint(new Coordinate(inputDto.Origin.Longitude,
                            inputDto.Origin.Latitude)),
                    DestinationAddress = inputDto.Destination.Address,
                    DestinationLocation = _geometryFactory.CreatePoint(new Coordinate(inputDto.Destination.Longitude,
                        inputDto.Destination.Latitude)),
                    ShipmentRequestState = ShipmentRequestState.Requested,
                };
                await _requestShipmentRepository.InsertAsync(requestShipment);
                await CurrentUnitOfWork.SaveChangesAsync();
                return ResultModel.Success(ex.Message, 500)

         }
         catch (Exception ex)
         {
             return ResultModel.Fail(ex.Message, 500);
         }
}

the error occurs on CurrentUnitOfWork.SaveChangesAsync() Here is the error Stack Trace:

   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CheckForCircularReference(JsonWriter writer, Object value, JsonProperty property, JsonContract contract, JsonContainerContract containerContract, JsonProperty containerProperty)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter writer, Object value, JsonContainerContract contract, JsonProperty member, JsonProperty property, JsonContract& memberContract, Object& memberValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
   at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)
   at Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
   at Newtonsoft.Json.JsonConvert.SerializeObjectInternal(Object value, Type type, JsonSerializer jsonSerializer)
   at Newtonsoft.Json.JsonConvert.SerializeObject(Object value, Type type, JsonSerializerSettings settings)
   at Newtonsoft.Json.JsonConvert.SerializeObject(Object value, JsonSerializerSettings settings)
   at Abp.Json.JsonExtensions.ToJsonString(Object obj, JsonSerializerSettings settings)
   at Abp.Json.JsonExtensions.ToJsonString(Object obj, Boolean camelCase, Boolean indented)
   at Abp.EntityHistory.EntityHistoryHelper.CreateEntityPropertyChange(Object oldValue, Object newValue, IProperty property)
   at Abp.EntityHistory.EntityHistoryHelper.GetPropertyChanges(EntityEntry entityEntry, Boolean auditedPropertiesOnly)
   at Abp.EntityHistory.EntityHistoryHelper.CreateEntityChangeSet(ICollection`1 entityEntries)
   at Abp.Zero.EntityFrameworkCore.AbpZeroCommonDbContext`3.SaveChangesAsync(CancellationToken cancellationToken)
   at Abp.EntityFrameworkCore.Uow.EfCoreUnitOfWork.SaveChangesInDbContextAsync(DbContext dbContext)
   at Abp.EntityFrameworkCore.Uow.EfCoreUnitOfWork.SaveChangesAsync()
   at FAStudio.Mexanik.App.RequestedServiceServices.RequestShipmentServices.RequestShipmentService.CalculateShipmentPrice(RequestShipmentInputDto inputDto) in D:\Software Programming Projects\Fa Studio\mechanicback\src\FAStudio.Mexanik.Application\App\RequestedServiceServices\RequestShipmentServices\RequestShipmentService.cs:line 102

Message: Self referencing loop detected for property 'CoordinateValue' with type 'NetTopologySuite.Geometries.Coordinate'. Path 'Coordinates[0]'. Please help me with this issue I have searched all over the internet and non of solutions worked so far. Thanks

Further technical details

EF Core version: 3.1.7 Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: NET Core 3.0 Operating system: Windows 10 IDE: Jetbrains Rider

Farnam88 commented 4 years ago

I find the answer. It is actually https://github.com/aspnetboilerplate/aspnetboilerplate issue that could not integrate with SqlServer.NetTopologySuite