与Dapper一起使用Microsoft.SqlServer.Types时发生RuntimeBinderInternalCompilerException [英] RuntimeBinderInternalCompilerException when using Microsoft.SqlServer.Types with Dapper

查看:93
本文介绍了与Dapper一起使用Microsoft.SqlServer.Types时发生RuntimeBinderInternalCompilerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Using a Sql Server Data Tools project whose target platform is set to one of:


  • SQL Server 2008

  • SQL Server 2012

  • SQL Server 2014

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

And deploying to (localdb)\Projects or (localdb)\ProjectsV12

调用一个存储过程,该存储过程返回Geometry,Geography或HierachyId类型,例如:

Calling a stored procedure that returns a Geometry, Geography or HierachyId type such as:

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行上出现以下异常。

results in the following exception on the STEquals line.


Microsoft.CSharp.RuntimeBinder.RuntimeBinderInternalCompilerException
未通过用户代码处理HResult = -2146233088消息=绑定动态操作时发生
意外异常

Source = Microsoft.CSharp StackTrace:Microsoft.CSharp.RuntimeBinder.RuntimeBinder.Bind(DynamicMetaObjectBinder
有效载荷,IEnumerable 1参数,DynamicMetaObject [] args,
DynamicMetaObject& deferredBinding)
at Microsoft.CSharp.RuntimeBinder.BinderHelper.Bind(DynamicMetaObjectBinder
操作,RuntimeBinder绑定程序,IEnumerable
1个参数,IEnumerable 1
arginfos,DynamicMetaObject onBindingError)
,位于Microsoft.CSharp.RuntimeBinder.CSharpConvertBinder.FallbackConvert(DynamicMetaObject
目标,DynamicMetaObject errorSuggestion)
,位于System.Dy namic.DynamicMetaObject.BindConvert(ConvertBinder活页夹)
在System.Dynamic.ConvertBinder.Bind(DynamicMetaObject目标,DynamicMetaObject [] args)
在System.Dynamic.DynamicMetaObjectBinder.Bind(Object [] args,ReadOnlyCollection
1个参数,LabelTarget returnLabel)在System.Runtime.CompilerServices.CallSiteBinder.BindCore [T](CallSite`1
网站,Object [] args)在系统中
.Dynamic.UpdateDelegates.UpdateAndExecute1 [T0,TRet](CallSite
网站,T0 arg0)
,位于DATailor.Examples.Dapper.SqlClient.Test.AllTypesDAOTest.TestAllTypesDynamic()

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, IEnumerable1 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, ReadOnlyCollection1 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,但有一个潜在的异常正在被吞噬。

Although the root cause is not Dapper, there is an underlying exception that is being swallowed.

使用ADO.Net代码,例如:

Using ADO.Net code like:

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 <中抛出以下异常/ code>

The following exception is thrown at reader.GetValue


未处理System.InvalidCastException HResult = -2147467262

消息= [A] Microsoft无法将.SqlServer.Types.SqlGeometry强制转换为
[B] Microsoft.SqlServer.Types.SqlGeometry。类型A源自
'Microsoft.SqlServer.Types,Version = 10.0.0.0,Culture = neutral,
PublicKeyToken = 89845dcd8080cc91'在上下文'Default'中的位置
'C:\ Windows \GAC_MSIL\Microsoft.SqlServer.Types\10.0.0.0__89845dcd8080cc91\Microsoft.SqlServer.Types.dll'。
类型B源自上下文
中的'Microsoft.SqlServer.Types,Version = 11.0.0.0,
Culture = neutral,PublicKeyToken = 89845dcd8080cc91' C:Windows程序集GAC_MSIL Microsoft.SqlServer.Types 11.0.0.0__89845dcd8080cc91Microsoft.SqlServer.Types.dll。
Source = DynamicGeometryIssue StackTrace:
在c:\Users\rich\Documents\Visual Studio
2013\Projects\DynamicGeometryIssue\中的DynamicGeometryIssue.TestDao.TestGeometry() DynamicGeometryIssue.TestDao.cs:line
27
在DynamicGeometryIssue.Program.Main(String [] args)在c:\Users\rich\Documents\Visual Studio
2013 \项目\DynamicGeometryIssue\DynamicGeometryIssue\Program.cs:line
15
在System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly,String [] args)
在System.AppDomain.ExecuteAssembly (String assemblyFile,Evidence assemblySecurity,String [] args)Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在System.Threading.ThreadHelper.ThreadStart_Context(Object state)在系统处
.Threading.ExecutionContext.RunInternal(ExecutionContext
executionContext,ContextCallback回调,对象状态,布尔值
serveSyncCtx) b $ b在System.Threading.ExecutionContext.Run(ExecutionContext executeContext,ContextCallback回调,对象状态,布尔值
reserveSyncCtx)
在System.Threading.ExecutionContext.Run(ExecutionContext执行上下文,ContextCallback回调,对象状态) System.Threading.ThreadHelper.ThreadStart()处的
InnerException:

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中使用。请参阅以下MSDN文档的 SQL CLR数据类型部分

The cause of the underlying exception is a known breaking change in SQL Server 2012. See the SQL CLR Data Types section of the following MSDN documentation

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

对我有用的解决方法是在app.config或web.config中创建以下bindingRedirect。

The resolution, that has worked for me is to create the following bindingRedirect in the app.config or web.config.

<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版。

Alternately, with .NET 4.5 you can change your connection string to include a value of "SQL Server 2012" for the "Type System Version" attribute to force SqlClient to load version 11.0 of the assembly.

另一种解决方法是代码,例如:

Another workaround is code like:

var geo = SqlGeography.Deserialize(rdr.GetSqlBytes(0));

但是,我不认为这是Dapper的选择。

However, I don't believe this is an option with Dapper.

这篇关于与Dapper一起使用Microsoft.SqlServer.Types时发生RuntimeBinderInternalCompilerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆