I am trying to create a SOAP service with code unit and performing a simple scenario of creating sales order header. I am unable to create sales order as I am calling my API from console application. Error when I run my console app code:
An unhandled exception of type 'System.ServiceModel.Security.MessageSecurityException' occurred in mscorlib.dll
Additional information: The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'Basic realm="", Bearer authorization_uri="https://login.windows.net/......75668f36-65d37.../oauth2/authorize"'.
If I run same code unit on a trigger it works fine
Here is my code
Code unit:
codeunit 50131 SalesOrderCodeUnit
{
trigger OnRun()
begin
Create();
end;
[ServiceEnabled]
procedure Create()
var
SalesHeader: Record "Sales Header";
Document_Type: Enum "Sales Document Type";
Sell_to_Customer_No: Code[20];
Sell_to_Customer_Name: Text[100];
begin
Document_Type := SalesHeader."Document Type"::Order;
Sell_to_Customer_No := '30000';
Sell_to_Customer_Name := 'School of Fine Art';
Clear(SalesHeader);
SalesHeader.Init();
SalesHeader.Validate("Document Type", SalesHeader."Document Type"::Order);
SalesHeader.Validate("Sell-to Customer No.", Sell_to_Customer_No);
SalesHeader.Validate("Sell-to Customer Name", Sell_to_Customer_Name);
SalesHeader.Insert(true);
end;
}
console app code:
ServiceReference1.SalesOrderCodeUnit_PortClient portClient = new ServiceReference1.SalesOrderCodeUnit_PortClient();
portClient.ClientCredentials.UserName.UserName = "DANYAL.BUTT";
portClient.ClientCredentials.UserName.Password = "....qInECuJqR2Q........";
portClient.Create();
Console.WriteLine();