zoukankan      html  css  js  c++  java
  • uft调用rfc接口

    RFC接口函数调用:

    以下代码是封装好的,为了提供给UFT工具调用,使用c#写成dll。

    类型项目分成两个:

    1.baseConfigModel.cs  //sap配置登录信息,属性实体类

    using System;
    using System.Collections.Generic;
    //using System.Linq;
    using System.Text;
    //using System.Threading.Tasks;
    using SAP.Middleware.Connector;
    namespace I_RFC_LIB.Model
    {
        public class baseConfigModel
        {
    
    
            public string Name { get; set; } //系统名字QH5
            public string AppServerHost { get; set; }//系统ip地址
            public string SystemNumber { get; set; } //系统实例
            public string SystemID { get; set; }//系统id
            public string User { get; set; }//用户ATPSUSER
            public string Password { get; set; }//密码
            public string Client { get; set; }//客户端号800
            public string Language { get; set; } //语言zh en
            public string IFUNC { get; set; } //接口函数名
            public RfcDestination sapConfig { get; set; } //sap登录配置
            public RfcRepository rfcRepository { get; set; } //Repository对象
            public string resultConfig { get; set; } //配置结果
        }
    }

    2.C_RFC.cs //具体实现

    using System;
    using System.Collections.Generic;
    //using System.Linq;
    using System.Text;
    using System.Reflection;
    //using System.Threading.Tasks;
    ///**************************************************************///
    using SAP.Middleware.Connector;
    ///引用合名空间SAP.Middleware.Connector需要引用sapnco3.0 dll
    ///需要framework2.0支持
    ///sapnco.dll
    ///sapnco_utils
    ///**************************************************************///
    
    
    namespace I_RFC_LIB
    {
        public class C_RFC
        {
            Model.baseConfigModel   configModel = new Model.baseConfigModel(); //实例类  登录配置属性
    
            /// <summary>
            /// 调用rfc接口函数
            /// </summary>
            /// <param name="headstr">包含配置信息的头</param>
            /// <param name="itemstr">接口需要参数</param>
            /// <returns></returns>
            public string invoke(string headstr,string itemstr)
            {
                string[] headArr = headstr.Split(new string[] { "@__@" }, StringSplitOptions.RemoveEmptyEntries);
                string[] itemArr = itemstr.Split(new string[] { "@__@" }, StringSplitOptions.RemoveEmptyEntries);
    
                #region //head属性赋值
                //rfc config
    
                var proValue = configModel.GetType(); //获取属性实例
    
                for (var i = 0; i < headArr.Length; i++)
                {
                    var tmpVal = headArr[i].Split(new string[] { "@_@" }, StringSplitOptions.None);
                    foreach (PropertyInfo py in proValue.GetProperties())
                    {
                        if (py.Name.ToUpper() == tmpVal[0].ToUpper())
                        {
                            proValue.GetProperty(py.Name).SetValue(configModel, tmpVal[1], null);
                        }
                    }
                }
    
                #endregion
    
                if (rfc_config()) //执行配置,获取对象
                {
                    IRfcFunction rfc_invoke = configModel.rfcRepository.CreateFunction(configModel.IFUNC); //调用"ZFUC_APWZ_AP_PREVIEW"
                    rfc_invoke.Invoke(configModel.sapConfig); //执行函数
    
                    #region //item传参
                    for (var i = 0; i < itemArr.Length; i++)
                    {
                        var tmpitVal = itemArr[i].Split(new string[] { "@_@" }, StringSplitOptions.RemoveEmptyEntries);
                        rfc_invoke.SetValue(tmpitVal[0], tmpitVal[1]); //设置参数 (参数名,参数值)
                    }
                    #endregion
    
                    //rfc_invoke.SetValue("PSPID", ""); //设置参数 项目编号
                    IRfcTable rfcTable = rfc_invoke.GetTable("RETURN"); //获取内表
                    string message = rfcTable.GetValue("MESSAGE").ToString();
    
                    return message;
                }
                else
                {
                    return "配置登录时出现问题,请查检配置!"+configModel.resultConfig;
                }
            }
    
    
            /// <summary>
            /// 登录配置
            /// </summary>
            /// <returns>bool</returns>
            public bool rfc_config()
            {
                try
                {
                    //rfc配置
                    RfcConfigParameters argsP = new RfcConfigParameters();
                    argsP.Add(RfcConfigParameters.Name, configModel.Name);
                    argsP.Add(RfcConfigParameters.AppServerHost, configModel.AppServerHost);
                    argsP.Add(RfcConfigParameters.SystemNumber, configModel.SystemNumber);
                    argsP.Add(RfcConfigParameters.SystemID, configModel.SystemID);
                    argsP.Add(RfcConfigParameters.User, configModel.User);
                    argsP.Add(RfcConfigParameters.Password, configModel.Password);
                    argsP.Add(RfcConfigParameters.Client, configModel.Client);
                    argsP.Add(RfcConfigParameters.Language, configModel.Language);
                    argsP.Add(RfcConfigParameters.PoolSize, "5");
                    argsP.Add(RfcConfigParameters.MaxPoolSize, "10");
                    argsP.Add(RfcConfigParameters.IdleTimeout, "60");
    
                    //获取rfc配置
                    configModel.sapConfig = RfcDestinationManager.GetDestination(argsP);
                    configModel.rfcRepository = configModel.sapConfig.Repository;
                }
                catch (RfcBaseException ex)
                {
                    configModel.resultConfig = ex.Message;
                    return false;
                }
                return true;
            }
    
        }
    }

    3.uft调用

    需要设置,程序集com可见

     4、uft调用方法:

    set rfc = dotnetfactory.CreateInstance("I_RFC_LIB.C_RFC","d:\rfc.dll")

    5、c#类库调用结果。接口返回“ATPSUSER不是该步骤允许的审批人”

  • 相关阅读:
    常用排序算法(快速排序,冒泡排序,最大公约数,Fibonacci )【原创】
    Sql之left_join、right_join、inner_join的解释
    Effective C# 学习笔记(九) 在你的API中避免使用类型转换运算
    Effective C# 学习笔记(十) 用可选参数尽量减少方法重载
    NHibernate学习笔记(5)—正向与反向生成
    NHibernate学习(7)—对于实现机理的猜测
    Effective C# 学习笔记(一) 用属性替代公有变量
    Effective C# 学习笔记(十一)尽量缩减函数体的大小,提高运行效率
    我的学习之路
    NHibernate学习笔记(4)—使用存储过程
  • 原文地址:https://www.cnblogs.com/yhleng/p/7262835.html
Copyright © 2011-2022 走看看