API Support Forum
OEC API > API Support > Try to run a sample program from the documentaion get System.TypeLoadException error
Author Topic: Try to run a sample program from the documentaion get System.TypeLoadException error
(4 messages, Page 1 of 1)
Moderators: VPfau
AAmi1662
Posts: 4
Joined: Sep 15, 2021


Posted: Sep 15, 2021 @ 03:55 PM             Msg. 1 of 4
Hi, first time using C# and gain api. I'm trying to create a connection to the servers and i get the error:

System.TypeLoadException: 'Method 'TryGetConstructor' in type 'GF.SimpleInjector.InternalConstructorResolutionBehavior' from assembly 'GF.SimpleInjector, Version=4.0.3.13, Culture=neutral, PublicKeyToken=b6b45f2252711217' does not have an implementation.'

not sure if it helpful but i got alert as well with yellow triangle saying:

Package 'GFAPI 4.0.3.44' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8' instead of the project target framework '.NETCoreApp,Version=v3.1'. This package may not be fully compatible with your project.

The code:
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
internal class Program
{
private static void Main(string[] args)
{
GF.Api.IGFClient gfClient = GF.Api.Impl.GFApi.CreateClient();

var runner = new GF.Api.Threading.GFClientRunner(gfClient);
runner.Start();

Console.WriteLine("Connecting...");

gfClient.Connection.Aggregate.LoginCompleted += (client, e) => Console.WriteLine("Connection complete");
gfClient.Connection.Aggregate.LoginFailed += (client, e) => Console.WriteLine($"Connection failed: {e.FailReason}");
gfClient.Connection.Aggregate.Disconnected += (client, e) => Console.WriteLine($"Disconnected: {e.Message}");
gfClient.Logging.ErrorOccurred += (client, e) => Console.WriteLine($"{e.Exception.Message}");

gfClient.Threading.Invoke(() =>
gfClient.Connection.Aggregate.Connect(
new GF.Api.Connection.ConnectionContextBuilder()
.WithUserName("user")
.WithPassword("pass")
.WithUUID("9e61a8bc-0a31-4542-ad85-33ebab0e4e86")
.WithPort(9210)
.WithHost("api.gainfutures.com")
.WithForceLogin(true)
.Build()));

var timer = new System.Timers.Timer { Interval = TimeSpan.FromSeconds(2).TotalMilliseconds };
timer.Elapsed += (_, __) =>
{
// The timer callback is on a different thread than the GFClientRunner, so we must delegate to the runner thread
gfClient.Threading.Invoke(() =>
{
if (!gfClient.Connection.Aggregate.IsConnected)
return;

var account = gfClient.Accounts.Get().First();
GF.Api.Balances.IBalance totalBalance = account.TotalBalance;

Console.WriteLine($"Account: {account.Spec}");
Console.WriteLine($"\tNetLiq: {totalBalance.NetLiq:c}");
Console.WriteLine($"\tCash: {totalBalance.Cash:c}");
Console.WriteLine($"\tOpen P/L: {totalBalance.OpenPnL:c}");
Console.WriteLine($"\tTotal P/L: {totalBalance.RealizedPnL + totalBalance.OpenPnL:c}");
Console.WriteLine($"\tInitial Margin: {totalBalance.InitialMargin:c}");
Console.WriteLine($"\tNet Options Value: {totalBalance.LongCallOptionsValue + totalBalance.LongPutOptionsValue + totalBalance.ShortCallOptionsValue + totalBalance.ShortPutOptionsValue:c}");
Console.WriteLine($"Average Positions: {account.AvgPositions.Count}");
Console.WriteLine($"Orders: {gfClient.Orders.Get().Count}, last one: {(gfClient.Orders.Get().Count > 0 ? gfClient.Orders.Get().Last().ToString() : string.Empty)}");
Console.WriteLine();
});
};

Console.WriteLine("Press any key to quit");
Console.ReadKey();

timer.Stop();
runner.Stop();
}
}






How do i solve it?
Thank you
Edited by AAmi1662 on Sep 17, 2021 05:29 AM
AAmi1662
Posts: 4
Joined: Sep 15, 2021


Posted: Sep 17, 2021 @ 05:30 AM             Msg. 2 of 4
Hi, updated the post with the code.
AAmi1662
Posts: 4
Joined: Sep 15, 2021


Posted: Sep 17, 2021 @ 02:34 PM             Msg. 3 of 4
** FIxed by reinstalling everything
SRuscak
Posts: 41
Joined: Aug 24, 2017


Posted: Sep 17, 2021 @ 02:48 PM             Msg. 4 of 4
Hello.

The answer is in the GF API assembly warning:
Package 'GFAPI 4.0.3.44' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8' instead of the project target framework '.NETCoreApp,Version=v3.1'. This package may not be fully compatible with your project.
.NET Framework assemblies can't be called from .NET Core 3.1.