C# .NET 如何调用 SAP RFC 接口

这篇具有很好参考价值的文章主要介绍了C# .NET 如何调用 SAP RFC 接口。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1.分析传参结构
SAP 传参格式对应 .NET 参数格式

SAP 参数 .NET 参数 参数类型
import(导入)——关联类型为数据元素 Param 单个变量参数
import(导出)——关联类型为结构体 Struct 结构体
table Table

下面是 SAP 对应参数类型:
C# .NET 如何调用 SAP RFC 接口,ABAP,.net,c#,.net,SAP,ABAP,RFC
C# .NET 如何调用 SAP RFC 接口,ABAP,.net,c#,.net,SAP,ABAP,RFC
C# .NET 如何调用 SAP RFC 接口,ABAP,.net,c#,.net,SAP,ABAP,RFC

2.web.config 配置
配置文件需要客户端名和连接地址和账户密码

  <appSettings>
    <add key="SAPINSName" value="OND"/>    //客户端名字
  </appSettings>
 <SAP.Middleware.Connector>
    <ClientSettings>
      <DestinationConfiguration>
        <destinations>
         <add NAME="OND" USER="" PASSWD="******" CLIENT="300" LANG="ZH" ASHOST="10.10.xx.xx" SYSNR="00" MAX_POOL_SIZE="10" IDLE_TIMEOUT="10"/>
        </destinations>
      </DestinationConfiguration>
    </ClientSettings>
  </SAP.Middleware.Connector>

3.调用示例:
调用 SAP 接口类型为输入输出表的接口:

var SAP = ConfigHelper.AppSettings("SAPINSName");
Test.SapDBBase.SAPBaseDAL sd = new Test.SapDBBase.SAPBaseDAL(SAP); //基本设置
DataTable tb = new DataTable();
tb.Columns.Add("WERKS");
tb.Columns.Add("MATNR");
DataRow rowsap = tb.NewRow();
rowsap["WERKS"] = WERK;
rowsap["MATNR"] = PN;
tb.Rows.Add(rowsap);
try
{
	//调用 SAP RFC 接口,传参是表结构,返回表数据和结构体
    DataTable returntd = sd.WriteDataTable("ZFM_EXPORT", "IT_IN", tb, "IT_OUT");
}
catch (Exception e)
{
    Type = "E";
    Msg = e.Message;
}      

4.调用方法部分代码文章来源地址https://www.toymoban.com/news/detail-547734.html

  • 调用准备
using System;
using System.Collections;
using System.Data;
using System.Linq;
using ONET.ServerCommLib;
using SAP.Middleware.Connector;

		public SAPBaseDAL(string SapConfigNodeName = "")
		{
			if (!string.IsNullOrEmpty(SapConfigNodeName))
			{
				SapRFCBase.SapConfigNodeName = SapConfigNodeName;
			}
		}


try
{
	RfcDestination rfcDestination = SapRFCBase.RegisterDestination();
	RfcRepository repository = rfcDestination.Repository;
	IRfcFunction rfcFunction = repository.CreateFunction(functionName);
	...//不同入参类型
	rfcFunction.Invoke(rfcDestination);
	...//不同出参类型
}
catch (Exception ex)
{
	this.writeExceptionLog(functionName, ex);
}
  • 出入参构造
//入参结构体
rfcFunction = this.GetIRfcStructeFromDataTable(rfcFunction, writeSapStructName, writeStructTable);
//入参表
rfcFunction = this.GetIRfcTableFromDataTable(rfcFunction, writeSapTableName, writeTable);
rfcFunction = writeDataSet.Tables.Cast<DataTable>().Aggregate(rfcFunction, (IRfcFunction current, DataTable table) => this.GetIRfcTableFromDataTable(current, table.TableName, table));//多表
//入参单个变量
                if (htParam != null && htParam.Count > 0)
                {
                    foreach (object obj in htParam.Keys)
                    {
                        string text = (string)obj;
                        rfcFunction.SetValue(text, htParam[text].ToString());
                    }
                }
//出参结构
方式1:
				if (!string.IsNullOrEmpty(returnStructName))
				{
					IRfcStructure structure = rfcFunction.GetStructure(returnStructName);
					return new SAPRetStruct
					{
						Type = structure.GetString("TYPE"),
						Msg = structure.GetString("MESSAGE")
					};
				}
				return default(SAPRetStruct);
方式2:
                //ref Hashtable hstb 返回hashtable
				IRfcStructure structure = rfcFunction.GetStructure(returnStructName);
				if (hstb != null && hstb.Count > 0)
				{
					ArrayList arrayList = new ArrayList(hstb.Keys);
					for (int i = 0; i < arrayList.Count; i++)
					{
						hstb[arrayList[i]] = structure.GetString(arrayList[i].ToString());
					}
				}
//出参单个变量参数  
                Hashtable hashtable = new Hashtable();
                if (outParam != null && outParam.Count > 0)
                {
                    foreach (object obj2 in outParam.Keys)
                    {
                        string text2 = (string)obj2;
                        hashtable.Add(text2, rfcFunction.GetString(text2));
                    }
                    if (hashtable.Count > 0)
                    {
                        outParam.Clear();
                        foreach (object obj3 in hashtable.Keys)
                        {
                            string text2 = (string)obj3;
                            outParam.Add(text2, hashtable[text2]);
                        }
                    }
                }    
//出参表
				IRfcTable table = rfcFunction.GetTable(returnTableName);//出参表名
				dtOut = this.GetDataTableFromRFCTable(table); //返回的表         
  • 实例1:入参:变量+表 出参:变量+结构体
public SAPRetStruct WriteParamAndTableToSapReturnParamAndRetStruct(string functionName, Hashtable htParam, string writeSapTableName, DataTable writeTable, ref Hashtable outParam, string returnStructName)
		{
			try
			{
				RfcDestination rfcDestination = SapRFCBase.RegisterDestination();
				RfcRepository repository = rfcDestination.Repository;
				IRfcFunction rfcFunction = repository.CreateFunction(functionName);
				rfcFunction = this.GetIRfcTableFromDataTable(rfcFunction, writeSapTableName, writeTable);
				if (htParam != null && htParam.Count > 0)
				{
					foreach (object obj in htParam.Keys)
					{
						string text = (string)obj;
						rfcFunction.SetValue(text, htParam[text].ToString());
					}
				}
				rfcFunction.Invoke(rfcDestination);
				Hashtable hashtable = new Hashtable();
				if (outParam != null && outParam.Count > 0)
				{
					foreach (object obj2 in outParam.Keys)
					{
						string text2 = (string)obj2;
						hashtable.Add(text2, rfcFunction.GetString(text2));
					}
					if (hashtable.Count > 0)
					{
						outParam.Clear();
						foreach (object obj3 in hashtable.Keys)
						{
							string text2 = (string)obj3;
							outParam.Add(text2, hashtable[text2]);
						}
					}
				}
				IRfcStructure structure = rfcFunction.GetStructure(returnStructName);
				return new SAPRetStruct
				{
					Type = structure.GetString("TYPE"),
					Msg = structure.GetString("MESSAGE")
				};
			}
			catch (Exception ex)
			{
				this.writeExceptionLog(functionName, ex);
			}
			return default(SAPRetStruct);
		}
  • 实例2:入参:多个表 出参:变量+结构体
		public SAPRetStruct WriteTablesToSapReturnParamAndRetStruct(string functionName, DataSet writeDataSet, ref Hashtable outParam, string returnStructName)
		{
			try
			{
				RfcDestination rfcDestination = SapRFCBase.RegisterDestination();
				RfcRepository repository = rfcDestination.Repository;
				IRfcFunction rfcFunction = repository.CreateFunction(functionName);
				rfcFunction = writeDataSet.Tables.Cast<DataTable>().Aggregate(rfcFunction, (IRfcFunction current, DataTable table) => this.GetIRfcTableFromDataTable(current, table.TableName, table));
				rfcFunction.Invoke(rfcDestination);
				Hashtable hashtable = new Hashtable();
				if (outParam != null && outParam.Count > 0)
				{
					foreach (object obj in outParam.Keys)
					{
						string text = (string)obj;
						hashtable.Add(text, rfcFunction.GetString(text));
					}
					if (hashtable.Count > 0)
					{
						outParam.Clear();
						foreach (object obj2 in hashtable.Keys)
						{
							string text = (string)obj2;
							outParam.Add(text, hashtable[text]);
						}
					}
				}
				IRfcStructure structure = rfcFunction.GetStructure(returnStructName);
				return new SAPRetStruct
				{
					Type = structure.GetString("TYPE"),
					Msg = structure.GetString("MESSAGE")
				};
			}
			catch (Exception ex)
			{
				this.writeExceptionLog(functionName, ex);
			}
			return default(SAPRetStruct);
		}
  • 实例3:入参:变量 出参:表
		public DataTable GetTable(string functionName, Hashtable htParam, string returnTableName)
		{
			try
			{
				RfcDestination rfcDestination = SapRFCBase.RegisterDestination();
				RfcRepository repository = rfcDestination.Repository;
				IRfcFunction rfcFunction = repository.CreateFunction(functionName);
				if (htParam != null && htParam.Count > 0)
				{
					foreach (object obj in htParam.Keys)
					{
						string text = (string)obj;
						rfcFunction.SetValue(text, htParam[text].ToString());
					}
				}
				rfcFunction.Invoke(rfcDestination);
				IRfcTable table = rfcFunction.GetTable(returnTableName);
				return this.GetDataTableFromRFCTable(table);
			}
			catch (Exception ex)
			{
				this.writeExceptionLog(functionName, ex);
			}
			return null;
		}

到了这里,关于C# .NET 如何调用 SAP RFC 接口的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • C# 调用SAP WebService接口(SoapUI Postman)

    SAP wsdl地址发放在浏览器中是需要输入用户名密码进行认证的 将wsdl地址放到SoapUI进行解析 输入用户名密码 左边就是请求的XML格式,右边是接口返回内容,点击运行就可以看到结果 Request1的地址就是我们需要调用的地址(这里提供的域名和wsdl 的域名不一致,需要换成wsdl的域

    2024年02月09日
    浏览(36)
  • SAP ABAP调用Http/Https方式实例

    注意: 1.url必须带\\\"http://\\\"或者“https://”。 2. http_client-propertytype_redirect = http_client-co_disabled .这一行是设置是否允许重定向,是为“co_enabled”,否为“co_disabled”。 3. 可能出现的错误 404 Hostname Unknow,是因为服务器未配置该url的dns 4. 可能出现的错误 401 refused,可能是被防火墙

    2024年02月07日
    浏览(43)
  • 【SAP ABAP】SAP Webservice & RESTful 接口服务发布教程

    关于 WebService 概念,这篇文章讲解的非常全面,可以移步阅读《SAP Web service》。 本想通过 RFC 来发布 ODATA 服务,奈何当前 SAP ECC 版本过低不支持,只好采用其他方式来发布服务,于是就尝试了下面这两种方法。 SE37,创建以下测试用 RFC 测试执行 RFC,得到的数据结果如下图:

    2024年02月02日
    浏览(35)
  • SAP ABAP 使用SICF发布HTTP API接口

    一、SE24创建类: Z_HCX_HTTP 1、创建类: 2、切换到接口(interface)页签,输入IF_HTTP_EXTENSION ,回车。切换到方法(method)页签,双击IF_HTTP_EXTENSION~HANDLE_REQUEST进入代码编辑界面。   3、在 IF_HTTP_EXTENSION~HANDLE_REQUEST 方法中编写代码: (注:文末附另一种写法) 二、使用事务码 

    2024年02月11日
    浏览(32)
  • C#开发DLL,CAPL调用(CAPL>> .NET DLL)

    ret为dll里函数返回的值。 在visual studio中建立。

    2024年02月08日
    浏览(36)
  • .net中最简单的http请求调用(比如调用chatgpt的openAI接口)

    支持.Net Core(2.0及以上)/.Net Framework(4.5及以上),可以部署在Docker, Windows, Linux, Mac。 http请求调用是开发中经常会用到的功能,因为,很多第三方功能接口往往是通过http地址的形式提供的,比如:ChatGpt、OpenAI、短信服务、在线翻译、地图服务、语音智能、等…   .net中调用http请

    2024年02月02日
    浏览(72)
  • 解决远程调用三方接口:javax.net.ssl.SSLHandshakeException报错

    最近在对接腾讯会议API接口,在鉴权完成后开始调用对方的接口,在此过程中出现调用报错:javax.net.ssl.SSLHandshakeException。 当你在进行https请求时,JDK中不存在三方服务的信任证书,导致出现错误javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX路径构建失败。

    2024年02月13日
    浏览(40)
  • C# .NET调用OpenAI及微软语音服务实现语音输入及输出

    目前C#调用Open AI有很多的开源库,本文使用的nuget包如下: 都有开源库,所以实现起来就比较简单了,主要难度可能还是注册账号,有能力的可以自己试试,在这儿贴出部分调用的主要代码,项目已开源,Github搜索JamesBaiJun/OpenAI-Azure,记得点个Star。 调用ChatGPT使用Nuget的Open

    2024年02月02日
    浏览(29)
  • 每个.NET开发都应掌握的C#接口知识点

    作为.NET开发者,接口是C#必须掌握的知识点,接口是C#中实现多态和组件间互操作性的关键机制之一。 接口是一种抽象的类型,它定义了一组成员(方法、属性、事件等)的规范,但没有实现代码。类可以实现一个或多个接口,以表明它们提供了特定的功能。 以下是每个.N

    2024年02月13日
    浏览(35)
  • C# .NET ADO.NET介绍和如何使用

    .NET Framework 4.7.2 Visual Studio 2022 Sql server 2008 新建项目 我们看一下visual studio 里面ADO.NET文件 ADO.NET是实体数据模型,是ORM对象文件。ORM,即Object-Relational Mapping(对象关系映射)。 ORM实际上是对业务的简化。就想面向过程到面向对象的转变一样。 面向过程和面向对象 面向过程:程序

    2024年02月09日
    浏览(52)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包