Could not load type RestSharp.RestClientOption from assembly RestSharp

Could not load type RestSharp.RestClientOption from assembly RestSharp

esatis
Advocate Advocate
2,335 Views
7 Replies
Message 1 of 8

Could not load type RestSharp.RestClientOption from assembly RestSharp

esatis
Advocate
Advocate

Hi, 

I am submitting app to Autodesk App store and using RestSharp to check for User Entitlement (as suggested by Autodesk). Everything works fine on my PC but when Autodesk creates installer and I launch the addin from Revit it throws an error:
Revit encountered a Could not load type
'RestSharp.RestClientOptions' from assembly 'RestSharp,
Version: 106.12.00, Culture: neutral,
PublicKeyToken=598062e77ß15f75'.

Any ideas why this is happening? 

Do I have to pack RestSharp.dll together with my addin.dll ?

Could not find any information on the web.. 

Accepted solutions (1)
2,336 Views
7 Replies
Replies (7)
Message 2 of 8

jeremy_tammik
Alumni
Alumni
Accepted solution

First of all, check whether Revit.exe has already loaded RestSharp on its own. If it has, you can reuse the same version as Revit uses. In that case, you do not need to load your own version. The error message may be caused because you are trying to load a different version, and it clashes with the Revit one.

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
Message 3 of 8

esatis
Advocate
Advocate

Hi,

I just found, that Revit installation contains the same RestSharp.dll version that I use: 

106.12.0.0

esatis_0-1674570208224.png

So I need to uninstall my RestSharp reference from VS project and use this one?

 

 

0 Likes
Message 4 of 8

jeremy_tammik
Alumni
Alumni

No. Keep your reference, and ensure that the Copy Local flag is set to False in its properties.

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 5 of 8

esatis
Advocate
Advocate
Thank you @jeremy_tammik !
The issue was that I used newer version of RestSharp - 108.5.0.0
Had to rewrite the options part. Now it works on my PC as before, hopefully the Autodesk installer version will also work!
Message 6 of 8

ricaun
Advisor
Advisor

Is a simple REST API so you could use System.Net.WebClient and DownloadString to make the request.

 

 

public string GetString(string address)
{
    using (var client = new System.Net.WebClient())
    {
        return client.DownloadString(address);
    }
}

 

 

And use the Newtonsoft.Json that is already included in Revit.

 

I have created a simple implementation class AppEntitlement and a Command to test.

 

[Transaction(TransactionMode.Manual)]
public class CommandAppEntitlement : IExternalCommand
{
    public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elementSet)
    {
        UIApplication uiapp = commandData.Application;

        var appid = "86934581652450005802";
        var userid = uiapp.Application.LoginUserId;
        var userEntitlement = new AppEntitlement(appid);

        var response = userEntitlement.GetEntitlement(userid);
        if (response != null)
        {
            var text = "AppId: " + response.AppId + "\n";
            text += "UserId: " + response.UserId + "\n";
            text += "IsValid: " + response.IsValid + "\n";
            text += "Message: " + response.Message;

            TaskDialog.Show("AppEntitlement", text);
        }

        return Result.Succeeded;
    }
}

 

 

Here is the full AppEntitlement class: https://gist.github.com/ricaun/e86e63c42bd1a5add1f8d08d6fa84aff

 

 

Luiz Henrique Cassettari

ricaun.com - Revit API Developer

AppLoader EasyConduit WireInConduit ConduitMaterial CircuitName ElectricalUtils

Message 7 of 8

esatis
Advocate
Advocate
Hi,
nice solution, but it seems to have similar problem to using RestSharp.
Different Revit versions use different Newtonsoft.Json versions.
I found out the hard way, that RestSharp versions shipped with Revit change even between Revit updates in same year.
For example- Revit 2021.1.70.21 uses different RestSharp version than Revit 2021.1.109 ....

Can this happen to Newtonsoft.Json ?
0 Likes
Message 8 of 8

ahmed.elhawary73
Advocate
Advocate

I tried your code it works well but it seems that it is not checking for internet connection or even for user login for autodesk or he bought the add-in or not please advice?

0 Likes