Problem in BC using my own DLL?

Hello guys,

I am writing here, because we have one problem occurring whatever we try to do:

The Problem:
Cannot create an instance of the following .NET Framework object: assembly XXXX, Version=1.0.0.0, Culture=neutral, PublicKeyToken=97113f8bddac1f33, type XXXX.ClassA

* It triggers in the function below

I made my own .NET Framework class library and generated XXXX.dll.

I made everything in my BC app:

In Codeunit:
dotnet
{
assembly(XXXX)
{
version = '1.0.0.0';
culture = 'neutral';
PublicKeyToken = '97113f8bddac1f33';

type(XXXX.ClassA; ClassA)
{ }
}
}

as well as in function:
procedure CallRESTWebService()
var
ClassAVar: DotNet ClassA;
begin
UnicarsSetup.Get();

ClassAVar:= ClassAVar.ClassA(); //triggers the problem
//...

end;

Also in settings.json:
"al.assemblyProbingPaths": [
"./.netpackages",
"C:/Program Files/Microsoft Dynamics 365 Business Central/180/Service/Add-ins"]

* The dll security settings are in Full Control it is uploaded in each path included in settings.json file
* Service is restarted every time
* as you can see in above code I have publicKey, Version.

I tried with making the Constructor of the class, not using the default. The app can be published without problems.

I would be grateful for any kind of help!

Answers

  • txerifftxeriff Member Posts: 444
    edited 2021-12-21
    I think running dlls in BC on cloud is no longer supported. I think there are some workarounds but no idea, as I dont use that version.
    I recall you have to instance them in NAV as : metapackConstructor:=metapackConstructor.MetaPackProgram();

    Anyway I can see you are on prem (because the addins path).

    however, if you want to call a WS, and you have access to AL, I suggest you do this way:
    procedure CallService(ProjectName: Text; RequestUrl: Text; RequestType: Integer; payload: Text; Username: Text; Password: Text; var HttpReturnedStatuscode: Integer): Text
        var
            Client: HttpClient;
            RequestHeaders: HttpHeaders;
            RequestContent: HttpContent;
            ResponseMessage: HttpResponseMessage;
            RequestMessage: HttpRequestMessage;
            ResponseText: Text;
            contentHeaders: HttpHeaders;
        begin
            RequestHeaders := Client.DefaultRequestHeaders();
            RequestHeaders.Add('Authorization', CreateBasicAuthHeader(Username, Password));
            case RequestType of
                0:
                    Client.Get(RequestURL, ResponseMessage);
                1:
                    begin
                        RequestContent.WriteFrom(payload);
                        RequestContent.GetHeaders(contentHeaders);
                        contentHeaders.Clear();
                        contentHeaders.Add('Content-Type', 'application/json');
                        RequestMessage.Content := RequestContent;
    
                        RequestMessage.SetRequestUri(RequestURL);
                        // RequestMessage.Method := 'PATCH';
                        RequestMessage.Method := 'PUT';
                        client.Put(RequestURL, RequestContent, ResponseMessage);
                    end;
                2:
                    begin
                        RequestContent.WriteFrom(payload);
                        RequestContent.GetHeaders(contentHeaders);
                        contentHeaders.Clear();
                        contentHeaders.Add('Content-Type', 'application/json');
    
                        Client.Post(RequestURL, RequestContent, ResponseMessage);
                    end;
                3:
                    Client.Delete(RequestURL, ResponseMessage);
            end;
    
            ResponseMessage.Content().ReadAs(ResponseText);
            HttpReturnedStatuscode := ResponseMessage.HttpStatusCode;
    
            if ResponseMessage.HttpStatusCode <> 200 then begin
                if StrPos(ResponseText, '"message"') <> 0 then
                    ResponseText := CopyStr(ResponseText, StrPos(ResponseText, '"message"') + 10, StrLen(ResponseText));
                /*if GuiAllowed then
                    Error(strsubstno(Text50004, ResponseMessage.HttpStatusCode, ResponseText, payload))
                else begin
                    LogErrors(ResponseText);
                end;
                */
                //Error(strsubstno(Text50004, ResponseMessage.HttpStatusCode, ResponseText, payload))
                if GuiAllowed then
                    Message((strsubstno(Text50004, ResponseMessage.HttpStatusCode, ResponseText, payload)));
    
            end else
                exit(ResponseText);
        end;
    

    if you dont, you can use winhttp automation/dotnet.

    Name DataType Subtype Length
    WinHTTP Automation 'Microsoft WinHTTP Services, version 5.1'.WinHttpRequest
    
    IF ISCLEAR(WinHTTP) THEN
      CREATE(WinHTTP,TRUE,TRUE);
    
    WinHTTP.Open('POST',T50161."PayPal URL"+'oauth2/token', FALSE);
    
    //you can put here any headers you require, first use postman to test it etc.
    WinHTTP.SetRequestHeader('Accept', 'application/json');
    WinHTTP.SetRequestHeader('Accept-Language', 'en_US');
    WinHTTP.SetRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    WinHTTP.SetRequestHeader('Authorization', STRSUBSTNO('Basic %1', T50161."Base64 Client and Secret"));
    WinHTTP.Send('grant_type=client_credentials');
    
    vInStream := WinHTTP.ResponseStream;
    REPEAT
      vInStream.READTEXT(vtext);
      vBigT.ADDTEXT(vtext);
    UNTIL (vInStream.EOS);
    
    

Sign In or Register to comment.