• Sapnco3.0 RFC Server Programs Receive Idocs


    sapnco3 接收IDOC准备工作:安装sapcon3.0_X64.rar 或 Setup_ntintel_301_32位安装包.rar。并将安装目录下的dll引用到工程项目中

    当与SAP进行数据交互,作为Server的时候,需要注册客户端和服务端,并在服务端提供SAP调用的函数,示例中供SAP调用的函数名为IDOC_INBOUND_ASYNCHRONOUS

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using SAP.Middleware.Connector;
     6 
     7 
     8 
     9 namespace Sapnoc30Demo_Yan
    10 {
    11     
    12     public class SapncoClient
    13     {
    14         private RfcDestination _rfcDestination = null;
    15 
    16         /// <summary>
    17         /// Register Client
    18         /// </summary>
    19         public void RegisterRfcDestination()
    20         {
    21             try
    22             {
    23                 if (_rfcDestination == null)
    24                     _rfcDestination = RfcDestinationManager.GetDestination("NCO_Destination"); //获取 config配置中NCO_Destination节点参数,并注册RfcClient
    25             }
    26             catch (Exception ex)
    27             {
    28                 throw ex;
    29             }
    30         }
    31 }
    32 }
    注册客户端
      1 using System;
      2 using System.Collections.Generic;
      3 using System.Linq;
      4 using System.Text;
      5 using SAP.Middleware.Connector;
      6 
      7 namespace Sapnoc30Demo_Yan
      8 {
      9     public class SapncoServer
     10     {
     11         private RfcServer _rfcServer = null;
     12        
     13         public void RegisterRfcServer()
     14         {
     15             try
     16             {
     17                 Type[] handlers = new Type[1] { typeof(SAPServerHandler) };
     18                 if (_rfcServer == null)
     19                     _rfcServer = RfcServerManager.GetServer("NCO_Server", handlers); //获取 config配置中NCO_Server节点参数,并注册RfcServer
     20                 _rfcServer.RfcServerError += OnRfcServerError;
     21                 _rfcServer.RfcServerApplicationError += OnRfcServerError;
     22                 _rfcServer.TransactionIDHandler = new SapncoTidHandler();
     23                 SAPServerHandler.IDocEndReceiveCompleteEvent+= new SAPServerHandler.IDocReceiveEventHandler(SAPServerHandler_IDocEndReceiveCompleteEvent);
     24                 _rfcServer.Start();
     25             }
     26             catch (Exception ex)
     27             {
     28                 throw ex;
     29             }
     30         }
     31         //函数调用执行完成
     32         private void SAPServerHandler_IDocEndReceiveCompleteEvent(Dictionary<string, List<string>> diclist)
     33         { 
     34             //处理 diclist
     35             //根据IDOC各个字段长度,从SDATA字符串中截取对应的字段值
     36         }
     37 
     38         private static void OnRfcServerError(Object server, RfcServerErrorEventArgs errorEventData)
     39         {
     40             string exMsg = String.Empty;
     41             RfcServer rfcServer = server as RfcServer;
     42             RfcServerApplicationException appEx = errorEventData.Error as RfcServerApplicationException;
     43             if (appEx != null)
     44                 exMsg += string.Format("RfcServerApplicationError occured in RFC server {0} :", rfcServer.Name);
     45             else
     46                 exMsg += string.Format("RfcServerError occured in RFC server {0} :", rfcServer.Name);
     47 
     48             if (errorEventData.ServerContextInfo != null)
     49             {
     50                 exMsg += string.Format("RFC Caller System ID: {0} ", errorEventData.ServerContextInfo.SystemAttributes.SystemID);
     51                 exMsg += string.Format("RFC function Name: {0} ", errorEventData.ServerContextInfo.FunctionName);
     52             }
     53             exMsg += string.Format("Error type: {0}", errorEventData.Error.GetType().Name);
     54             exMsg += string.Format("Error message: {0}", errorEventData.Error.Message);
     55             if (appEx != null)
     56             {
     57                 exMsg += string.Format("Inner exception type: {0}", appEx.InnerException.GetType().Name);
     58                 exMsg += string.Format("Inner exception message: {0}", appEx.InnerException.Message);
     59             }
     60 
     61             Exception ex = new Exception(exMsg);
     62             throw ex;
     63         }
     64     }
     65 
     66     public class SAPServerHandler
     67     {
     68         public delegate void IDocReceiveEventHandler(Dictionary<string, List<string>> diclist);
     69         public static event IDocReceiveEventHandler IDocEndReceiveCompleteEvent;
     70         //public static event IDocReceiveEventHandler IDocBeginReceiveCompleteEvent;
     71 
     72         /// <summary>
     73         /// Call back handler method,invoked by RFC Server.
     74         /// </summary>
     75         /// <param name="context">RfcServerContext</param>
     76         /// <param name="function">Rfc Function passed by Rfc Server manager</param>
     77         [RfcServerFunction(Name = "IDOC_INBOUND_ASYNCHRONOUS", Default = false)]
     78         public static void IDOC_INBOUND_ASYNCHRONOUS(RfcServerContext context, IRfcFunction function)
     79         {
     80             Dictionary<string, List<string>> diclist = new Dictionary<string, List<string>>();
     81             string docnum = String.Empty;
     82 
     83             IRfcTable RfcTb = function.GetTable("EDI_DC40"); //获取 EDI_DC40 结构
     84             foreach (IRfcStructure rfcControlRecord in RfcTb)
     85             {
     86                 docnum = rfcControlRecord.GetValue("DOCNUM").ToString(); //从 EDI_DC40 结构中获取 DOCNUM 字段值
     87             }
     88 
     89 
     90             IRfcTable RfcDtTable = function.GetTable("EDI_DD40"); //获取 EDI_DD40 结构
     91             List<string> list = new List<string>();
     92             foreach (IRfcStructure rfcDataRecord in RfcDtTable)
     93             {
     94                 string sdataStr = rfcDataRecord.GetValue("SDATA").ToString(); //获取IDOC_DATA_REC_40 结构中SDATA字段值(IDOC值)
     95                 list.Add(sdataStr);
     96             }
     97             diclist.Add(docnum, list);
     98             if (IDocEndReceiveCompleteEvent != null)
     99                 IDocEndReceiveCompleteEvent(diclist);
    100         }
    101 
    102         #region 含IDOC 的RfcTable结构
    103         //function.GetTable(0)    
    104         //{TABLE  [STRUCTURE EDI_DC40 { FIELD TABNAM=EDI_DC40 FIELD MANDT=777 FIELD DOCNUM=0000000009241610 FIELD DOCREL=700 FIELD STATUS=30 FIELD DIRECT=1 FIELD OUTMOD=2 FIELD EXPRSS= FIELD TEST= FIELD IDOCTYP=ZTZECODE_IDOC FIELD CIMTYP= FIELD MESTYP=ZECODE_MSG FIELD MESCOD= FIELD MESFCT= FIELD STD= FIELD STDVRS= FIELD STDMES= FIELD SNDPOR=SAPASB FIELD SNDPRT=LS FIELD SNDPFC= FIELD SNDPRN=AZASB01777 FIELD SNDSAD= FIELD SNDLAD= FIELD RCVPOR=A000000017 FIELD RCVPRT=LS FIELD RCVPFC= FIELD RCVPRN=CN_ECODING FIELD RCVSAD= FIELD RCVLAD= FIELD CREDAT=2013-06-18 FIELD CRETIM=10:35:40 FIELD REFINT= FIELD REFGRP= FIELD REFMES= FIELD ARCKEY= FIELD SERIAL=20130618103540 }]}    SAP.Middleware.Connector.IRfcTable {SAP.Middleware.Connector.RfcTable}
    105         //function.GetTable(1)    
    106         //{TABLE  [STRUCTURE EDI_DD40 { FIELD SEGNAM=ZTZECODE000 FIELD MANDT=777 FIELD DOCNUM=0000000009241610 FIELD SEGNUM=000001 FIELD PSGNUM=000000 FIELD HLEVEL=02 FIELD SDATA=0003575897008500006600000010251                   CNIT00006 9000001001                              10010     2              1 }]}
    107         //SAP.Middleware.Connector.IRfcTable {SAP.Middleware.Connector.RfcTable}
    108         #endregion
    109 
    110     }
    111 
    112     public class SapncoTidHandler : ITransactionIDHandler
    113     {
    114         //ONLY for tests. Use a database to store the TID state!
    115         static List<string> tids = new List<string>();
    116 
    117         //If DB is down, throw an exception at this point. .Net Connector will then abort 
    118         //the tRFC and the R/3 backend will try again later.
    119         public bool CheckTransactionID(RfcServerContextInfo serverContext, RfcTID tid)
    120         {
    121             //TextBox.Text += ("Check transaction ID " + tid);
    122             lock (tids)
    123             {
    124                 if (tids.Contains(tid.TID))
    125                     return false;
    126                 else
    127                 {
    128                     tids.Add(tid.TID);
    129                     return true;
    130                 }
    131             }
    132             // "true" means that NCo will now execute the transaction, "false" means
    133             // that we have already executed this transaction previously, so NCo will
    134             // skip the function execution step and will immediately return an OK code to R/3.
    135         }
    136 
    137         // clean up the resources
    138         public void ConfirmTransactionID(RfcServerContextInfo serverContext, RfcTID tid)
    139         {
    140             //TextBox.Text += ("Confirm transaction ID " + tid);
    141             try
    142             {
    143                 //clean up the resources
    144                 //partner won't react on an exception at this point
    145             }
    146             finally
    147             {
    148                 lock (tids)
    149                 {
    150                     tids.Remove(tid.TID);
    151                 }
    152             }
    153         }
    154 
    155 
    156         // react on commit e.g. commit on the database
    157         // if necessary throw an exception, if the commit was not possible
    158         public void Commit(RfcServerContextInfo serverContext, RfcTID tid)
    159         {
    160             //TextBox.Text += ("Commit transaction ID " + tid);
    161             lock (tids)
    162             {
    163                 if (!tids.Contains(tid.TID))
    164                     throw new Exception("tid " + tid.TID + " doesn't exist, hence it cannot be committed");
    165 
    166             }
    167         }
    168 
    169         // react on rollback e.g. rollback on the database
    170         public void Rollback(RfcServerContextInfo serverContext, RfcTID tid)
    171         {
    172             //TextBox.Text += ("Rollback transaction ID " + tid);
    173             /* Make sure the TID is removed from the list, otherwise CheckTransactionID will
    174              * return false the next time the backend retries this failed transaction, and then
    175              * it will never be executed...
    176              */
    177             tids.Remove(tid.TID);
    178         }
    179     }
    180 }
    注册服务端
     1 <?xml version="1.0"?>
     2 <configuration>
     3   <configSections>
     4     <sectionGroup name="SAP.Middleware.Connector">
     5       <sectionGroup name="ClientSettings">
     6         <section name="DestinationConfiguration" type="SAP.Middleware.Connector.RfcDestinationConfiguration,sapnco"/>
     7       </sectionGroup>
     8       <sectionGroup name="ServerSettings">
     9         <section name="ServerConfiguration" type="SAP.Middleware.Connector.RfcServerConfiguration, sapnco"/>
    10       </sectionGroup>
    11     </sectionGroup>
    12   </configSections>
    13   <SAP.Middleware.Connector>
    14     <ClientSettings>
    15       <DestinationConfiguration>
    16         <destinations>
    17           <add NAME="NCO_Destination" USER="CX_HANM" PASSWD="hg113" CLIENT="300" SYSNR="00" ASHOST="172.16.8.11" LANG="EN" GROUP="PUBLIC" MAX_POOL_SIZE="5"></add>
    18         </destinations>
    19       </DestinationConfiguration>
    20     </ClientSettings>
    21     <ServerSettings>
    22       <ServerConfiguration>
    23         <servers>
    24           <add NAME="NCO_Server" GWHOST="hostsys1" GWSERV="sapgw00" PROGRAM_ID="NCoServer" REPOSITORY_DESTINATION="NCO_TESTS" REG_COUNT="1"/>
    25         </servers>
    26       </ServerConfiguration>
    27     </ServerSettings>
    28   </SAP.Middleware.Connector>
    29 </configuration>
    配置文件
     1 using System;
     2 using System.Collections.Generic;
     3 using System.ComponentModel;
     4 using System.Data;
     5 using System.Drawing;
     6 using System.Linq;
     7 using System.Text;
     8 using System.Windows.Forms;
     9 
    10 namespace Sapnoc30Demo_Yan
    11 {
    12     public partial class ServerTest : Form
    13     {
    14         public ServerTest()
    15         {
    16             InitializeComponent();
    17         }
    18 
    19         private void ServerTest_Load(object sender, EventArgs e)
    20         {
    21             SapncoClient client = new SapncoClient();
    22             SapncoServer server = new SapncoServer();
    23             client.RegisterRfcDestination();
    24             server.RegisterRfcServer();
    25         }
    26     }
    27 }
    服务端调用


    源码下载地址:Sapnoc30Demo_Yan.rar

  • 相关阅读:
    PHPCMS模型
    SQL 根据中文分词组件来搜索表
    初识wordpress
    session_set_save_handler
    使用Sharepoint 中Webservice得到用户列表
    鸡蛋里挑骨头PHPCMS
    Type: FileNotFoundException, Exception Message: 未能加载文件或程序集
    很实用的Jquery验证插件
    CSS link media属性
    OCS (错误代码: 01492)
  • 原文地址:https://www.cnblogs.com/yf2011/p/3095157.html
Copyright © 2020-2023  润新知