Skip to content

Instantly share code, notes, and snippets.

@eduardoalba
Forked from georgepaoli/rfc-sap-dotnet.cs
Created September 2, 2020 13:14
Show Gist options
  • Save eduardoalba/b6c23664dcc43b475451efacda740b7a to your computer and use it in GitHub Desktop.
Save eduardoalba/b6c23664dcc43b475451efacda740b7a to your computer and use it in GitHub Desktop.
Exemplo de integração com SAP via RFC em C#
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SAP.Middleware.Connector;
namespace exemplo
{
public class Teste
{
// Configuracoes armazenadas no .config
private static class SapConfigs
{
public static string Destination {
get { return ConfigurationManager.AppSettings["SAP:DESTINATION"]; }
}
public static string Host {
get { return ConfigurationManager.AppSettings["SAP:HOST"]; }
}
public static string SysNumber {
get { return ConfigurationManager.AppSettings["SAP:SYSNUMBER"]; }
}
public static string Client {
get { return ConfigurationManager.AppSettings["SAP:CLIENT"]; }
}
public static string User {
get { return ConfigurationManager.AppSettings["SAP:USER"]; }
}
public static string Password {
get { return ConfigurationManager.AppSettings["SAP:PASSWORD"]; }
}
public static string Function {
get { return ConfigurationManager.AppSettings["SAP:FUNCTION"]; }
}
}
// Parametros que enviaremos para a funcao no SAP
public class Form
{
public string solicitante { get; set; }
public string centroCusto { get; set; }
public string emailSolicitante { get; set; }
}
// Faz a chamada RFC de teste
public string Solicitar(Form dados)
{
var rfcParams = new RfcConfigParameters();
// Alimenta parametros SAP Logon
rfcParams.Add(RfcConfigParameters.Name, SapConfigs.Destination);
rfcParams.Add(RfcConfigParameters.AppServerHost, SapConfigs.Host);
rfcParams.Add(RfcConfigParameters.SystemNumber, SapConfigs.SysNumber);
// Alimenta parametros SAP GUI
rfcParams.Add(RfcConfigParameters.Client, SapConfigs.Client);
rfcParams.Add(RfcConfigParameters.User, SapConfigs.User);
rfcParams.Add(RfcConfigParameters.Password, SapConfigs.Password);
// Faz a conexao rfc
var destination = RfcDestinationManager.GetDestination(rfcParams);
RfcRepository repo = destination.Repository;
IRfcFunction function = repo.CreateFunction(SapConfigs.Function);
function.SetValue("SOLICITANTE", dados.solicitante);
function.SetValue("CENTRO_CUSTO", dados.centroCusto);
function.SetValue("EMAIL_SOLIC", dados.emailSolicitante);
// Chama a funcao
function.Invoke(destination);
// Aguarda e recebe retoro
var result = function.GetDecimal("E_NSOLIC");
return result.ToString();
}
// Ping para checar conexao com SAP
public void Ping()
{
var rfcParams = new RfcConfigParameters();
// Alimenta parametros SAP Logon
rfcParams.Add(RfcConfigParameters.Name, SapConfigs.Destination);
rfcParams.Add(RfcConfigParameters.AppServerHost, SapConfigs.Host);
rfcParams.Add(RfcConfigParameters.SystemNumber, SapConfigs.SysNumber);
// Alimenta parametros SAP GUI
rfcParams.Add(RfcConfigParameters.Client, SapConfigs.Client);
rfcParams.Add(RfcConfigParameters.User, SapConfigs.User);
rfcParams.Add(RfcConfigParameters.Password, SapConfigs.Password);
// Faz a conexao rfc
var destination = RfcDestinationManager.GetDestination(rfcParams);
RfcRepository repo = destination.Repository;
destination.Ping();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment