简体   繁体   English

Microsoft.VisualBasic.dll中发生了类型为'System.InvalidCastException'的第一次机会异常

[英]A first chance exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll

I'm working on converting some console code in a Microsoft sample for Dynamics CRM SDK 2011 to an ASP.NET web application. 我正在将Dynamics CRM SDK 2011的Microsoft示例中的某些控制台代码转换为ASP.NET Web应用程序。 It runs fine in console mode, but after converting to run in ASP.NET I get the error: 它在控制台模式下运行良好,但是转换为在ASP.NET中运行后,出现错误:

A first chance exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll Microsoft.VisualBasic.dll中发生了类型为'System.InvalidCastException'的第一次机会异常

Here is my source code: 这是我的源代码:

    Public Sub Run(ByVal connectionString As String, ByVal promptforDelete As Boolean)
    Try
        Dim connection As Microsoft.Xrm.Client.CrmConnection =
            CrmConnection.Parse(connectionString)
        _orgService = New OrganizationService(connection)
        Using _orgService
            Dim account_Renamed As Account = New Account With {.Name = "Fourth Coffee"}
            _accountId = _orgService.Create(account_Renamed)
            Response.Write(CType("{0} {1}", Char()), account_Renamed.LogicalName, account_Renamed.Name)
            Dim cols As New ColumnSet(New String() {"name",
                                                   "address1_postalcode",
                                                    "lastusedincampaign"})
            Dim retrievedAccount As Account = _orgService.Retrieve("account", _accountId, cols).ToEntity(Of Account)()
            Response.Write("Retrieved record.")
            retrievedAccount.Address1_PostalCode = "98052"
            Response.Write("Updated Account Postal Code.")

        End Using
    Catch ex As Exception

    End Try
End Sub

So, I figured it out. 所以,我想通了。 In a web-based application there is no need to use {0} {1}: 在基于Web的应用程序中,无需使用{0} {1}:

Response.Write(CType("{0} {1}", Char()), account_Renamed.LogicalName, account_Renamed.Name)

Instead it can look like this: 而是看起来像这样:

Response.Write(account_Renamed.LogicalName & " " & account_Renamed.Name)

The reason that fails is not related to your application being web based. 失败的原因与您的基于Web的应用程序无关。 It has to do with how you're using CType() . 这与您如何使用CType() You're using it to concatenate two strings which is not what it's used for. 您正在使用它来连接两个字符串,这不是它的用途。

The CType() method is used to cast an object to a particular type. CType()方法用于将对象转换为特定类型。 With this snippet of code: 使用以下代码段:

CType("{0} {1}", Char())

You're telling CType() to convert the string literal "{0} {1}" to a character array. 您要告诉CType()将字符串文字"{0} {1}"转换为字符数组。 Which actually works! 哪个有效! Now you're passing a character array to Reponse.Write() along with two additional parameters which appear to be string values to me. 现在,您将一个字符数组传递给Reponse.Write()以及两个附加参数,这些参数对我来说似乎是字符串值。

There is an overload of Reponse.Write() that accepts three arguments, the first being a character array but the other values are integers (index and count). Reponse.Write()有一个重载,它接受三个参数,第一个是字符数组,而其他值是整数(索引和计数)。

Honestly, I'm surprised this even built let alone ran. 老实说,令我惊讶的是甚至连它都没有运行。 For future code, string concatenation is best done (IMHO) like so: 对于将来的代码,最好使用字符串连接(IMHO),如下所示:

String.Format("{0} {1}", account_Renamed.LogicalName, account_Renamed.Name)

instead of concatenating with the & operator. 而不是与&运算符串联。 If you concatenated the string in this manner, your code would change to this. 如果以这种方式连接字符串,则代码将更改为此。

Response.Write(String.Format("{0} {1}", account_Renamed.LogicalName, account_Renamed.Name))

Using String.Format() is actually a shortcut way of using the StringBuilder class which is very efficient at string processing. 实际上,使用String.Format()是使用StringBuilder类的快捷方式,该类在处理字符串时非常有效。 When you use the concatenate operator, & , you create a completely new string object in memory every time a piece is attached to the string. 使用串联运算符& ,每次在字符串上附加一个片段时,都会在内存中创建一个全新的字符串对象。 StringBuilder creates/mutates strings in a mutable manner and is far faster when doing a lot of string work. StringBuilder以可变的方式创建/更改字符串,并且在执行大量字符串工作时要快得多。 That's why I consider it to be a best practice to use the shortcut method. 这就是为什么我认为使用快捷方式方法是最佳做法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 EntityFramework.Core.dll中出现“System.InvalidCastException”类型的异常,但未在用户代码中处理 - An exception of type 'System.InvalidCastException' occurred in EntityFramework.Core.dll but was not handled in user code 类型System.Security.Cryptography.CryptographicException的第一次机会异常发生在System.Core.dll中 - A first chance exception of type 'System.Security.Cryptography.CryptographicException' occurred in System.Core.dll mscorlib.dll中发生类型'System.UnauthorizedAccessException'的第一次机会异常吗? - A first chance exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll? System.InvalidCastException在 - System.InvalidCastException in Microsoft.Practices.EnterpriseLibrary.Data.dll中发生了类型为'System.InvalidOperationException'的异常 - An exception of type 'System.InvalidOperationException' occurred in Microsoft.Practices.EnterpriseLibrary.Data.dll Microsoft.AspNet.Identity.EntityFramework.dll中发生类型为“ System.ArgumentNullException”的异常 - An exception of type 'System.ArgumentNullException' occurred in Microsoft.AspNet.Identity.EntityFramework.dll DAL.dll中发生了'System.StackOverflowException'类型的未处理异常 - An unhandled exception of type 'System.StackOverflowException' occurred in DAL.dll 在 mscorlib.dll 中发生 System.StackOverflowException 类型的未处理异常 - An unhandled exception of type System.StackOverflowException occurred in mscorlib.dll System.InvalidCastException:无法转换类型为'System.Linq.GroupedEnumerable的对象 - System.InvalidCastException: Unable to cast object of type 'System.Linq.GroupedEnumerable 如何避免错误——System.InvalidCastException:无法将 object 转换为类型“System.Collections.Generic.ICollection” - How to Avoid Error -- System.InvalidCastException: Unable to cast object to type 'System.Collections.Generic.ICollection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM