gpt4 book ai didi

c# - SharpMap 和 Entity Framework : The invoked member is not supported in a dynamic assembly

转载 作者:行者123 更新时间:2023-11-30 16:19:26 26 4
gpt4 key购买 nike

我在自定义“ map 小部件”组件中使用清晰的 map 。为了填充 map ,我想使用 Entity Framework ,它位于一个单独的 DLL 中。如果我创建一个 map ,然后获取数据,这很好用。

public void loadMap() {
var map = new MapWidget(); // Create a new widget which internally uses SharpMap
map.AddCountriesLayer(); // Load the map background from .shp file
var data = new IPService.GetPointsForMap(); // Gets IP address from entity framework, inside "domain.dll"
map.AddDots(data); // Add dots
}

但是,如果我先获取数据,然后制作 map ,事情就会崩溃:

public void loadMap() {
var data = new IPService.GetPointsForMap(); // Accessing entity framework before sharpmap
var map = new MapWidget();
map.AddCountriesLayer();
map.AddDots(data);
}

结果

   System.NotSupportedException "The invoked member is not supported in a dynamic assembly."
at System.Reflection.Emit.InternalAssemblyBuilder.GetExportedTypes()
at GeoAPI.GeometryServiceProvider.ReflectInstance()
at GeoAPI.GeometryServiceProvider.get_Instance()
at SharpMap.Data.Providers.ShapeFile.set_SRID(Int32 value) in C:\dev\DLLs\SharpMap Source\Trunk\SharpMap\Data\Providers\ShapeFile.cs:line 859
at SharpMap.Data.Providers.ShapeFile.ParseProjection() in C:\dev\DLLs\SharpMap Source\Trunk\SharpMap\Data\Providers\ShapeFile.cs:line 978
at SharpMap.Data.Providers.ShapeFile..ctor(String filename, Boolean fileBasedIndex) in C:\dev\DLLs\SharpMap Source\Trunk\SharpMap\Data\Providers\ShapeFile.cs:line 302
at Dashboard.Widgets.MapWidget.AddCountriesLayer() in c:\dev\Dashboard\v1\Dashboard\Classes\Widgets\Generic\MapWidget.cs:line 86

这到底是怎么回事?为什么先用entity framework会破?

为了解决这个问题,我将其添加到 program.cs 中,以强制首先加载小部件。

static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

// Hack to force SharpMap to register before entity framework
var widget = new Widgets.MapWidget();
widget.Update();

Application.Run(new DashboardForm());
}

但是,我不喜欢它 - 它看起来很脆弱而且我不喜欢“巧合编码”。我能做些什么来修复它吗?

注意事项:

我找到了这篇博文:http://elegantcode.com/2010/01/28/the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception/我将域程序集添加到 connectionString

我的项目结构是这样的:

仪表盘.exe

  • App.Config 包含连接字符串
  • 引用 SharpMap
  • 引用域.Dll
  • 包含 MapWidget

域.dll

  • 包含领域模型和服务
  • 使用实体模型进行持久化
  • App.config 包含连接字符串、 Entity Framework 配置部分和 Entity Framework 连接工厂

所以我的问题是:

  1. 为什么会这样?
  2. 我能做些什么来阻止它? (如果没有,有没有比 Program.cs 更好的地方来存放骇人听闻的代码)

感谢阅读,如有不明白的地方欢迎指正。

最佳答案

我有一个非常相似的问题,但我没有使用 Entity Framework(我使用的是 NHibernate),所以,我发现这可能根本不是代理对象问题。

我也不喜欢“巧合编码”,但我假设通过调用 new MapWidget(),一些与 GeoApi 相关的初始化是在内部执行的——因为 GeoApi 由 SharpMap 在内部使用。

在我的例子中,我没有直接使用 map ,我只是使用 NHibernate 在我的数据库中插入一些地理数据,我得到了完全相同的堆栈跟踪,所以我认为这可能是同一个问题。

尽管我很讨厌它,但我有这样的事情:

// my object to be persisted using NHibernate
var myObj = new MyObj();

// add polygon of type GeoAPI.Geometries.IGeometry
myObj.CoveredArea = myGeoFactory.CreatePolygonArea(/* ... */);

// use NHibernate to save my obj
sessioNScope.Save(myObj); // <- throws NotSupportedException here

它给了我和你一样的异常(exception)。改成之后

// Ignore this line: hack to initialize GeoApi
new Map();

// my object to be persisted using NHibernate
var myObj = new MyObj();

// add polygon of type GeoAPI.Geometries.IGeometry
myObj.CoveredArea = myGeoFactory.CreatePolygonArea(/* ... */);

// use NHibernate to save my obj
sessioNScope.Save(myObj);

它工作得很好。在我的例子中,我使用了 new Map() 而不是 new MapWidget() 因为它是一个 应用。

TLDR:将其视为执行初始化的 hack

关于c# - SharpMap 和 Entity Framework : The invoked member is not supported in a dynamic assembly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15317420/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com