How to get data from Business Central?
I have made a C#-project where I am trying to connect to my Business Central cloud sandbox.
In Business Central, I have made an extension with a new codeunit, which has a function that I call "MHSTest". It just returns a text.
In Visual Studio, I have made the service reference that connects to the SOAP URL in Business Central. So I can see the name of my function, "MHSTest", in Visual Studio.
When I write this in Visual Studio, I get an error, because it tries to connect anonymously:
var client = new MHSTest.CSharpCodeunit_PortClient();
myTextBox.Text = client.MHSTest();
If I try the below instead, I get an error message saying that "https" is not valid. It has to be "http".
string endpoint = "">api.businesscentral.dynamics.com/.../CSharpCodeunit";
var binding = new BasicHttpBinding();
binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
binding.MaxBufferSize = 999999;
binding.MaxReceivedMessageSize = 999999;
var client = new MHSTest.CSharpCodeunit_PortClient(binding, new EndpointAddress(endpoint));
myTextBox.Text = client.MHSTest();
How do I connect to Business Central and get (or set) data?
App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="Debitorer_Binding">
<security mode="Transport" />
</binding>
<binding name="Debitorer_Binding1" />
<binding name="CSharpCodeunit_Binding">
<security mode="Transport" />
</binding>
<binding name="CSharpCodeunit_Binding1" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="api.businesscentral.dynamics.com/.../2999befa-1255-4304-a79a-67de0e24f090/Sandbox/WS/CRONUS%20Danmark%20A%252FS/Page/Debitorer?tenant=msneua5672t92712555&aid=FIN"
binding="basicHttpBinding" bindingConfiguration="Debitorer_Binding"
contract="BCCustomer.Debitorer_Port" name="Debitorer_Port" />
<endpoint address="api.businesscentral.dynamics.com/.../2999befa-1255-4304-a79a-67de0e24f090/Sandbox/WS/CRONUS%20Danmark%20A%252FS/Codeunit/CSharpCodeunit?tenant=msneua5672t92712555&aid=FIN"
binding="basicHttpBinding" bindingConfiguration="CSharpCodeunit_Binding"
contract="MHSTest.CSharpCodeunit_Port" name="CSharpCodeunit_Port" />
</client>
</system.serviceModel>
</configuration>
Solution 1:
The problem is solved. I just needed to add this code after defining my client variable:
System.ServiceModel.Security.UserNamePasswordClientCredential cre = client.ClientCredentials.UserName;
cre.UserName = " "; cre.Password = " ";