Error not being caught (AsyncCallback) - vb.net

Hi there i have a piece of code which connects using sockets to a server
Try
'Connect To The Server
Dim socketclient As New TcpClient
Dim success
Dim result
result = socketclient.BeginConnect(ip, port, socket.EndConnect, socketclient)
success = result.AsyncWaitHandle.WaitOne(5000, True)
If socketclient.Connected = False Then
Throw New Exception("Server is offline")
End If
catch ex as exception
End Try
As you can see. if a server is offline, my try-catch setup doesn't catch the error "host is unknown" i believe this is because the error occurs during the async call. IF this is true, how does one catch an error in an async call?

You should end the async operation.
Your wait is totally unnecessary, if you just want to block waiting for the connection just call EndConnect over the socket ;)
http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.endconnect.aspx

Related

How to properly close a WCF Netnamedpipebinding Channel?

I have a WCF Netnamedpipebinding from my application to an Office Addin. I have noticed that when the office app is busy doing something else that my application locks up when using a WCF method. I have added an example of my code. It appears that the code stops and waits with the channel.close method.
Is the solution to change channel.close to channel.BeginClose ?
What is the the state object I need to pass in to the BeginClose method?
Public Function RequestPersonStatus(ByVal id As String, ByVal email As String)
Using factory As New ChannelFactory(Of IToOffice)(New NetNamedPipeBinding(), New EndpointAddress("net.pipe://localhost/" + XXXXXX))
Dim OfficeChannel As IToOffice = factory.CreateChannel()
Try
OfficeChannel.RequestPersonStatus(id:=id, email:=email)
Catch ex As Exception
Return False
Finally
CloseChannel(CType(OfficeChannel, ICommunicationObject))
End Try
End Using
Return True
End Function
and the closeChannel
Private Sub CloseChannel(ByVal channel As ICommunicationObject)
Try
If channel.State = CommunicationState.Opened Then
Dim caller As New AsyncCallback(AddressOf callback)
channel.BeginClose(caller, New Object)
' channel.Close()
End If
Catch ex As Exception
Log(LogTypes.AllExceptions, "CloseChannel - Error closing the channel. ", ex.ToString)
Finally
channel.Abort()
End Try
End Sub
There seems to be a lot of discussion on what and when to clean up / Dispose / close down channels. I am just posting here what I am now doing and thus my answer to my question.
Private Sub CloseChannel(ByVal channel As ICommunicationObject)
Try
If channel.State <> CommunicationState.Closed AndAlso channel.State <> CommunicationState.Faulted Then
channel.BeginClose(Sub(asr)
Try
channel.EndClose(asr)
Catch
channel.Abort()
End Try
End Sub, Nothing)
Else
channel.Abort()
End If
Catch commEx As CommunicationException
channel.Abort()
Catch ex As Exception
channel.Abort()
Finally
End Try
End Sub
As far as I know, most of the cases like the timeout error/connection/communication error are caused by that the channel/client proxy is not properly closed. Putting the client proxy/Service Channel in a using block will eliminate the problem.
using (ServiceReference1.ServiceClient client=new ServiceClient())
{
var result = client.Test();
Console.WriteLine(result);
}
The Using statement is useful for automatically closed the service proxy/service communication channel after completing the invocation. Besides, Service client proxy is similar to the communication channel created by the ChannelFactory.
Feel free to let me know if there is anything I can help with.

Exception handling with Async Task functions

I am beginning to learn about Async / Task functions for running cancellable SQL queries in VB.NET.
I have the following code in a class library which runs two tasks and does not have any exception handling, as I would like to handle this in the application that calls the class library. I have the following code in the class library.
Public Async Function DirectoryList(ct As CancellationToken) As Task(Of List(Of Directory))
ct.ThrowIfCancellationRequested()
Dim ds As DataSet
Dim dirs As List(Of Directory)
Dim ctUnregister As CancellationTokenRegistration
ds = Await Task.Run(Function()
Using newConnection = New SqlConnection(Me.InitializationData.ConnectionString)
Using sqlAdapter = New SqlDataAdapter("DirectoryList", newConnection)
ctUnregister = ct.Register(Sub() sqlAdapter.SelectCommand.Cancel())
With sqlAdapter
.SelectCommand.CommandType = CommandType.StoredProcedure
.SelectCommand.CommandTimeout = Me.InitializationData.CommandTimeout
End With
Dim newDataSet As DataSet = New DataSet()
sqlAdapter.Fill(newDataSet)
Return newDataSet
End Using
End Using
' Remove the registration we set earlier so the cancellation token can be used normally.
ctUnregister.Dispose()
End Function, ct)
dirs = Await Task.Run(Function()
Dim dirsResult As New List(Of Directory)
Dim tbl As DataTable = ds.Tables(0)
For Each row As DataRow In tbl.Select()
' process the data
ct.ThrowIfCancellationRequested()
Next
Return dirsResult
End Function, ct)
Return dirs
End Function
I then call it as follows:
Try
dirs = Await databaseLibrary.DirectoryList(cts.Token)
MsgBox("Query complete!")
Catch ex As System.Data.SqlClient.SqlException
MsgBox("Cancelled (SQL)")
Catch ex2 As OperationCanceledException
MsgBox("Cancelled")
Catch ex3 As Exception
MsgBox("Cancelled")
End Try
Functionally it seems to work as expected - I can cancel the requests and exceptions are thrown as expected.
However I would like to handle the exceptions so I can gracefully cancel the tasks, but even when running in Debug mode within the IDE, the app still breaks and the exception (e.g. SqlException) is shown within the IDE. If I step on, eventually the Catch logic runs.
When the application is run outside of the IDE, the exception handling works as expected.
This seems different to the normal behaviour when running in the debugger- normally the exception handling runs but only breaks if the exception is unhandled.
Why is this behaviour different, presumably because of the Async functions?
when running in Debug mode within the IDE, the app still breaks and the exception (e.g. SqlException) is shown within the IDE.
Why is this behaviour different, presumably because of the Async functions?
Exceptions thrown by asynchronous functions are not caught directly by the catch. What actually happens is that the exception is caught by a compiler-generated state machine, and that exception is placed on the returned Task. Later, when that task is awaited, the exception is re-raised, and then can be caught by the catch.
However, this "indirection" in exception handling means that the IDE does kind of freak out when the exception is initially thrown. As far as it can tell, there is no catch in your code that is going to catch it, so that's why it breaks and displays the exception. It doesn't know that the compiler-generated code will catch it. You can tell the IDE not to worry about uncaught exceptions.

vb.net WebClient.DownloadString fails with exception

I am working on a program in VS 2017 coding in VB.Net. The program downloads Web pages using Net.WebClient.DownloadString and then parses the data. It worked fine for a year or more then one day I started getting an exception when downloading the pages.
The ex.Message is: 'The underlying connection was closed: An unexpected error occurred on a send.'
The ex.InnerException.Message is: 'Unable to write data to the transport connection: A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied.'
I have VS 2017 installed on 2 other PC's at this location and on 1 at another location. They all continue to run the code without exception. It is only an issue on this PC.
The code is below (I changed the Web address but it fails for any valid URL).
Any ideas why this fails on my main PC only?
Public Function DownloadData() As Boolean
Dim strURL As String = "https://www.google.com/"
Dim strOutput As String
Try
Using WC As New Net.WebClient
strOutput = WC.DownloadString(strURL)
If strOutput.Length > 0 Then
If ParseData(strOutput) = True Then
Return True
End If
Else
Return False
End If
End Using
Catch ex As Exception
MessageBox.Show(ex.InnerException.Message, "Error")
End Try
End Function

ObjectDisposedException obtaining user name in SignalR event

I'm logging current SignalR connections in a sql server database. Records are added via the OnConnected and OnReconnected events and removed via the OnDisconnected event.
On deploying to our staging server I am now experiencing an intermittent issue fetching the username of the disconnected user.
This is using SignalR v2.0.3
Public Overrides Function OnDisconnected() As Threading.Tasks.Task
Try
Dim username As String = String.Empty
Try
username = Me.Context.User.Identity.Name ' this is where the error occurs
Catch ex As Exception
_logger.LogWarning(ex, EventLogEntryType.Warning)
End Try
Dim connId = Me.Context.ConnectionId
Dim referer = Me.Context.Headers("Referer")
_connService.RemoveConnection(connId)
Dim msg = New With {.username = username, .connectionId = connId, .referer = referer}
Dim hub = GlobalHost.ConnectionManager.GetHubContext(Of IncidentsHub)()
hub.Clients.All.disconnectionEvent(msg)
Catch ex As Exception
_logger.LogWarning(ex, EventLogEntryType.Error)
End Try
Return MyBase.OnDisconnected()
End Function
I have had to wrap the offending line in an additional try/catch and accept that sometimes there will be no username returned.
The exception seen is:
MOAE - System.ObjectDisposedException: Safe handle has been closed
at System.Runtime.InteropServices.SafeHandle.DangerousAddRef(Boolean& success)
at Microsoft.Win32.Win32Native.GetTokenInformation(SafeTokenHandle TokenHandle, UInt32 TokenInformationClass, SafeLocalAllocHandle TokenInformation, UInt32 TokenInformationLength, UInt32& ReturnLength)
at System.Security.Principal.WindowsIdentity.GetTokenInformation(SafeTokenHandle tokenHandle, TokenInformationClass tokenInformationClass)
at System.Security.Principal.WindowsIdentity.get_User()
at System.Security.Principal.WindowsIdentity.GetName()
at System.Security.Principal.WindowsIdentity.get_Name()
at MyProject.IncidentsHub.OnDisconnected() in C:\Projects\...\IncidentsHub.vb:line 95
Can anyone tell me why this is occurring and if there is a more reliable way to obtain this information?

Using AxInterop.MSWinsockLib for socket programming's state stays at 6-Connecting

I am using AxInterop.MSWinsockLib.dll together with Interop.MSWinsockLib.dll and MSWINSCK.OCX to create a socket connection to a printer. The following code is what I use but no matter if I put the code inside a while loop or Thread.Sleep() after the attempt to connect, The connection state still remains at 6, which is "Connecting". Please assist ASAP.
Try
Dim sock As AxMSWinsockLib.AxWinsock
sock = New AxMSWinsockLib.AxWinsock
CType(sock, System.ComponentModel.ISupportInitialize).BeginInit()
Me.Controls.Add(sock)
CType(sock, System.ComponentModel.ISupportInitialize).EndInit()
sock.RemoteHost = "10.194.1.132"
sock.RemotePort = 3001
sock.Connect()
MessageBox.Show(sock.CtlState.ToString())
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try