演示Nco3.0 RFC client programming的方法(也就是還有server端編程)囱持。代碼分成四塊:
- DestinationProvider: 填充登錄參數(shù)(logon parameters)汹桦,獲取
RfcDestination
- Rfc函數(shù)調(diào)用:調(diào)用SAP FM的代碼是模式化的辩蛋,基本都是這種調(diào)用模式
- 處理
IRfcStructure
- 單元測試
DestinationProvider
文件:DestinationProvider.cs
using SAP.Middleware.Connector;
namespace Nco02
{
public class DestinationProvider
{
private static RfcConfigParameters GetConfigParams()
{
RfcConfigParameters configParams = new RfcConfigParameters();
// Name property is neccessary, otherwise, NonInvalidParameterException will be thrown
configParams.Add(RfcConfigParameters.Name, "ECC");
configParams.Add(RfcConfigParameters.AppServerHost, "192.168.65.100");
configParams.Add(RfcConfigParameters.SystemNumber, "00"); // instance number
configParams.Add(RfcConfigParameters.SystemID, "D01");
configParams.Add(RfcConfigParameters.User, "STONE");
configParams.Add(RfcConfigParameters.Password, "123456");
configParams.Add(RfcConfigParameters.Client, "001");
configParams.Add(RfcConfigParameters.Language, "EN");
configParams.Add(RfcConfigParameters.PoolSize, "5");
configParams.Add(RfcConfigParameters.MaxPoolSize, "10");
configParams.Add(RfcConfigParameters.IdleTimeout, "30");
return configParams;
}
public static RfcDestination GetDestination()
{
RfcConfigParameters configParams = GetConfigParams();
RfcDestination dest = RfcDestinationManager.GetDestination(configParams);
return dest;
}
}
}
Rfc函數(shù)調(diào)用
文件:RfcCall.cs
using System;
using System.Collections;
using SAP.Middleware.Connector;
namespace Nco02
{
public class RfcCall
{
// get information from company code
public ArrayList GetCocdInfo(String cocd)
{
var list = new ArrayList();
RfcDestination dest = DestinationProvider.GetDestination();
RfcRepository repository = dest.Repository;
IRfcFunction fm = repository.CreateFunction("BAPI_COMPANYCODE_GETDETAIL");
fm.SetValue("COMPANYCODEID", cocd); // Populate parameter
fm.Invoke(dest); // call function
// BAPI_COMPANYCODE_GETDETAIL returns a structure named COMPANYCODE_DETAIL
// which contains the information of the company code
IRfcStructure cocdDetail = fm.GetStructure("COMPANYCODE_DETAIL");
list = Utils.ToArrayList(cocdDetail);
return list;
}
}
}
和之前RFC調(diào)用的方法相比劲厌,現(xiàn)在的方法確實方便多了。說下幾個重點:
編程人員不用關(guān)心與SAP系統(tǒng)的連接沮榜,只需要關(guān)心RfcDestination签钩,RfcDestination代表后端SAP系統(tǒng),nco3.0負(fù)責(zé)連接和關(guān)閉帜慢。我們把RfcDestination封裝在DestinationProvider類中笼裳,RfcCall類只需要
RfcDestination dest = DestinationProvider.GetDestination();
獲取就可以。IRfcFunction代表SAP系統(tǒng)的函數(shù)
SAP的函數(shù)包含在Repository中粱玲,所以獲取SAP函數(shù)使用下面的代碼:
RfcDestination dest = XXX;
RfcRepository repository = dest.Repository;
IRfcFunction fm = repository.CreateFunction("xxx");
一般我們關(guān)心Repository
不多躬柬,所以也可以寫為:
RfcDestination dest = XXX;
IRfcFunction fm = dest.Repository.CreateFunction("xxx")
IRfcStructure
BAPI_COMPANYCODE_GETDETAIL函數(shù)exporting參數(shù)COMPANYCODE_DETAIL是一個結(jié)構(gòu),我們將它轉(zhuǎn)換成一個ArrayList抽减,代碼如下允青,在Utils.cs中:
using SAP.Middleware.Connector;
using System;
using System.Collections;
namespace Nco02
{
public class Utils
{
public static ArrayList ToArrayList(IRfcStructure stru)
{
var list = new ArrayList();
for (int i = 0; i < stru.ElementCount; i++) {
// get column name from position
RfcElementMetadata colMeta = stru.GetElementMetadata(i);
list.Add(String.Format("{0}: {1}",
colMeta.Name, // column name
stru.GetString(colMeta.Name))); // get value from column name
}
return list;
}
}
}
單元測試
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Nco02;
using System.Collections;
namespace UnitTestProject1
{
[TestClass]
public class TestRfcCall
{
[TestMethod]
public void Test_RfcCall()
{
RfcCall rfc = new RfcCall();
ArrayList list = rfc.GetCocdInfo("0001");
foreach (String item in list) {
System.Console.WriteLine(item);
}
}
}
}