Support Topics Documentation Slack YouTube Blog

Trouble uploading a file with the .NET API

Mission: FILES MASTER
Task: Upload a file using API

Please, describe your problem here.

Hi, I am trying to work through the mission to upload a file via the .NET API. I have created a simple console project, .Net Core, Visual Studio 2019. I have copied and pasted the sample code from the documentation on the site. My code looks like this (I have changed the API key and APP ID):

using System;
using System.Data;
using System.IO;
using BackendlessAPI;
using BackendlessAPI.Async;

namespace BackEndless
{
class Program
{
static void Main(string[] args)
{
const string appID = “???”;
const string apiKey = “???”;

        Backendless.InitApp(appID, apiKey);


        Console.WriteLine("Hello World!");

        UploadFile();
    }

    static void UploadFile()
    {
        AsyncCallback<BackendlessAPI.File.BackendlessFile> callback = new AsyncCallback<BackendlessAPI.File.BackendlessFile>(
                result =>
                {
                    System.Console.WriteLine("File uploaded. URL - " + result.FileURL);
                },

                fault =>
                {
                    System.Console.WriteLine("Error - " + fault);
                });

        FileStream fs = new FileStream(@"c:\projects\BackEndless\superfast.html", FileMode.Open, FileAccess.Read);
        BackendlessAPI.Backendless.Files.Upload(fs, "mission", callback);
    }
}

}

The exception that I receive is shown here:

System.TypeLoadException
HResult=0x80131522
Message=Could not load type ‘BackendlessAPI.Backendless’ from assembly ‘BackEndless, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null’.
Source=
StackTrace:

The output in the output console is shown here:

‘BackEndless.exe’ (CoreCLR: DefaultDomain): Loaded ‘C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.9\System.Private.CoreLib.dll’. Skipped loading symbols. Module is optimized and the debugger option ‘Just My Code’ is enabled.
‘BackEndless.exe’ (CoreCLR: clrhost): Loaded ‘C:\Projects\BackEndless\bin\Debug\netcoreapp3.1\BackEndless.dll’. Symbols loaded.
‘BackEndless.exe’ (CoreCLR: clrhost): Loaded ‘C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.9\System.Runtime.dll’. Skipped loading symbols. Module is optimized and the debugger option ‘Just My Code’ is enabled.
An unhandled exception of type ‘System.TypeLoadException’ occurred in Unknown Module.
Could not load type ‘BackendlessAPI.Backendless’ from assembly ‘BackEndless, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null’.

Any ideas what might be goiung wrong? I tsounds like something blew up in a constructor call, but the exception info doesn’t give much of a hint about the exact nature of the problem.

Thanks,

Aaron

Hello @Aaron_Schaefer.

Is the assembly in the Global Assembly Cache (GAC) or any place the might be overriding the assembly that you think is being loaded? This is usually the result of an incorrect assembly being loaded, for me it means I usually have something in the GAC overriding the version I have in bin/Debug.

Also you can try to create a new project with the same code but with a different name.
Most likely during compilation, something went wrong.

Regards, Nikita.

Hi,

I don’t think there’s anything strange in the GAC. I did try creating a new project with a different name as suggested. This time, I am creating a .Net Framework console project, named xxx. The code is shown below. This time, I am not seeing an exception, but my callback is also not being called, either the success or the failure handler, and the file is not uploaded to the server. I set breakpoints in both that did not get hit, and the trace output was not written.

Any other ideas? I have successfully used the API from the REST console and JavaScript, but my app will be a .NET app, so that’s the one I need to work.

Thanks,

Aaron

    static void Main(string[] args)
    {
        string appId = "...";
        string apiKey = "...";

        Backendless.InitApp(appId, apiKey);

        UploadFile();
    }

    static void UploadFile()
    {
        AsyncCallback<BackendlessAPI.File.BackendlessFile> callback = new AsyncCallback<BackendlessAPI.File.BackendlessFile>(
                result =>
                {
                    System.Diagnostics.Trace.WriteLine("File uploaded. URL - " + result.FileURL);
                },

                fault =>
                {
                    System.Diagnostics.Trace.WriteLine("Error - " + fault);
                });

        FileStream fs = new FileStream(@"c:\projects\BackEndless\superfast.html", FileMode.Open, FileAccess.Read);
        BackendlessAPI.Backendless.Files.Upload(fs, "mission", callback);

    }
}

Hello @Aaron_Schaefer.

Most likely, the program exited before the call was processed by the server.
Try setting a delay after the above code Thread.Sleep( 5000 );

Regards, Nikita.

Hello, @Aaron_Schaefer.

Has your problem been solved?

Regards, Nikita.

Thanks for checking back. Everything seems to be working now after trying your suggestion.

Thanks!