0

I'm thinking I must be missing something obvious, but im trying to create a new DesignAutomationClient object like follows:

private void runDAButton_Click(object sender, EventArgs e)
        {
            createWorkItem();
        }

        private async Task createWorkItem()
        {
            var forgeConfig = new Autodesk.Forge.Core.ForgeConfiguration();
            forgeConfig.ClientId = clientID;
            forgeConfig.ClientSecret = clientSecret;

            var apiInstance = new DesignAutomationClient();            

            // Code to create work item will go here

        }

but when I do, the following error appears in my Visual Studio Debug/Immediate window after trying to execute the var apiInstance = new DesignAutomationClient(); line:

Exception thrown: 'System.TypeLoadException' in mscorlib.dll

Am i missing something obvious? The design automation client was downloaded using NuGet so i should have all the required depencies, but searches of forums for this kind of error all say it means I'm either missing a DLL file, or the type I'm looking for doesn't exist within a DLL, neither of which I believe are true.

This code is in a simple windows form application written in C# There are no web servers or ASP.NET involved. The user clicks a button on the form which runs the runDAButton_Click function(which in turn runs the createWorkItem() function). That function should create an instance of the API, and then use it to create my work item.

Can anyone help?

jh_dempsey
  • 39
  • 3
  • Hi, @jh_dempsey, do you get a chance to read some doc [here](https://github.com/Autodesk-Forge/forge-api-dotnet-design.automation#getting-started). Hope it helps. – Emma Zhu Aug 26 '20 at 23:37
  • Hi Emma I have seen that document but it doesn't seem to be helping me. Originally I was setting the clientID and secret after trying to create the DesignAutomationClient object, so I moved those lines to before, but it hasn't made a difference. Im not creating a webservice. Im creating a simple Windows Form application, which makes I hard for me to figure out what parts of the getting started I need, and which I don't..... – jh_dempsey Aug 27 '20 at 10:08
  • ``` private async Task createWorkItem() { var forgeConfig = new ForgeConfiguration(); forgeConfig.ClientId = clientID; forgeConfig.ClientSecret = clientSecret; var apiInstance = new DesignAutomationClient(); ``` – jh_dempsey Aug 27 '20 at 10:09

1 Answers1

0

We need more information to troubleshoot, is it a ASP .NET core? how you are handling DI but if your app is .NET core console app from the code as it appears. The right way to do is.

dotnet new console
dotnet add package Autodesk.Forge.DesignAutomation --version 3.0.3

Code:

namespace daconsole
{
    using Autodesk.Forge.Core;
    using Autodesk.Forge.DesignAutomation;
    using Autodesk.Forge.DesignAutomation.Model;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Hosting;
    using Microsoft.Extensions.Options;
    using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading;
    using System.Threading.Tasks;

    /// <summary>
    /// Defines the <see cref="ConsoleHost" />.
    /// </summary>
    class ConsoleHost : IHostedService
    {
        /// <summary>
        /// The StartAsync.
        /// </summary>
        /// <param name="cancellationToken">The cancellationToken<see cref="CancellationToken"/>.</param>
        /// <returns>The <see cref="Task"/>.</returns>
        public Task StartAsync(CancellationToken cancellationToken)
        {
            return Task.CompletedTask;
        }

        /// <summary>
        /// The StopAsync.
        /// </summary>
        /// <param name="cancellationToken">The cancellationToken<see cref="CancellationToken"/>.</param>
        /// <returns>The <see cref="Task"/>.</returns>
        public Task StopAsync(CancellationToken cancellationToken)
        {
            return Task.CompletedTask;
        }
    }

    /// <summary>
    /// Defines the <see cref="App" />.
    /// </summary>
    internal class App
    {
        /// <summary>
        /// Defines the api.
        /// </summary>
        public DesignAutomationClient api;

        /// <summary>
        /// Defines the config.
        /// </summary>
        public ForgeConfiguration config;

        /// <summary>
        /// Initializes a new instance of the <see cref="App"/> class.
        /// </summary>
        /// <param name="api">The api<see cref="DesignAutomationClient"/>.</param>
        /// <param name="config">The config<see cref="IOptions{ForgeConfiguration}"/>.</param>
        public App(DesignAutomationClient api, IOptions<ForgeConfiguration> config)
        {
            this.api = api;
            this.config = config.Value;
        }

        /// <summary>
        /// The CreateWorkItem.
        /// </summary>
        /// <returns>The <see cref="Task"/>.</returns>
        private async Task CreateWorkItem()
        {
            //step1:
            var forgeEnginesApi = api.EnginesApi;
            ApiResponse<Page<string>> engines = await forgeEnginesApi.GetEnginesAsync();
            if (engines.HttpResponse.IsSuccessStatusCode)
            {
                Console.WriteLine(JsonConvert.SerializeObject(engines.Content, Formatting.Indented));
            }

            //step2:
            Console.WriteLine("\nActiviy Start");
            var activitiesApi = api.ActivitiesApi;
            ApiResponse<Page<string>> activitiesResp = await activitiesApi.GetActivitiesAsync();
            List<string> listOfActivities = new List<string>();

            string activityName = null;
            if (activitiesResp.HttpResponse.IsSuccessStatusCode)
            {
                var page = activitiesResp.Content.PaginationToken;
                activitiesResp.Content.Data.ForEach(e => listOfActivities.Add(e));
                while (page != null)
                {
                    activitiesResp = await activitiesApi.GetActivitiesAsync(page);
                    page = activitiesResp.Content.PaginationToken;
                    activitiesResp.Content.Data.ForEach(e => listOfActivities.Add(e));
                }
                var activities = listOfActivities.Where(a => a.Contains("PlotToPDF")).Select(a => a);
                if (activities.Count() > 0)
                {
                    activityName = activities.FirstOrDefault();
                }

            }

            //step3:
            Console.WriteLine("\nWorkItem Start...");
            var workItemsApi = api.WorkItemsApi;
            ApiResponse<WorkItemStatus> workItemStatus = await workItemsApi.CreateWorkItemAsync(new Autodesk.Forge.DesignAutomation.Model.WorkItem()
            {
                ActivityId = activityName,
                Arguments = new Dictionary<string, IArgument>() {
                              {
                               "HostDwg",
                               new XrefTreeArgument() {
                                Url = "http://download.autodesk.com/us/samplefiles/acad/blocks_and_tables_-_metric.dwg",
                                Verb = Verb.Get
                               }
                              }, {
                               "Result",
                               new XrefTreeArgument() {
                                Verb = Verb.Put, Url = "azure blob storage url",
                                Headers = new Dictionary<string,string>()
                                {
                                    { "Content-Type","application/octet-stream" },
                                    { "x-ms-blob-type","BlockBlob" }
                                }
                               }
                              }
                             }
            });

            Console.Write("\tPolling status");
            while (!workItemStatus.Content.Status.IsDone())
            {
                await Task.Delay(TimeSpan.FromSeconds(2));
                workItemStatus = await workItemsApi.GetWorkitemStatusAsync(workItemStatus.Content.Id);

                Console.Write(".");
            }
            Console.WriteLine(JsonConvert.SerializeObject(workItemStatus.Content, Formatting.Indented));
        }

        /// <summary>
        /// The RunAsync.
        /// </summary>
        /// <returns>The <see cref="Task"/>.</returns>
        public async Task RunAsync()
        {
            await CreateWorkItem();
        }
    }

    /// <summary>
    /// Defines the <see cref="Program" />.
    /// </summary>
    internal class Program
    {
        /// <summary>
        /// The Main.
        /// </summary>
        /// <param name="args">The args<see cref="string[]"/>.</param>
        /// <returns>The <see cref="Task"/>.</returns>
        static async Task Main(string[] args)
        {
            var host = new HostBuilder()
                .ConfigureAppConfiguration(builder =>
                {
                    builder.AddEnvironmentVariables();
                    builder.AddForgeAlternativeEnvironmentVariables();
                }).ConfigureServices((hostContext, services) =>
                { // add our no-op host (required by the HostBuilder)
                    services.AddHostedService<ConsoleHost>();

                    // our own app where all the real stuff happens
                    services.AddSingleton<App>();

                    // add and configure DESIGN AUTOMATION
                    services.AddDesignAutomation(hostContext.Configuration);
                    services.AddOptions();
                })
                .UseConsoleLifetime()
                .Build();
            using (host)
            {
                await host.StartAsync();
                // Get a reference to our App and run it
                var app = host.Services.GetRequiredService<App>();
                await app.RunAsync();
                await host.StopAsync();
            }
        }
    }
}

add Forge Env to your launchSettings.json

{
  "profiles": {
    "daconsole": {
      "commandName": "Project",
      "environmentVariables": {
        "FORGE_CLIENT_SECRET": "",
        "FORGE_CLIENT_ID": ""
      }
    }
  }
}

To run:

dotnet run --launch-profile daconsole
Madhukar Moogala
  • 635
  • 1
  • 5
  • 11
  • No web server, web services, or ASP involved. Its a standalone windows form application written purely in c# The user presses a button on the form which runs a function. That function is the code you see in the OP. The function should create a new instance of the API, then use that to create a new work item – jh_dempsey Aug 27 '20 at 15:20