Franklin
Franklin

Reputation: 1

Problem with C# and SAP .Net Connector 3.1.5.0 (x64)

At the moment I am trying to use the SAP .Net Connector 3.1.5.0 in one of my C# console apps. I would like to retrieve data from our SAP BW system with the C# program by using a BW query that is saved in a MDX file.

I have searched and tested a lot in the past days but I am not able to get the program running. I always get the following error message at line 18 of the code:


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. 

My program.cs code is the following:


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;
            }
        }
    }
}

My app.config is looking like this:

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

</configuration>

My .csproj file looks like this:


<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>

Does anybody of you has an idea or a hint how I can get rid of this error?

Many thanks in advance.

Frank.

Upvotes: 0

Views: 160

Answers (1)

programwindow
programwindow

Reputation: 1

  1. Your your project may be compiled using different .Net (framework/.netcore/.net) version and the dependencies used in it target different .Net version.
  2. It could be asking for some VC++ runtimes pre-installed.

Upvotes: 0

Related Questions