mercredi 10 février 2021

SAP.net Connector using reflection C#

We are using SAP.net connector in C# windows service application, as per license documentation on SAP page, the libraries are not redistributable(sapnco.dll, sapnco_utils.dll), the customer needs to have its own licensed copy of the library, and we are using DirectoryCatalog(MefFunctions.GetPlugInDir()) to load all assemblies from plugin folder so over without physical copy of dll the windows service application does not work and it throws an error that the sap dlls are missing as we have added dll's as part of references, could you please help to overcome this problem, one thing I thought off is to use reflection instead of directly using the dll references in project, not sure how to do with references like inheriting the sap classes in our custom class, could you please help.

 class RfcConfig : IDestinationConfiguration   // How to inherit IDestinationConfiguration using reflection and how to handle events using reflection
{
 private Dictionary<string, RfcConfigParameters> availableDestinations = new Dictionary<string, RfcConfigParameters>();
        public bool ChangeEventsSupported()
        {
            return true;
        }

        RfcDestinationManager.ConfigurationChangeHandler changeHandler;

        public event RfcDestinationManager.ConfigurationChangeHandler ConfigurationChanged
        {
            add { changeHandler += value; }
            remove { changeHandler -= value; }
        }
 private void AddOrEditDestination(RfcConfigParameters parameters)
        {
            string name = parameters[RfcConfigParameters.Name];
            if (availableDestinations.ContainsKey(name))
            {
                // Fire a change event
                if (changeHandler != null)
                {
                    RfcConfigurationEventArgs eventArgs = new RfcConfigurationEventArgs(RfcConfigParameters.EventType.CHANGED, parameters);
                    changeHandler(name, eventArgs);
                }
            }

            // Replace the current parameters of an existing destination or add a new one
            availableDestinations[name] = parameters;
        }
public RfcConfigParameters GetParameters(string destinationName)
        {
            RfcConfigParameters foundDestination;
            availableDestinations.TryGetValue(destinationName, out foundDestination);
            return foundDestination;

        }
  public string Disconnect( string name = null)
        {
            try
            {
                if (name == null)
                {
                    changeHandler(name, new RfcConfigurationEventArgs(RfcConfigParameters.EventType.DELETED));
                }
                foreach (var availableDestination in availableDestinations)
                {
                    changeHandler(availableDestination.Key, new RfcConfigurationEventArgs(RfcConfigParameters.EventType.DELETED));
                }

            }
            catch(Exception ex)
            {
                var exMsg = $"Error while closing the connection for SAP adapter : [{name}] \n Error : {ex.Message}";
                return exMsg;
            }
            return $"Connection for SAP adapter: {name} closed.";
        }

private void AddOrEditDestination(RfcConfigParameters parameters)
        {
            string name = parameters[RfcConfigParameters.Name];
            if (availableDestinations.ContainsKey(name))
            {
                // Fire a change event
                if (changeHandler != null)
                {
                    RfcConfigurationEventArgs eventArgs = new RfcConfigurationEventArgs(RfcConfigParameters.EventType.CHANGED, parameters);
                    changeHandler(name, eventArgs);
                }
            }

            // Replace the current parameters of an existing destination or add a new one
            availableDestinations[name] = parameters;
        }

}




Aucun commentaire:

Enregistrer un commentaire