我使用.NET XSD.EXE导入程序从XSD文件集合生成C#类.当我尝试将其中一个类序列化为XML时,它失败了(InvalidOperationException),当我挖到它时,我发现其中一个创建的类看起来是错误的.
以下是相关的XSD代码:
<xsd:complexType name="SuccessType">
<xsd:annotation>
<xsd:documentation>Indicates in a response message that a request was successfully processed.</xsd:documentation>
</xsd:annotation>
<xsd:sequence>
<xsd:element ref="Warnings" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<!-- .. snip .. -->
<xsd:element name="Warnings" type="WarningsType">
<xsd:annotation>
<xsd:documentation>The processing status of a business message and any related warnings or informational messages.</xsd:documentation>
</xsd:annotation>
</xsd:element>
<!-- .. snip .. -->
<xsd:complexType name="WarningsType">
<xsd:annotation>
<xsd:documentation>A collection of warnings generated by the successful processing of a business message.</xsd:documentation>
</xsd:annotation>
<xsd:sequence>
<xsd:element ref="Warning" maxOccurs="unbounded"/>
</xsd:sequence> …Run Code Online (Sandbox Code Playgroud) 我想我知道我的意思,但我不太确定......
Framework 文档总结了该类型如下:
当方法调用对于对象的当前状态无效时抛出的异常。
有明确的情况,我想到的情况是当操作需要打开数据库但对象尚未使用连接到数据库所需的信息进行初始化时。
(切线:另一方面,ADO.NETs 还要求您显式打开连接的行为并不那么明确;DataAdapter 通过简单地打开连接 instad 与此不同,当且仅当它关闭时再次关闭它条目,我发现这很方便,并让自己成为一个 ADO.NET 包装器,该包装器对所有内容都使用此模式。当然,这意味着我冒着执行 2 ExecuteNonQuery 并不必要地将连接返回到两者之间的池的风险,但我仍然可以打开和关闭与获得异常相比,我想要的连接和这种性能损失算不了什么。)
我想我的问题的答案是,只有在这种明确的情况下,我们才应该抛出异常。但是在以下场景中哪种异常类型最合适:
public class FormatterMapping
{
Dictionary formattersByName = new ...();
public IFormatter GetFormatter(string key)
{
IFormatter f;
if (formattersByName.TryGetValue(key, out f))
return f;
else
throw new ??Exception("explanation of why this failed.");
}
}
我的第一反应是抛出 ArgumentException。然后我开始认为映射缺少一个键也可能与参数“错误”一样。基本上“获取格式化程序 X”操作是无效的,因为X 不在映射中,但我真的不知道 X 是否“应该在那里”或者在这里要求 X 是不明智的。
我当然可以通过返回 null 来规避整个问题,但这会打开一个更大、更深的蠕虫罐头。没有办法知道什么时候会使用返回值,所以稍后因 NullReferenceException 而爆炸的代码可能与出错的地方没有明显的关系。要么映射设置不当,要么使用它的代码要求一些不应该的东西。
避免该问题的另一种方法是使用 TryGetFormatter 选项,但我打算使用它的方式实际上调用者应该知道映射中的内容和不包含的内容,因此在用户代码上强制使用此模式不是也不错。
请不要回答我应该抛出 ApplicationException!无论您认为代码应该做什么,请提供原因。毕竟,这里真正有问题的是推理。
除非有人说服我,否则我倾向于 ArgumentException。从映射的角度来看,这个论点是错误的,所以至少有一个明确的推理支持这一点。:)
我有一个Windows手机应用程序,有时得到InvalidOperationExceptions但不知道为什么以及如何避免它们.错误报告中的问题函数是Microsoft.Xna.Framework.Media.MediaLibraryEnumerator_1[[System.__Canon,_mscorlib]].get_Item,我得到这个堆栈跟踪
"Frame Image Function Offset
0 Microsoft.Xna.Framework.ni.dll Microsoft.Xna.Framework.Media.MediaLibraryEnumerator_1[[System.__Canon,_mscorlib]].get_Item 0x0003e4d8
1 Microsoft.Xna.Framework.ni.dll Microsoft.Xna.Framework.Media.MediaLibraryEnumerator_1[[System.__Canon,_mscorlib]].System.Collections.IEnumerator.get_Current 0x00000006
2 Microsoft.Xna.Framework.ni.dll Microsoft.Xna.Framework.Media.MediaLibraryEnumerator_1[[System.__Canon,_mscorlib]].System.Collections.Generic.IEnumerator_T_.get_Current 0x0000001c
3 MapLense.ni.DLL MapLense.Helper.PictureMapping.Add 0x000000a8
4 MapLense.ni.DLL MapLense.Helper.PictureMapping+_GetPicture_d__b.MoveNext 0x000000f6
5 mscorlib.ni.dll System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess 0x00216c46
6 mscorlib.ni.dll System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification 0x0000003a
7 mscorlib.ni.dll System.Runtime.CompilerServices.TaskAwaiter_1[[System.__Canon,_mscorlib]].GetResult 0x0000001c
8 MapLense.ni.DLL MapLense.Helper.Map+_AddPictureToMap_d__17.MoveNext 0x00000118
9 mscorlib.ni.dll System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess 0x00216c46
10 mscorlib.ni.dll System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification 0x0000003a
11 mscorlib.ni.dll System.Runtime.CompilerServices.TaskAwaiter_1[[System.__Canon,_mscorlib]].GetResult 0x0000001c
12 MapLense.ni.DLL MapLense.MainPage+_ViewModelOnPropertyChanged_d__1e.MoveNext 0x00000204
13 mscorlib.ni.dll System.Runtime.CompilerServices.AsyncMethodBuilderCore._ThrowAsync_b__0 0x00000036"
Run Code Online (Sandbox Code Playgroud)
我还尝试在代码块周围添加一个try-catch块,但没有结果
public static bool Add(DBPicture dbpicture)
{
if (Pictures.ContainsKey(dbpicture.UniqueID))
return true;
var root = new MediaLibrary().RootPictureAlbum;
foreach …Run Code Online (Sandbox Code Playgroud) 如果我冲浪,http://localhost:58472/Account/Register我就有这个例外
System.InvalidOperationException:当前类型IUserStore<ApplicationUser>是一个接口,无法构造.你错过了类型映射吗?
这是我的代码:
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(IUserStore<ApplicationUser> store)
: base(store)
{ }
public static ApplicationUserManager Create(
IdentityFactoryOptions<ApplicationUserManager> options,
IOwinContext context)
{
var manager = new ApplicationUserManager(
new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>())
);
// other code
return manager;
}
}
Run Code Online (Sandbox Code Playgroud)
这里是控制器的代码:
[Authorize]
public class AccountController : Controller
{
private ApplicationSignInManager _signInManager;
private ApplicationUserManager _userManager;
public AccountController()
{
}
public AccountController(
ApplicationUserManager userManager,
ApplicationSignInManager signInManager)
{
UserManager = userManager;
SignInManager = signInManager;
}
public ApplicationSignInManager SignInManager …Run Code Online (Sandbox Code Playgroud) c# asp.net-mvc user-management unity-container invalidoperationexception
我的 Winform 应用程序正在根AppDomain.CurrentDomain.UnhandledException级别记录和 Application.ThreadException ,我得到了这个异常:
System.InvalidOperationException:由于对象的当前状态,操作无效。在 System.Windows.Forms.DataGridView.DataGridViewDataConnection.ProcessListChanged(ListChangedEventArgs e) 在 System.Windows.Forms.DataGridView.DataGridViewDataConnection.currencyManager_ListChanged(对象发送者,ListChangedEventArgs e) 在 System.Windows.Forms.CurrencyManager.OnListChanged(ListChangedEventArgs e) 在System.Windows.Forms.CurrencyManager.List_ListChanged(对象发送者,ListChangedEventArgs e)在System.ComponentModel.BindingList
1.OnListChanged(ListChangedEventArgs e) at System.ComponentModel.BindingList1.FireListChanged(ListChangedType类型,Int32索引)在System.ComponentModel.BindingList1.InsertItem(Int32 index, T item) at System.Collections.ObjectModel.Collection1.Add(T项)在System.ComponentModel。 BindingList1.AddNewCore() at System.ComponentModel.BindingList1.System.ComponentModel.IBindingList.AddNew() 在 System.Windows.Forms.CurrencyManager.AddNew() 在 System.Windows.Forms.DataGridView.DataGridViewDataConnection.AddNew() 在 System.Windows.Forms.DataGridView.DataGridViewDataConnection.OnNewRowNeeded ()在System.Windows.Forms.DataGridView.OnRowEnter(DataGridViewCell&dataGridViewCell,Int32 columnIndex,Int32 rowIndex,布尔canCreateNewRow,布尔validationFailureOccurred)在System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32 columnIndex,Int32 rowIndex,布尔setAnchorCellAddress,布尔validateCurrentCell ,布尔通过MouseClick)在System.Windows.Forms.DataGridView.ProcessDownKeyInternal(键keyData,布尔&移动)在System.Windows.Forms.DataGridView.ProcessEnterKey(键keyData)在System.Windows.Forms.DataGridView.ProcessDialogKey(键keyData)在System.Windows.Forms.Control.ProcessDialogKey(Keys keyData) 在 System.Windows.Forms.TextBoxBase.ProcessDialogKey(Keys keyData) 在 System.Windows.Forms.Control.PreProcessMessage(Message& msg) 在 System.Windows.Forms.Control。 PreProcessControlMessageInternal(控制目标,消息&msg)在System.Windows.Forms.Application.ThreadContext.PreTranslateMessage(MSG&msg)
这是 ex.ToString() 的结果,它不返回我的应用程序的自定义代码,仅返回内部 System.Windows.Forms 方法。
在某些客户机器上有时会出现异常,我什至无法自己重现它。
这味道不好,我的假设是当我更改 datagridview 的数据源边界时。但在这种情况下,我至少应该在异常堆栈中看到我的类,但这里什么也没有。
有什么线索可以找到根本原因或进行调试吗?
非常感谢
我在执行源代码时遇到此错误。但我在网上能找到的信息似乎不多。因此,我希望可以从这里的专业人士那里得到一些线索。
源代码
public List<string> GetData (List<long> Id)
{
List<string> data;
string sql = "select * from tblSample with(nolock) where SampleId in @sampleId";
Dapper.DynamicParameters param = new Dapper.DynamicParameters();
param.Add("@sampleId", Id);
try
{
data = this.queryrunner.QueryList(sql, param);
}
catch (Exception ex)
{
logger.Error(ex.Message, ex);
}
return data;
}
Run Code Online (Sandbox Code Playgroud)
QueryRunner 类方法
private void OpenConnection()
{
if (this.conn.State != ConnectionState.Open)
{
this.conn.Open();
}
}
public List<t> QueryList(String sql, DynamicParameters param)
{
List<t> t;
try
{
OpenConnection();
t = this.conn.Query<t>(sql, param).ToList();
}
catch (System.Data.SqlClient.SqlException ex) …Run Code Online (Sandbox Code Playgroud) 所以我用F#在winforms中做了一个简单的个人项目.我的代码曾经工作过,但现在抛出这个异常似乎没有理由.
An unhandled exception of type 'System.InvalidOperationException' occurred in FSharp.Core.dll
Additional information: The initialization of an object or value resulted in an object or value being accessed recursively before it was fully initialized.
Run Code Online (Sandbox Code Playgroud)
代码是从表单本身的构造函数调用的成员方法
do
//lots of other constructor code before this point
// render the form
form.ResumeLayout(false)
form.PerformLayout()
form.ReloadGoals
//several other members before here
member form.ReloadGoals =
let x = 10 //crashes on this line
Run Code Online (Sandbox Code Playgroud)
我抓住我正在使用的项目的模板的网站就是这个.不幸的是,我已经为此做了一些实质性的补充.
我很乐意发布更多代码,但我需要知道哪些代码完全相关,因为我不完全确定并且不想在无关代码中陷入困境.
另外,我无法在System.InvalidOperationException上找到很多文档.每次我找到它,它被用作一个例子例外的,你可以扔在你自己的,不是什么原因造成的.
我正在测试我的代码,我认为这段代码是正确的:
while True:
try:
p = Decimal(raw_input(...))
if ...condition... : break
else: raise ValueError
except ValueError:
print "Error! ..."
Run Code Online (Sandbox Code Playgroud)
但事实并非如此,因为当我输入"a"时,这就是我得到的:
File "multiple.py", line 28, in <module>
precision = Decimal(raw_input(...))
File "/usr/lib/python2.7/decimal.py", line 548, in __new__
"Invalid literal for Decimal: %r" % value)
File "/usr/lib/python2.7/decimal.py", line 3872, in _raise_error
raise error(explanation)
decimal.InvalidOperation: Invalid literal for Decimal: 'a'
Run Code Online (Sandbox Code Playgroud)
ValueError不会捕获InvalidOperation.我不希望程序停止因为这个,我希望它一直要求输入,直到它满足条件.我该怎么做?
foreach (string key in HttpContext.Current.Request.Form.AllKeys)
{
string value = HttpContext.Current.Request.Form[key];
}
Run Code Online (Sandbox Code Playgroud)
什么是上述代码的.net核心版本?好像.net核心取出AllKeys并用Keys代替它.我试图将上面的代码转换为.net核心方式,但它抛出了无效的操作异常.
HttpContext.Request.Form ='HttpContext.Request.Form'抛出类型'System.InvalidOperationException'的异常
转换代码:
foreach (string key in HttpContext.Request.Form.Keys)
{
}
Run Code Online (Sandbox Code Playgroud) 将Chrome更新到最新版本后,我不断收到此InvalidOperationException.我在c#中使用Selenium-Webdriver
System.InvalidOperationException: disconnected: unable to connect to renderer
(Session info: chrome=62.0.3202.62)
(Driver info: chromedriver=2.32.498550 (9dec58e66c31bcc53a9ce3c7226f0c1c5810906a),platform=Windows NT 6.1.7601 SP1 x86_64) (102)
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWindow.Maximize()
Run Code Online (Sandbox Code Playgroud) c# ×6
.net ×2
exception ×2
winforms ×2
.net-4.0 ×1
asp.net-core ×1
asp.net-mvc ×1
asynchronous ×1
f# ×1
httpcontext ×1
python ×1
python-2.7 ×1
sql-server ×1
throw ×1
xml ×1
xsd ×1