简体   繁体   中英

Forge .NET API RuntimeBinderException

I'm using Forge .NET API 1.3.0

I'm trying to get hubs via API, just like it is described in the example

I know that...

  • My authentication works
  • Query results correct data

But for some reason the following line fails with RuntimeBinderException:

Hubs hubs = apiInstance.GetHubs(/*filterId, filterExtensionType*/);

The exception message says:

Exception thrown: 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' in Unknown Module. Additional information: Cannot implicitly convert type 'Autodesk.Forge.Model.DynamicJsonResponse' to 'Autodesk.Forge.Model.Hubs'

So obviously the method example shows is not valid anymore (?). How should the resulting data to be converted into Hubs type.

OK, I managed to get it working by using the raw data. Following an example:

var hubs = await hubsApi.GetHubsAsync();
foreach (KeyValuePair<string, dynamic> hubInfo in new DynamicDictionaryItems(hubs.data))
{
  new { Id = hubInfo.Value.id, Name = hubInfo.Value.attributes.name }
}

For a reference, following kind of data is inside the "hubs.data" array:

{
"type": "hubs",
"id": "b.aaaaaaaa-bbbb-cccc-1111-222223333333",
"attributes": {
    "name": "The hub",
    "extension": {
        "type": "hubs:autodesk.bim360:Account",
        "version": "1.0",
        "schema": {
            "href": "https://developer.api.autodesk.com/schema/v1/versions/hubs:autodesk.bim360:Account-1.0"
        },
        "data": {}
    }
},
"links": {
    "self": {
        "href": "https://developer.api.autodesk.com/project/v1/hubs/b.aaaaaaaa-bbbb-cccc-1111-222223333333"
    }
},
"relationships": {
    "projects": {
        "links": {
            "related": {
                "href": "https://developer.api.autodesk.com/project/v1/hubs/b.aaaaaaaa-bbbb-cccc-1111-222223333333/projects"
            }
        }
    }
}

}

.NET 教程展示了如何列出集线器,您还可以下载即用型示例DataManagementController代码显示了它的实际效果, 请参见此处

This happened to me as well. I solved it using Json Deserializer.

        string token = "a token";
        var apiInstance = new HubsApi();
        apiInstance.Configuration.AccessToken = token;

        // Get Hub
        dynamic hubsJson = apiInstance.GetHubs();
        Hubs allHubs = JsonConvert.DeserializeObject<Hubs>(hubsJson.ToString());

Now the json object is converted to the Forge Api Hubs class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM