huysentruitw / SapNwRfc

SAP NetWeaver RFC library for .NET 5, .NET Core and .NET Framework
MIT License
148 stars 43 forks source link

SapServer Error Event Handler - unable to use #98

Closed tom-j-irvine closed 2 months ago

tom-j-irvine commented 2 months ago

I have been successfully using the SapServer functionality for quite a while, but haven't been able to handle errors in the connection. I'm handling the StateChange event which works as expected when the server is explicitly started, stopped, etc. However, if something breaks the connection on the SAP side (including forcing the disconnection in SMGW), the StateChange event doesn't fire. It does appear this is correct, but any attempt to handle the Error event immediately aborts my application.

I can add the Error handler at startup without issue. However, as soon as it fires, my application crashes and I'm unable to trap the exception. The exception looks something like this:

An unhandled exception of type 'System.NullReferenceException' occurred in Unknown Module.: 'Object reference not set to an instance of an object.'
Stack trace:
 ><Cannot evaluate the exception stack trace>

System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=<Cannot evaluate the exception source>
  StackTrace:
<Cannot evaluate the exception stack trace>

In the debugger, the offending thread shows Thread - [Managed to Native Transition]

So, it would seem this issue is off in un-managed code that I can't handle myself. I looked pretty closely at the RFC SDK documentation vs. the RfcInterop and I don't see any discrepancies.

These are the steps I can use to cause the issue:

Any input or help on how this event can be handled would be greatly appreciated.

campersau commented 2 months ago

Are you using this implementation https://github.com/huysentruitw/SapNwRfc/pull/53/commits/7fa7175ea4e6c3b7bd9246b1d1d90faaa6090db5 ?

tom-j-irvine commented 2 months ago

No, that doesn't look exactly like what I have. I can try adding those changes see if it changes things.

campersau commented 2 months ago

I added that later in https://github.com/huysentruitw/SapNwRfc/pull/53

tom-j-irvine commented 2 months ago

That is what I needed. Error event is working now and will be very helpful. Thanks!