0

I've created a .net core 5 web application with visual studio 2019. I've added GDAL Packages to my project. When I run the web application locally there are no problems but when I publish the application on the IIS it makes an error. The Packages that I've added are like below: GDAL Packages

and my code is like this :

public IActionResult GetLayerInfo(string fileName)
        {

            var path = Path.Combine(
                        Directory.GetCurrentDirectory(), @"wwwroot\files\import",
                        fileName);

            List<string> fList = new List<string>();
            GdalConfiguration.ConfigureGdal();
            GdalConfiguration.ConfigureOgr();

            Ogr.RegisterAll();

            DataSource sDatasource = Ogr.Open(path, 1);
            if (sDatasource == null)
                return null;

            OSGeo.OGR.Layer sLayer = sDatasource.GetLayerByIndex(0);
            FeatureDefn srcVectorDefn = sLayer.GetLayerDefn();
            var type = srcVectorDefn.GetGeomType();
            var geomType = wkbGeometryType.GetName(type);
            var spatialReference = sLayer.GetSpatialRef();
            long fcount = sLayer.GetFeatureCount(1);
            var epsg = spatialReference.GetAttrValue("AUTHORITY", 1);
            for (int i = 0; i < srcVectorDefn.GetFieldCount(); i++)
            {
                FieldDefn fieldDefn = srcVectorDefn.GetFieldDefn(i);
                fList.Add(fieldDefn.GetName());
            }
            sDatasource.Dispose();
            return Json(new
            {
                fCount = fcount,
                epsg = epsg,
                geomType = geomType.Replace("wkb", ""),
                fList = fList,
                tName = Path.GetFileNameWithoutExtension(fileName)
            });
        }

and when I publish project in the IIS the error is like this :

An unhandled exception occurred while processing the request.
NotSupportedException: Serialization and deserialization of 'System.Type' instances are not supported and should be avoided since they can lead to security issues.
System.Text.Json.Serialization.Converters.TypeConverter.Write(Utf8JsonWriter writer, Type value, JsonSerializerOptions options)

NotSupportedException: Serialization and deserialization of 'System.Type' instances are not supported and should be avoided since they can lead to security issues. Path: $.error.TargetSite.DeclaringType.
System.Text.Json.ThrowHelper.ThrowNotSupportedException(ref WriteStack state, NotSupportedException ex)

The Error fired when it reachs this line

Ogr.RegisterAll();

what is the problem ?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
hamed
  • 1
  • 2
  • Serialize/deserialize anything (like `System.Type`) can be risky, so when .NET Core migrated to `System.Text.Json` from JSON.NET, Microsoft decided to raise such exceptions to raise awareness, https://github.com/dotnet/runtime/issues/41920 – Lex Li Jul 06 '22 at 05:21
  • The same error you can use as reference: Declare an enumeration for your types you can parse the value for the requested type explicitly. [.NET Core: Type serialization denied](https://www.banditoth.net/2021/03/11/net-core-type-serialization-denied). – samwu Jul 06 '22 at 05:24
  • Lex Li tnx but how can I solve it in this case ?? – hamed Jul 06 '22 at 06:02
  • Might you please [edit] your question to share the full `ToString()` output of your exception including exception type, message, **complete** traceback and inner exception(s) if any? Just a guess, is there any possibility you are catching exceptions somewhere and trying to return the exception itself along with some error code? If so that would account for the error you are seeing, System.Text.Json cannot serialize an exception. – dbc Jul 06 '22 at 06:34
  • dbc tnx, bro you save my life . – hamed Jul 06 '22 at 07:47

0 Answers0