3
votes

I have a c# Framework 4.7.2 Com visible library that calls a web api.

Unit tests in the VS2017 C# IDE library work fine.

However if I try to call via VB6 I get

System.IO.IOException unable to read data from the transport connection: An existing connection was forcibly closed by the rempte host.

System.Net.Sockets.SocketException

I am running Windows 10

I call as administrator

C:\Windows\Microsoft.NET\Framework\v4.0.30319\regasm /codebase /verbose  /tlb:SBD.ComBridge.tlb C:\dev\SBD.ComBridge.dll 

to create the .dll and .tlb

I also tried running regasm from the VS2017 command prompt as administrator

RegAsm reports that the libraries register successfully.

In Vb6 the (simplified) code is

Dim o As SBD_ComBridge.BridgeImplementation
Set o = New dBridgeImplementation
o.SetOrderDates id 
set o = nothing

In BridgeImplementation the (simplified) code is

[DispId(25)]
[ComVisible(true)]
public void SetOrderDates(int Id)
{
     PackAndSend.SetReadByInfo(Id) // calls freight service
}

I know that the code calling the service from within SetReadyByInfo works because my unit test passes when I run it in VS2017

Unfortunately I have been asked not to post the code. However I know that the vb6 code calls Com correctly because there are other methods I call without errors.

I had a similar issue with MYOB api and TLS the solution was to upgrade the Framework. However I can't upgrade the VB6 framework ( the large re-write is not an option) Probably I will just make a .Exe and shell out to it.

[Update]

The link Simon Mourier gave me solves it. If I add

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

then my com call works on VB6

As the link points out,

This isn't a good solution since it hard codes what TLS version to use, so it wouldn't use TLS 1.3 in future

1
Going to have to show some code, both the calling VB6 code and probably the .NET side too.tcarvin
Is PackAndSend.SetReadByInfo a blocking call? It is a .NET exception so I think the next set of code to post is that.tcarvin
As you found out, TLS is probably the issue. But you shouldn't have to upgrade anything if the machine and the installed .NET framework support TLS 1.2 (which seems to be the case since you say it works with other programs) Your VB6 exe is probably not using the good .NET Framework (there can be more than one installed on a given machine), or not using the configuration that works. Check out the answers here, for example: stackoverflow.com/a/44765698/403671 (adapt to your context)Simon Mourier
Based on @SimonMourier links, you could probably just use code to explicitly set ` ServicePointManager.SecurityProtocol` as desired. You could try the config file route, but since VB6 is the host app, I'm not sure a config-file based approach will work.tcarvin
Thanks :-) If you found the solution, you should answer yourself with some details of what you did to make it work.Simon Mourier

1 Answers

0
votes

As per the update section at the bottom of my question, a work around is to include

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

I need to investigate further for a more complete solution