gpt4 book ai didi

.net - 将 Microsoft.SqlServer.Types 与 Dapper 一起使用时的 RuntimeBinderInternalCompilerException

转载 作者:行者123 更新时间:2023-12-03 13:42:52 24 4
gpt4 key购买 nike

使用目标平台设置为以下之一的 Sql Server Data Tools 项目:

  • SQL Server 2008
  • SQL Server 2012
  • SQL Server 2014

  • 并部署到 (localdb)\Projects 或 (localdb)\ProjectsV12

    调用返回 Geometry、Geography 或 HierachyId 类型的存储过程,例如:
    CREATE PROCEDURE [dbo].[SelectSqlGeometry]
    @x Geometry
    AS
    SELECT @x as y
    RETURN 0

    以下调用代码:
    var result = Connection.Query("dbo.SelectSqlGeometry", new { x = geometry }, commandType: CommandType.StoredProcedure).First();
    bool isSame = ((bool)geometry.STEquals(result.y));

    在 STEquals 行上导致以下异常。

    Microsoft.CSharp.RuntimeBinder.RuntimeBinderInternalCompilerException was unhandled by user code HResult=-2146233088 Message=An unexpected exception occurred while binding a dynamic operation
    Source=Microsoft.CSharp StackTrace: at Microsoft.CSharp.RuntimeBinder.RuntimeBinder.Bind(DynamicMetaObjectBinder payload, IEnumerable1 parameters, DynamicMetaObject[] args,
    DynamicMetaObject& deferredBinding)
    at Microsoft.CSharp.RuntimeBinder.BinderHelper.Bind(DynamicMetaObjectBinder
    action, RuntimeBinder binder, IEnumerable
    1 args, IEnumerable1
    arginfos, DynamicMetaObject onBindingError)
    at Microsoft.CSharp.RuntimeBinder.CSharpConvertBinder.FallbackConvert(DynamicMetaObject
    target, DynamicMetaObject errorSuggestion)
    at System.Dynamic.DynamicMetaObject.BindConvert(ConvertBinder binder)
    at System.Dynamic.ConvertBinder.Bind(DynamicMetaObject target, DynamicMetaObject[] args)
    at System.Dynamic.DynamicMetaObjectBinder.Bind(Object[] args, ReadOnlyCollection
    1 parameters, LabelTarget returnLabel) at System.Runtime.CompilerServices.CallSiteBinder.BindCore[T](CallSite`1 site, Object[] args) at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0) at DATailor.Examples.Dapper.SqlClient.Test.AllTypesDAOTest.TestAllTypesDynamic()

    最佳答案

    虽然根本原因不是 Dapper,但有一个潜在的异常正在被吞噬。

    使用 ADO.Net 代码,如:

    var geometry = Util.CreateSqlGeometry();
    SqlDataReader reader=null;
    SqlCommand cmd=null;
    try
    {
    cmd = new SqlCommand("dbo.SelectSqlGeometry", (SqlConnection)Connection);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add(new SqlParameter("@x", geometry) { UdtTypeName = "Geometry" });
    cmd.ExecuteNonQuery();
    reader = cmd.ExecuteReader();
    while (reader.Read())
    {
    var y = (SqlGeometry)reader.GetValue(0);
    var same = geometry.STEquals(y);
    }
    reader.Dispose();
    reader = null;
    }
    finally
    {
    if (reader != null)
    {
    if (!reader.IsClosed) try { cmd.Cancel(); }
    catch {}
    reader.Dispose();
    }
    if (cmd != null) cmd.Dispose();
    Connection.Close();
    }

    reader.GetValue 处抛出以下异常

    System.InvalidCastException was unhandled HResult=-2147467262
    Message=[A]Microsoft.SqlServer.Types.SqlGeometry cannot be cast to [B]Microsoft.SqlServer.Types.SqlGeometry. Type A originates from 'Microsoft.SqlServer.Types, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' in the context 'Default' at location 'C:\Windows\assembly\GAC_MSIL\Microsoft.SqlServer.Types\10.0.0.0__89845dcd8080cc91\Microsoft.SqlServer.Types.dll'. Type B originates from 'Microsoft.SqlServer.Types, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' in the context 'Default' at location 'C:\Windows\assembly\GAC_MSIL\Microsoft.SqlServer.Types\11.0.0.0__89845dcd8080cc91\Microsoft.SqlServer.Types.dll'. Source=DynamicGeometryIssue StackTrace: at DynamicGeometryIssue.TestDao.TestGeometry() in c:\Users\rich\Documents\Visual Studio 2013\Projects\DynamicGeometryIssue\DynamicGeometryIssue\TestDao.cs:line 27 at DynamicGeometryIssue.Program.Main(String[] args) in c:\Users\rich\Documents\Visual Studio 2013\Projects\DynamicGeometryIssue\DynamicGeometryIssue\Program.cs:line 15 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException:



    基础异常的原因是 SQL Server 2012 中已知的重大更改。请参阅 SQL CLR 数据类型 以下 MSDN 文档的部分

    http://msdn.microsoft.com/en-us/library/ms143179(v=sql.110).aspx

    对我有用的解决方案是在 app.config 或 web.config 中创建以下 bindingRedirect。
    <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
    <assemblyIdentity name="Microsoft.SqlServer.Types"
    publicKeyToken="89845dcd8080cc91"
    culture="neutral" />
    <bindingRedirect oldVersion="10.0.0.0"
    newVersion="11.0.0.0"/>
    </dependentAssembly>
    </assemblyBinding>
    </runtime>

    或者,在 .NET 4.5 中,您可以更改连接字符串以包含“类型系统版本”属性的“SQL Server 2012”值,以强制 SqlClient 加载程序集的 11.0 版。

    另一种解决方法是如下代码:
    var geo = SqlGeography.Deserialize(rdr.GetSqlBytes(0));

    但是,我认为这不是 Dapper 的一个选项。

    关于.net - 将 Microsoft.SqlServer.Types 与 Dapper 一起使用时的 RuntimeBinderInternalCompilerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25600043/

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