[Bug] NullReferenceException in SocketConnectionInitiator for UnixDomainSocket when cannot connect to server instead of meaningful error
- Dominant language
- C#
- Stars
- 1.8k
- Forks
- 576
- Avg merge
- 6d 9h
- Merged PRs (30d)
- 2
Description
**Describe the bug**
In `SocketConnectionInitiator` when cannot connect to server `SocketException` occurs, but due to incorrect handling instead of processing that exception `NullReferenceException` is thrown which is confusing
`lastException` is null if there is any exception in `CreateConnectionAsync`:

So further in ConvertConnectException we shouldn't check for `lastException` or check if it's not null and check `innerException` instead:

**To Reproduce**
Steps to reproduce the behavior:
1. Run projects in attached solution(server and client)
[ExceptionSample.zip](https://github.com/dotnet/wcf/files/14417654/ExceptionSample.zip)
2. Press enter in client project to start call
3. See `NullReferenceException` when using invalid path instead of more meaningful error
```
Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
at System.ServiceModel.Channels.SocketConnectionInitiator.ConvertConnectException(SocketException socketException, Uri remoteUri, TimeSpan timeSpent, Exception innerException)
at System.ServiceModel.Channels.SocketConnectionInitiator.ConnectAsync(Uri uri, TimeSpan timeout)
at System.ServiceModel.Channels.BufferedConnectionInitiator.ConnectAsync(Uri uri, TimeSpan timeout)
at System.ServiceModel.Channels.ConnectionPoolHelper.EstablishConnectionAsync(TimeSpan timeout)
at System.ServiceModel.Channels.ClientFramingDuplexSessionChannel.OnOpenAsync(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.OnOpenAsyncInternal(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.System.ServiceModel.IAsyncCommunicationObject.OpenAsync(TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.OnOpenAsync(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.OnOpenAsyncInternal(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.System.ServiceModel.IAsyncCommunicationObject.OpenAsync(TimeSpan timeout)
at System.Runtime.TaskHelpers.WaitForCompletion(Task task)
at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.Open()
at System.ServiceModel.Channels.ServiceChannelProxy.System.ServiceModel.ICommunicationObject.Open()
at Program.$(String[] args) in C:\Users\user\Desktop\ExceptionSample\ExceptionSample\Program.cs:line 15
```
Video demo:
https://github.com/dotnet/wcf/assets/71182505/cf649b99-29c1-4804-b9ef-5b426e952a9f
**Expected behavior**
NullReferenceException shouldn't happen and SocketException should be processed in some way.
**Screenshots**
If applicable, add screenshots to help explain your problem.
Exception in https://github.com/dotnet/wcf/blob/main/src/System.ServiceModel.UnixDomainSocket/src/System/ServiceModel/Channels/SocketConnection.cs#L792

**Additional context**
Add any other context about the problem here.
Contributor guide
Assessment
This issue has not been assessed yet.