Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Unanswered

Create sales order SOAP service

(0) ShareShare
ReportReport
Posted on by 259

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();

  • Danyal Profile Picture
    259 on at
    RE: Create sales order SOAP service

    I am using SaaS and yes web service is OK.

    Thank you for the response, I actually solved this issue by adding below line in my .net method

    portClient.ClientCredentials.Windows.AllowedImpersonationLevel =

                        System.Security.Principal.TokenImpersonationLevel.Impersonation;

    and a change in app.config by

    <basicHttpBinding>

                   <binding name="SalesOrderCodeUnit_Binding">

                     <security mode="Transport">

                       <transport clientCredentialType="Basic"></transport>

                     </security>

                   </binding>

                   <binding name="SalesOrderCodeUnit_Binding1" />

    </basicHttpBinding>

  • Lars Lohndorf-Larsen Profile Picture
    on at
    RE: Create sales order SOAP service

    Hi,

    Is NAV / BC running OnPrem or in SaaS? Meaning are you able to change the authentification methods. And if you are using Visual Studio to create your console app, do you see the web service OK from there?

    Sorry to only have questions for now.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
Sohail Ahmed Profile Picture

Sohail Ahmed 1,200

#2
YUN ZHU Profile Picture

YUN ZHU 1,006 Super User 2025 Season 1

#3
Mansi Soni Profile Picture

Mansi Soni 836

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans