我正在试图找出并修复崩溃(由InvalidCastException引起:无法将类型为'System.Object []'的对象转换为类型'System.Security.IPermission'),这种情况在我们的网站上不时发生执行以下代码时的客户端PC:
public List<Department> GetDepartmentList()
{
return DB.Departments.Where(d => d.Gender == SelectedGender && d.IsActive).ToList();
}
为什么会发生这种异常? 如何解决?
我不能简单地将它放入try catch块,因为数据库的结果是必要的。
代码在主UI线程上执行,不会从其他线程访问属性/方法。
此处的Gender是Department实体类中的EntityRef属性:
[Column(Name = "FK_Gender")]
public int? FK_Gender_Id;
private EntityRef<Gender> gender;
[Association(Name = "FK_Department_Gender", IsForeignKey=true, Storage="gender", ThisKey = "FK_Gender_Id")]
public Gender Gender
{
get { return gender.Entity; }
set { gender.Entity = value; }
}
从SelectedGender属性的setter调用GetDepartmentList,当用户通过绑定的ICommand SelectGenderCommand单击UserControl上的Male或Female按钮时分配:
private RelayCommand selectGenderCommand;
public ICommand SelectGenderCommand
{
get
{
return selectGenderCommand ?? (selectGenderCommand = new RelayCommand(param =>
{
var genderName = param as string;
SelectedGender = DB.Genders.Single(g => g.Description == genderName);
...
private Gender selectedGender;
public Gender SelectedGender
{
get { return selectedGender; }
set
{
selectedGender = value;
DepartmentList = GetDepartmentList();
NotifyPropertyChanged(() => SelectedGender);
...
}
}
DB是一个DataContext派生类实例,它在GetDepartmentList方法所在的ViewModel的构造函数中初始化:
public InventoryItemSelectorViewModel(...
{
DB = new Database();
DataLoadOptions dlo = new DataLoadOptions();
dlo.LoadWith<Department>(d => d.Gender);
DB.LoadOptions = dlo;
...
异常堆栈跟踪:
System.InvalidCastException: Unable to cast object of type 'System.Object[]' to type 'System.Security.IPermission'.
at System.RuntimeMethodHandle.PerformSecurityCheck(Object obj, RuntimeMethodHandleInternal method, RuntimeType parent, UInt32 invocationFlags)
at System.RuntimeMethodHandle.PerformSecurityCheck(Object obj, IRuntimeMethodInfo method, RuntimeType parent, UInt32 invocationFlags)
at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture)
at System.Data.Linq.CommonDataServices.GetDeferredSourceFactory(MetaDataMember member)
at System.Data.Linq.SqlClient.ObjectReaderCompiler.ObjectReaderBase`1.GetNestedLinkSource[T](Int32 iGlobalLink, Int32 iLocalFactory, Object instance)
at Read_Department(ObjectMaterializer`1 )
at System.Data.Linq.SqlClient.ObjectReaderCompiler.ObjectReader`2.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at GPFC.ROIPOS.ViewModels.InventoryItemSelectorViewModel.GetDepartmentList() in e:\Projects\POS - Develop\ROIPOS\ROIPOS\ViewModels\InventoryItemSelectorViewModel.cs:line 1173
at GPFC.ROIPOS.ViewModels.InventoryItemSelectorViewModel.set_SelectedGender(Gender value) in e:\Projects\POS - Develop\ROIPOS\ROIPOS\ViewModels\InventoryItemSelectorViewModel.cs:line 202
at GPFC.ROIPOS.ViewModels.InventoryItemSelectorViewModel.<get_SelectGenderCommand>b__12(Object param) in e:\Projects\POS - Develop\ROIPOS\ROIPOS\ViewModels\InventoryItemSelectorViewModel.cs:line 978
at GPFC.ROIPOS.MVVM.RelayCommand.Execute(Object parameter) in e:\Projects\POS - Develop\ROIPOS\ROIPOS\MVVM\RelayCommand.cs:line 168
at MS.Internal.Commands.CommandHelpers.CriticalExecuteCommandSource(ICommandSource commandSource, Boolean userInitiated)
at System.Windows.Controls.Primitives.ButtonBase.OnClick()
at System.Windows.Controls.Primitives.ToggleButton.OnClick()
at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
at System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e)
at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
at System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e)
at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
at System.Windows.Input.InputManager.ProcessStagingArea()
at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
可以与问题相关的另一个细节。我们的应用程序在几台客户端PC上运行,这些客户端PC都与安装在其中一台客户端PC上的一个本地网络数据库配合使用。