Search code examples
c#event-handlingdelegatesautofacsap-dotnet-connector

Implementing Delegate In .NET SAP Connector


Quite a specific question and probably basic on some levels. I have a gap in my knowledge of delegates and event handling in general so I'm not surprised that I am a bit stuck. But using Autofac is complicating matters in my understanding even more. I'll try and explain below.

I am connecting to SAP using the .NET V3 SAP connectors and implementing what's known as an RFC Server. As part of that the good people at SAP have exposed an RfcServerErrorEventHandler:

public delegate void RfcServerErrorEventHandler(object server, RfcServerErrorEventArgs errorEventData);

I start my server in a class library in which I have what we'll call a server manager. It is to start the server, stop it and everything in between. What I am stuck with is how on earth I can go about using the above delegate in my server manager class, which is injected using AutoFac to my main program with the implementation as below:

 public IServerManagerService _ServerManager;
 public ApplicationLogic(IServerManagerService serverManager)
 {
        _ServerManager = serverManager;
 }

 _ServerManager.StartServer(ServerName);

The above goes to the server manager class and runs the StartServer method obviously. I have attempted to try and register the event (this is where I am hazy) as below in the server manager class:

 public void ErrorHandler(object sender, RfcServerErrorEventHandler e)
    {
        
        throw new Exception("The method or operation is not implemented.");
    }

But can I for the life of me figure out how to either get the delegate hooked up to this or figure out how to do it via autofac. I have had some attempts but I just cant find any examples of this anywhere online. It seems that SAP .NET Connectors are quite a niche thing. The connector documentation provides only very basic code samples also.

I think my problem as mentioned is just a lack of basic event handling techniques possibly or esp when using with DI. I have done a fair amount of reading before posting but just not getting it.

Thanks in advance


Solution

  • This should be explained in any good C# book: see the topics "delegate" and "event". Basically, you can register an event handler at a certain event simply by using the += operator. ("=" should also work, if you are the only one using the RfcServer, but with "+=" you can have more than one subscriber for the event.)

    So for example in your case

    myServer.RfcServerError += new RfcServer.RfcServerErrorEventHandler(ErrorHandler);
    

    But note, that your defintion of the event handler (method ErrorHandler) is wrong! The second argument should be of type RfcServerErrorEventArgs, not RfcServerErrorEventHandler!

    public void ErrorHandler(object sender, RfcServerErrorEventArgs e)
    {
        // Do something with "e" here. Throwing an exception is probably
        // not a good idea...
        throw new Exception("The method or operation is not implemented.");
    }