Strange problem. I have a class, and if I add any methods from it (including private methods) it causes an exception in a different class.
The exception is:
An exception of type 'System.MemberAccessException' occurred in mscorlib.dll
but was not handled in user code
Additional information: Type initializer was not callable.
The line that causes the exception is:
compositionService.DefaultCompositionService.SatisfyImportsOnce(this);
(compositionService is of type Microsoft.VisualStudio.ComponentModelHost.ComponentModel and this is a class that implements the Microsoft.VisualStudio.Shell.TableManager.ITableDataSource interface.)
This occurs even if I add a method such as:
private void DoNothing()
{
}
If I remove a class, the exception happens in the same line, but is this exception instead:
An exception of type 'System.InvalidCastException' occurred in mscorlib.dll
but was not handled in user code
Additional information: Unable to cast object of type
'System.Reflection.RuntimeMethodInfo' to type
'System.Reflection.ConstructorInfo'.
Both the added and removed methods are private.
The code is from https://github.com/vinaykapadia/sarif-sdk in the Sarif.Viewer.VisualStudio project, the file I'm adding the DoNothing() method to is CodeAnalysisResultManager.cs, and the exception is caused in ErrorList\SarifTableDataSouce.cs on line 36.
Any thoughts?
Related
In the application I"m working with, there is some communication with the remote service using WCF (basic http binding, no fancy stuff). Since the client is lightweigh, the details of the server are irrelevant, you may assume that there is just a method that always return true (like ping or something).
The proxy is generated using a Task option, the new client instance is created each time the operation is called. Something like this could be spinning inside the timer:
void Foo()
{
var client = new PingServiceClient();
try
{
bool result = client.PingAsync().GetAwaiter().GetResult();
}
catch
{
//log something
}
finally
{
client.Abort();
}
}
My question is, how should I correctly handle the cases when the network is down? Because the behavior is different. I either get an application crashing (I assume on a task finalizer, which is for some reason not handled neither in AppDomain.CurrentDomain.UnhandledException nor in TaskScheduler.UnobservedTaskException), or sometimes it just silently outputs tons of error messages, but not crashing anything. Messages like these:
Exception thrown: 'System.Net.Sockets.SocketException' in System.dll
Exception thrown: 'System.Net.Sockets.SocketException' in System.dll
Exception thrown: 'System.Net.Sockets.SocketException' in System.dll
Exception thrown: 'System.Net.WebException' in System.dll
Exception thrown: 'System.ServiceModel.EndpointNotFoundException' in System.ServiceModel.dll
Exception thrown: 'System.ServiceModel.EndpointNotFoundException' in System.ServiceModel.Internals.dll
Exception thrown: 'System.ServiceModel.EndpointNotFoundException' in System.ServiceModel.Internals.dll
Exception thrown: 'System.ServiceModel.EndpointNotFoundException' in System.ServiceModel.Internals.dll
Exception thrown: 'System.ServiceModel.EndpointNotFoundException' in System.ServiceModel.Internals.dll
Exception thrown: 'System.Net.Sockets.SocketException' in System.dll
Exception thrown: 'System.AggregateException' in mscorlib.dll
I'm struggling to find a graceful way of handling these, so if anybody has some knowledge regarding this please share the approach.
Thanks in advance.
UPD:
I have tried to re-create the proxy with Begin/End pair and override the end method implementation in a partial class:
public partial class PingServiceClient : IPingServiceClient, PingService
{
public Task<bool> PingSync()
{
return Task.Factory.FromAsync(BeginPing(null, null), HandledEndPing);
}
private bool HandledEndPing(System.IAsyncResult result)
{
var res = false;
try
{
res = EndPing(result);
}
catch (Exception e)
{
;
}
return res;
}
}
Still the same barrage of messages in the output as before (the catch is working, though).
I'm trying to invoke an instance using constructor.Invoke and also passing some parameters in it, but I'm getting
System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation.'
Any inputs would be helpful
Thanks
TargetInvocationException basically means that the member could be invoked successfully and the exception occurred in the invoked member itself. To differentiate it from other issues (eg. wrong instance, invalid number or type of parameters, etc.) the exception thrown by the invoked member is wrapped into a TargetInvocationException. Just look at its InnerException to see what happened.
If you want to 'unwrap' such exceptions as if you called the constructor without reflection you can do something like this:
try
{
return myConstructorInfo.Invoke(parameters);
}
catch (TargetInvocationException e)
{
// we could just throw e.InnerException but that would corrupt the
// original stack trace, showing this line as the source of the exception
ExceptionDispatchInfo.Capture(e.InnerException).Throw();
throw; // unreachable, just to satisfy the compiler if there is no return later
}
I made a simple game in Unity3D and created a Windows Phone build. However, when I try to run the game, it crashes immediately with TypeInitializationException.
The crash happens in the generated MainPage.xaml.cs file, in the constructor:
// Constructor
public MainPage()
{
var bridge = new UnityBridge();
UnityApp.SetBridge(bridge);
InitializeComponent();
bridge.Control = DrawingSurfaceBackground; // <--- This line = crash
}
The exact error goes like this:
TypeInitializationException was unhandled by user code
An exception of type 'System.TypeInitializationException' occurred in WinRTBridge.DLL but was not handled in user code
Additional information: The type initializer for 'WinRTBridge.WinRTBridge' threw an exception.
Why does this happen? How do I fix this?
When a class initializer fails to initialize a type, a TypeInitializationException is created and passed a reference to the exception thrown by the type's class initializer. The InnerException property of TypeInitializationException holds the underlying exception. Try to log this property and see what actually cause a exception. Other way around it would be to wrap it in a try{}catch(TypeInitializationException) block .
I'm calling a delegate (dynamically configurable service) using:
public void CallService (Delegate service, IContext ctx)
{
var serviceArgs = CreateServiceArguments(service, ctx);
service.DynamicInvoke(serviceArgs);
}
At this point I want to catch all exceptions that occurred in the called service method, however, I do not want to catch any exception that occurred due to the DynamicInvoke call. E.g.:
service delegate throws DomainException -> catch the exception
DynamicInvoke() throws MemberAccessException because the delegate is a private method -> do not catch the exception, let it bubble up
I hope it is clear what I'm asking. How to decide whether a catched exception originates from the DynamicInvoke call itself or from the underlying delegate.
Oh yeah, and: I cannot use the exception type to decide! It is completely possible that the service itself throws a MemberAccessException as well, because it could do some delegate stuff itself...
Oh yeah, and: I cannot use the exception type to decide! It is completely possible that the service itself throws a MemberAccessException as well, because it could do some delegate stuff itself...
Yes, you can use the exception type to decide. As mentioned in the documentation for Delegate.DynamicInvoke, if the method being called throws an exception (any exception), it will be wrapped in a TargetInvocationException. That is the exception you can catch, and you can then look at its InnerException property to know whether it's an exception you can deal with.
I have a few lines of code that have worked fine for months, and now suddenly they do not work and I get a very strange error. Here is the function in question:
public void OnCommandsRequested(SettingsPane settingsPane, SettingsPaneCommandsRequestedEventArgs eventArgs) {
UICommandInvokedHandler handler = new UICommandInvokedHandler(OnSettingsCommand);
SettingsCommand appSettings = new SettingsCommand("appSettings", "アプリ内設定", handler);
eventArgs.Request.ApplicationCommands.Add(appSettings);
}
Naturally, this gets called in response to the SettingsPane.GetForCurrentView().CommandsRequested event. The error happens on the second line and is as follows:
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
Additional information: Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).
If I continue the app, then another exception comes:
A first chance exception of type 'System.InvalidCastException' occurred in mscorlib.dll
Additional information: Object in an IPropertyValue is of type 'String', which cannot be converted to a 'Guid'.
What is going on here? As you can see I'm not using any GUID values anywhere.
Although the documentation lists no restrictions, and all samples use strings, the first parameter is listed as taking Object - it's possible this now is simply set to require a giud.
Interestingly, it looks like this has been reported to Microsoft as a bug in the OS. No idea what the resolution is going to be.