C# Sap Rfc 连接代码实例

2021-02-15 12:18

阅读:600

根据不同的需求,安装不同位数的 Rfc SDK

技术分享图片

 

1.构造 Sap Adress Information,且继承 IDestinationConfiguration

 public class SapAddressInfo : IDestinationConfiguration
    {
        public string SapHostIp { get; set; }
        public string SapSystemNumber { get; set; }
        public string SapUserAccount { get; set; }
        public string SapUserPassword { get; set; }
        public string SapClient { get; set; }
        public string SapLanguage { get; set; }
        public string SapPoolSize { get; set; }
        public string SapPeakConnectionsLimit { get; set; }
        public string SapConnectionIdleTimeout { get; set; }
        public string DestinationName { get; set; }

        RfcConfigParameters IDestinationConfiguration.GetParameters(string destinationName)
        {
            var parms = new RfcConfigParameters();
            if (destinationName.Equals(DestinationName))
            {
                parms.Add(RfcConfigParameters.AppServerHost, SapHostIp); //SAP主机IP
                parms.Add(RfcConfigParameters.SystemNumber, SapSystemNumber); //SAP实例
                //通信账户  用户名 oms 密码123456
                parms.Add(RfcConfigParameters.User, SapUserAccount); //用户名
                parms.Add(RfcConfigParameters.Password, SapUserPassword); //密码
                parms.Add(RfcConfigParameters.Client, SapClient); // Client
                parms.Add(RfcConfigParameters.Language, SapLanguage); //登陆语言
                parms.Add(RfcConfigParameters.PoolSize, SapPoolSize);
                parms.Add(RfcConfigParameters.PeakConnectionsLimit, SapPeakConnectionsLimit);
                parms.Add(RfcConfigParameters.ConnectionIdleTimeout, SapConnectionIdleTimeout);
            }

            return parms;
        }

        public bool ChangeEventsSupported()
        {
            return false;
        }

        event RfcDestinationManager.ConfigurationChangeHandler IDestinationConfiguration.ConfigurationChanged
        {
            // ReSharper disable once ValueParameterNotUsed
            add { }
            // ReSharper disable once ValueParameterNotUsed
            remove { }
        }
    }

  2.创建一个Sap连接的服务接口

 public interface ISapConnection
    {
        RfcDestination Destination { get; set; }
        void Open();
        void Dispose();
    }

  3.接下来我们对接口进行实现

 public class SapConnection : IDisposable, ISapConnection
    {
        private const string SapHostIpName = "SapHostIpName";
        private const string SapSystemNumberName = "SapSystemNumberName";
        private const string SapUserAccountName = "SapUserAccountName";
        private const string SapUserPasswordName = "SapUserPasswordName";
        private const string SapClientName = "SapClientName";
        private const string SapLanguageLanguage = "SapLanguageLanguage";
        private const string SapPoolSizeName = "SapPoolSizeName";
        private const string SapPeakConnectionsLimitName = "SapPeakConnectionsLimitName";
        private const string SapConnectionIdleTimeoutName = "SapConnectionIdleTimeoutName";
        private const string DestinationName = "DestinationName";

        public SapConnection(Object settingsManager)
        {
            if (settingsManager == null)
                throw new ArgumentNullException(nameof(settingsManager));
            //自行赋值
            var sapHostIp = "SapHostIpName";
            var sapSystemNumber = "SapSystemNumberName";
            var sapUserAccount = "SapUserAccountName";
            var sapUserPassword = "SapUserPasswordName";
            var sapClient = "SapClientName";
            var sapLanguage = "SapLanguageLanguage";
            var sapPoolSize = "SapPoolSizeName";
            var sapPeakConnectionsLimit = "SapPeakConnectionsLimitName";
            var sapConnectionIdleTimeout = "SapConnectionIdleTimeoutName";
            var destinationName = "DestinationName";
            SapAddressInfo = new SapAddressInfo
            {
                SapHostIp = sapHostIp,
                SapSystemNumber = sapSystemNumber,
                SapUserAccount = sapUserAccount,
                SapUserPassword = sapUserPassword,
                SapClient = sapClient,
                SapLanguage = sapLanguage,
                SapPoolSize = sapPoolSize,
                SapPeakConnectionsLimit = sapPeakConnectionsLimit,
                SapConnectionIdleTimeout = sapConnectionIdleTimeout,
                DestinationName = destinationName/*"ECQ"*/
            };
            if (!RfcDestinationManager.IsDestinationConfigurationRegistered())
            {
                RfcDestinationManager.RegisterDestinationConfiguration(SapAddressInfo);
            }
        }

        public SapAddressInfo SapAddressInfo { get; set; }
        public RfcDestination Destination { get; set; }
        public void Open()
        {
            try
            {
                Destination = RfcDestinationManager.TryGetDestination(SapAddressInfo.DestinationName);
                if (Destination == null)
                {
                    if (!RfcDestinationManager.IsDestinationConfigurationRegistered())
                    {
                        RfcDestinationManager.RegisterDestinationConfiguration(SapAddressInfo);
                    }
                    Destination = RfcDestinationManager.GetDestination(SapAddressInfo.DestinationName);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("An exception occurred when trying to open a SapConnection. Check inner exception for details.", ex);
            }
        }
        public void Dispose()
        {
            //RfcDestinationManager.UnregisterDestinationConfiguration(SapAddressInfo);
            if (SapAddressInfo != null)
            {
                try
                {
                    RfcDestinationManager.UnregisterDestinationConfiguration(SapAddressInfo);
                }
                catch (Exception ex)
                {
                    Trace.TraceError("An error occurred when trying to unregister destination configuration. Exception: {0}", ex);
                }
            }
            GC.SuppressFinalize(this);
        }
    }

  

 4.这样你就可以使用这个接口进行连接了

 using (var SapConnection = new SapConnection(_settingsManager))
                        {
                            SapConnection.Open();
                            var function = SapConnection.Destination.Repository.CreateFunction("fucntion");
                            if (param != null)
                            {
                                var inputTable = function.GetTable("123");
                                inputTable.Append();
                                inputTable.SetValue("11", param.Sign);
                                inputTable.SetValue("22", param.Option);
                                inputTable.SetValue("33", param.Low);
                                inputTable.SetValue("44", param.High);
                            }
                            function.Invoke(SapConnection.Destination);
                            var result = function.GetTable("return");
                            //解析
                        }

  如果是用Bapi 提交数据,需要加Session最稳妥,可以保证数据提交成功,查询就无所谓了。


评论


亲,登录后才可以留言!