[英]Consuming a HTTPS service in a c# class library using Com and Delphi
我有一个要使用的https Web服务。
在C#中,我可以用大约10行代码来实现。
在Delphi XE5中,这令人头疼,而且证书存储和加密也一团糟。 我需要能够在Delphi XE5中使用它。
我的想法是将(简单)调用粘贴到C#类库中的Web服务,然后使用CreateOleObject
通过Delphi实例化它。 该类库是regasm library.dll /codebase
和gacutil -i library.dll
我的问题似乎是,当使用C#来使用Web服务时,它在很大程度上依赖于端点和存储在app.config文件中的绑定设置。 问题是,因为我使用的是Delphi和COM,所以此配置文件的读取出现了问题(我得到了错误: Could not find endpoint element with name...
)。
我尝试了MyDelphiTestApp.exe.config
和library.dll.config
各种排列, MyDelphiTestApp.exe.config
它们似乎都无法读取(从其中包含正确数据的类库项目中复制app.config
文件)。 我已经在运行时调试了C#类库,以查看配置文件在哪里,然后确保那里没有配置文件,但是仍然失败。
谁能给我解决方案或向正确的方向推进?
编辑:5月30日。 感谢你目前的帮助。 我的代码当前位于:
var binding = new WSHttpBinding(SecurityMode.TransportWithMessageCredential);
binding.SendTimeout = new TimeSpan(0, 0, 0, 0, 100000);
binding.OpenTimeout = new TimeSpan(0, 0, 0, 0, 100000);
binding.MaxReceivedMessageSize = 10000;
binding.ReaderQuotas.MaxStringContentLength = 10000;
binding.ReaderQuotas.MaxDepth = 10000;
binding.ReaderQuotas.MaxArrayLength = 10000;
X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection certs = store.Certificates.Find(X509FindType.FindByThumbprint, "XXXXXXXXXXXXXXXXXXXXXXXXXX", false);
store.Close();
if (certs.Count <= 0)
return "Could not find certificate";
AddressHeader addressHeader1 = AddressHeader.CreateAddressHeader("service1", "https://wibble.com/Service", 1);
AddressHeader[] addressHeaders = new AddressHeader[1] { addressHeader1 };
AddressHeaderCollection addressHeaderColl = new AddressHeaderCollection(addressHeaders);
var epIdentity = new X509CertificateEndpointIdentity(certs[0]);
EndpointAddress remoteAddress = new EndpointAddress(new Uri("https://wibble.com/Service/DataService.svc"), epIdentity, addressHeaderColl);
AiropsScheduleDataServiceClient client = new AiropsScheduleDataServiceClient(binding, remoteAddress);
var defaultCredentials = client.Endpoint.Behaviors.Find<ClientCredentials>();
client.Endpoint.Behaviors.Remove(defaultCredentials); //remove the default credentials
var loginCredentials = new ClientCredentials();
loginCredentials.UserName.UserName = Username;
loginCredentials.UserName.Password = Password;
client.Endpoint.Behaviors.Add(loginCredentials); //Add new credential to the proxy
ExportScheduleDataRequest request = new ExportScheduleDataRequest();
ExportScheduleDataResponse response = client.ExportScheduleData(request);
...但这给了我一个Delphi客户错误
“无法打开安全通道,因为与远程端点的安全协商失败。这可能是由于在用于创建通道的EndpointAddress中缺少或错误指定了EndpointIdentity。请验证EndpointAddress指定或暗示的EndpointIdentity正确标识了远程端点“
我又迷路了。
我不确定XE5会带来什么麻烦,但是要回答您的问题,您可以在C#代码中执行以下操作以通过代码进行绑定,而不是在app.config文件中进行绑定:
var binding = new WSHttpBinding();
binding.SendTimeout = new TimeSpan(0, 0, 0, 0, 100000);
binding.OpenTimeout = new TimeSpan(0, 0, 0, 0, 100000);
binding.MaxReceivedMessageSize = 10000;
binding.ReaderQuotas.MaxStringContentLength = 10000;
binding.ReaderQuotas.MaxDepth = 10000;
binding.ReaderQuotas.MaxArrayLength = 10000;
// Set the security mode
binding.Security.Mode = SecurityMode.Transport;
var endpoint = new EndpointAddress("http://localhost:57102/MyService.svc");
var myClient = new WebServiceclient(binding, endpoint);
您可以根据需要更改值,并根据需要使用BasicHttpBinding或其他值。
由于这些值在代码中,因此您可以直接从Delphi应用程序中传递它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.