在C#.NET Core 5中使用GDAL软件包导致错误

发布于 2025-02-13 11:31:26 字数 2561 浏览 0 评论 0原文

我已经使用Visual Studio 2019创建了一个.NET Core 5 Web应用程序。我已经在项目中添加了GDAL软件包。当我本地运行Web应用程序时,没有问题,但是当我在IIS上发布应用程序时,会出错。我添加的包装如下: gdal packages

,我的代码就是这样:

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

当我在IIS中发布项目时,错误是错误的是这样:

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)

到达此行时发出的错误

Ogr.RegisterAll();

是什么问题?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文