You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#结合SAP .Net Connector 3.1.5.0(x64)连接SAP BW遇类型加载错误求助

SAP .Net Connector 3.1.5.0 在.NET 8控制台应用中加载失败问题排查

我正尝试在C#控制台应用中使用SAP .Net Connector 3.1.5.0,通过MDX文件中保存的BW查询从SAP BW系统获取数据。过去几天做了大量搜索和测试,但程序始终无法运行,在代码第18行(RfcDestinationManager.RegisterDestinationConfiguration(new MyDestinationConfig());)总是出现以下错误:

Error message at:
Line 18 => RfcDestinationManager.RegisterDestinationConfiguration(new MyDestinationConfig());

=============================================================================================

Windows Version: 10.0.22621
.NET 8.0.11
Exception thrown: 'System.TypeInitializationException' in sapnco.dll
An error occurred: The type initializer for 'SAP.Middleware.Connector.RfcDestinationManager' threw an exception.
Inner exception: The type initializer for 'SAP.Middleware.Connector.RfcConfigParameters' threw an exception.
Innermost exception: Could not load type 'System.ServiceModel.Activation.VirtualPathExtension' from assembly 'System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089. 

Program.cs 代码

using SAP.Middleware.Connector;
using System.Runtime.InteropServices;

namespace SAPBW_MDX_Example
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                var version = Environment.OSVersion.Version;
                Console.WriteLine($"Windows Version: {version.Major}.{version.Minor}.{version.Build}");

                Console.WriteLine(RuntimeInformation.FrameworkDescription);
                
                RfcDestinationManager.RegisterDestinationConfiguration(new MyDestinationConfig());
                RfcDestination destination = RfcDestinationManager.GetDestination("mySAPdestination");

                RfcRepository repository = destination.Repository;
                IRfcFunction mdxFunction = repository.CreateFunction("BAPI_MDX_EXECUTE");

                // Read the MDX query from the file
                string mdxQuery = File.ReadAllText(@"C:\Daten\Q014.mdx");
                mdxFunction.SetValue("MDX_QUERY", mdxQuery);

                // Execute the MDX query
                mdxFunction.Invoke(destination);

                // Retrieve the result
                IRfcTable resultTable = mdxFunction.GetTable("RESULT");

                // Process the result
                foreach (IRfcStructure row in resultTable)
                {
                    Console.WriteLine($"Product Category: {row.GetString(\"PRODUCT_CATEGORY\")}, Sales: {row.GetDecimal(\"SALES\")}, Quantity: {row.GetDecimal(\"QUANTITY\")}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("An error occurred: " + ex.Message);
                if (ex.InnerException != null)
                {
                    Console.WriteLine("Inner exception: " + ex.InnerException.Message);
                    if (ex.InnerException.InnerException != null)
                    {
                        Console.WriteLine("Innermost exception: " + ex.InnerException.InnerException.Message);
                    }
                }
            }
        }
    }
    
    public class MyDestinationConfig : IDestinationConfiguration
    {
        public bool ChangeEventsSupported() => false;
        public event RfcDestinationManager.ConfigurationChangeHandler ConfigurationChanged;

        public RfcConfigParameters GetParameters(string destinationName)
        {
            if ("mySAPdestination".Equals(destinationName))
            {
                RfcConfigParameters parameters = new RfcConfigParameters();
                    
                parameters.Add(RfcConfigParameters.Name, "mySAPdestination");
                parameters.Add(RfcConfigParameters.AppServerHost, "xxx.xxx.41.158");
                parameters.Add(RfcConfigParameters.SystemNumber, "00");
                parameters.Add(RfcConfigParameters.User, "strUserID");
                parameters.Add(RfcConfigParameters.Password, "strPassword");
                parameters.Add(RfcConfigParameters.Client, "200" );
                parameters.Add(RfcConfigParameters.Language, "EN");
                
                return parameters;
            }
            else{
                return null;
            }
        }
    }
}

app.config 配置

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <startup useLegacyV2RuntimeActivationPolicy="true">
        <supportedRuntime version="v4.0" />
    </startup>

</configuration>

.csproj 文件内容

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0-windows10.0.22621</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <PlatformTarget>x64</PlatformTarget>
    <OutputPath>bin\Debug\</OutputPath>
  </PropertyGroup>
  
  <ItemGroup>
    <Reference Include="sapnco">
      <HintPath>libs\sapnco.dll</HintPath>
    </Reference>
    <Reference Include="sapnco_utils">
      <HintPath>libs\sapnco_utils.dll</HintPath>
      </Reference>
  </ItemGroup>
  
  <ItemGroup>
    <PackageReference Include="System.Configuration.ConfigurationManager" Version="9.0.1" />
    <PackageReference Include="System.ServiceModel.Http" Version="8.1.1" />
    <PackageReference Include="System.ServiceModel.Primitives" Version="8.1.1" />
  </ItemGroup>

</Project>

请问有人能提供解决该错误的思路或提示吗?

提前感谢!


内容的提问来源于stack exchange,提问作者Franklin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 21:07:32