'calling c# dll which connects to a webservice from delphi, but the config file is not being used
I have to write an dll in c# which connects to a webservice, reads some values from that webservice und populates a databasetable with it. The dll should than be used by a delphi application.
I have managed to use the DLLExport-Attribute to publish the functions inside my dll and make delphi able to use them. The problem seems to be, that the app.config (where the information for the webservice seems to come from) is not being used when i call my functions from delphi.
C# DLL:
namespace SomeDLL
{
[DllExport]
public static bool GetClients(int userId, [MarshalAs(UnmanagedType.LPWStr)] string url)
{
try
{
ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);
using (var client = new SomeWebserviceClient())
using (var scope = new OperationContextScope(client.InnerChannel))
{
var someData = client.GetSomeData();
//Insert data to db
}
return true;
}
catch (Exception e)
{
MessageBox.Show($"{e.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
}
I can use this dll inside an C# consoleapplication like this:
class Program
{
static void Main(string[] args)
{
try
{
var id = 1;
var url = "bla";
SomeDLL.GetClients(id, url);
}
catch (Exception e)
{
Console.WriteLine($"Exception: {e}");
}
finally
{
Console.ReadKey();
}
}
}
This works fine so far. But when i try to call this function from delphi like this:
function GetClients(userId: Integer; url: String) : Boolean; stdcall; external 'SomeDLL.dll';
procedure TForm1.Button1Click(Sender: TObject);
var bResult: Boolean;
begin
try
bResult := GetClients(1, 'bla');
if bResult
then RichEdit1.Text := 'true'
else RichEdit1.Text := 'false';
except on e: Exception do
RichEdit1.Text := e.Message;
end;
end;
I get the error
Could not find endpoint element with name and contract
This is the same error i get from my console application if i delete the app.config (which i copied from my dll-project) from the console project. I have already tried to copy the SomeDLL.dll.config which c# created together with the someDLL.dll file into the directory where the delphi application is located, but it seems like it still fails to use the config file.
If i delete the part of my dll where the webservice is used, there are no errors. So i guess the problem really comes down to the webservice not being able to work correctly without the app.config.
Any ideas how i can make this work?
Solution 1:[1]
So i have found a solution. Not for the problem of my question, but for the underlying problem...
The problem was, that delphi was not able to import the wsdl file, because the file itself was import datatypes from other xsd files. Delphi would create some methods, but alle datatypes from the xsd files were not present. The delphi team already tried to use this WSDLMerge-Tool to merge the xsd-files into the wsdl file. But they reported, that it didn't work.
I downloaded the Tool myself and tried to use it. The tool seems to run without problems when using a local file. But the merged file was the same as the inputfile. So no merge has been done.
I was able to use it correctly by setting the targetframework to .net Framework 4.6.2 and using the webadress for the wsdl file. I still got an errormessage, that there is some conflict with ssl-certificates. I added this:
ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);
line to ignore the ssl certificate validation. Running the tool now works again and the files are correctly merged. I was able to import the new merged wsdl-file in delphi and everything works now. I sent the merged wsdl file to the delphi team and they can now use the webservice on their own...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Olli |
