【Azure 应用服务】Azure App Service(Windows)环境中如何让.NET应用调用SAP NetWeaver RFC函数

问题描述

在Azure App Service for Windows的环境中,部署.NET应用,其中使用了 SAP NetWeaver RFC函数 (需要加载 sapnwrfc.dll)。详细的错误为:

“System.DllNotFoundException: Unable to load DLL 'sapnwrfc' or one of its dependencies: The specified module could not be found. (0x8007007E)”

那么在App Service中如何来解决这个问题呢?

 

问题解答

在App Service的日志中,除了可见”System.DllNotFoundException: Unable to load DLL 'sapnwrfc' or one of its dependencies: The specified module could not be found“外,也显示 Could not open the ICU common library。如下图:

 

在App Service不能加载ICU通用库的提示下,就需要在代码中显示的加载ICU库。所以在代码中添加 NativeLibrary.Load(path.Combinee(rootDir, "nwrfcsdk", "icuin50")) 即可缓解问题。

代码示例为:

复制代码
using System.Reflection;
using System.Runtime.InteropServices;
using NwRfcNet;

namespace CallSAPonAppService
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            // Add services to the container.

            builder.Services.AddControllers();
            // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
            builder.Services.AddEndpointsApiExplorer();
            builder.Services.AddSwaggerGen();

            var app = builder.Build();

            // Configure the HTTP request pipeline.
            if (app.Environment.IsDevelopment())
            {
                app.UseSwagger();
                app.UseSwaggerUI();
            }

            app.UseHttpsRedirection();

            app.UseAuthorization();


            app.MapControllers();

            NativeLibrary.SetDllImportResolver(
                assembly: typeof(RfcConnection).Assembly,
                resolver: (string libraryName, Assembly assembly, DllImportSearchPath? searchPath) =>
                {
                    if (libraryName == "sapnwrfc")
                    {
                        var rootDir = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                        string path = Path.Combine(rootDir, "nwrfcsdk", libraryName);

                        //手动加载ICU库
                        NativeLibrary.Load(path.Combinee(rootDir, "nwrfcsdk", "icuuc50"));
                        NativeLibrary.Load(path.Combinee(rootDir, "nwrfcsdk", "icudt50"));
                        NativeLibrary.Load(path.Combinee(rootDir, "nwrfcsdk", "icuin50"));

                        NativeLibrary.TryLoad(
                            libraryPath: path,
                            handle: out IntPtr handle);

                        return handle;
                    }

                    return IntPtr.Zero;
                });

            app.Run();
        }
    }
}
复制代码

 

附录一:在App Service Kudu(高级管理工具页面)如何来查看某个依赖是否存在?

如查看是否安装Visual C++ 2013 Redistributable依赖,可以使用如下命令:

reg query "HKLM\SOFTWARE\Classes\Installer\Dependencies\{61087a79-ac85-455c-934d-1fa22cc64f36}"

查看结果如图

 

 

 

参考资料

SapConnection connect issues #5https://github.com/huysentruitw/SapNwRfc/issues/5

 

SAP NetWeaver RFC library : https://github.com/huysentruitw/SapNwRfc#prerequisites

This cross-platform library allows you to call SAP NetWeaver RFC functions from .NET 5, .NET Core and the .NET Framework.

The library is fully tested and production ready. Supported operating systems are Windows, Linux and macOS.

posted @   路边两盏灯  阅读(182)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2022-02-21 【Azure 应用服务】Azure App Service能否使用Storage Account File Share
点击右上角即可分享
微信分享提示