【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。如下图:


image

在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}"

查看结果如图

image

参考资料

SapConnection connect issues #5 : https://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.

当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!

分类: 【Azure 应用服务】

标签: App Service for Window, SapNwRfc.dll, SAP NetWeaver RFC library, 手动加载ICU库, NativeLibrary.Load, (path.Combinee(rootDir, "nwrfcsdk", "icuuc50"))

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容