Pages

Tuesday, October 16, 2018

HttpClient, Web API REST service and self signed Certificates

I recently came up with the problem where my web API was hosted locally on IIS using self signed certificate. 

The same code was working absolutely fine in Production, but for local, it was throwing exception because locally I had installed a self signed IIS Express certificate.

Here is the exception that I received (in full)...

System.AggregateException
  HResult=0x80131500
  Message=One or more errors occurred.
  Source=mscorlib
  StackTrace:
   at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
   at ..............
   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass13.b__c(Object instance, Object[] methodParameters)
   at System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func`1 func, CancellationToken cancellationToken)
Inner Exception 1:
HttpRequestException: An error occurred while sending the request.
Inner Exception 2:
WebException: The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.
Inner Exception 3:
AuthenticationException: The remote certificate is invalid according to the validation procedure.

The only help that I got from the exception was from Inner Exception 3: AuthenticationException: The remote certificate is invalid according to the validation procedure.

So, to look into the details, I realized that the Self Signed Certificate from my local IIS is causing the problem. After a bit more googling I came up with this solution.


 #if DEBUG  
         //Accept all server certificate  
         ServicePointManager.ServerCertificateValidationCallback =  
           delegate (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)  
           {  
             return true;  
           };  
 #endif  

Notice the #if DEBUG to make sure that the self signed certificate only works in case of local development environment only.

This sorted out the issue as now, I was bypassing the Certificate check using my own delegate.

Monday, October 1, 2018

Referencing .NET 4.5 dll in .NET Core csproj

Hi All,

After a long time, I realized that I need to share something with the outer world.

Since we are inside the .Net core world and things are moving fast towards making the .Net 4 and pre .Net 4 obsolete. Let's try something that do not have much on the internet.


I have a .Net 4.5.2 application. I had to create a new console application in the solution to do some scheduling (not windows service, obviously). I went ahead and took the pleasure of doing it .Net Core. The latest version by the time of this writing is .Net core 2.1.

I stumbled upon a series of problems with .Net Core, so I thought it might be helpful for others as well, if I posted about those here...

When I created a new project and added a reference for .Net 452 project inside the console application, it went smooth and everything built alright, but as soon as I tried to run the console application it started crashing on the first .Net 452 reference inside the project structure. In my case, it was "System.Configuration".

It was a default error saying... 
System.IO.FileNotFoundException: 'Could not load file or assembly _ _ _. The system cannot find the file specified.'
So, what do I do, I went to Google and tried to find the problem, the first post that I could find was this one by Scott Hanselmann.

But this was not my problem, as this post was related to Nuget Packages, not related to normal assembly reference. After a little more searching I was able to find this StackOverflow Post. which is quite close to my problem.

I only had to modify the CSProj and set the TargetFramework to multiple frameworks as the VS 2017 UI does not allow me to do that...

So, I had to go to another editor or unload the project from visual studio and edit the csproj file. But I prefer VS Code, which is super fast to load and very good tool to help with these kind of operations. I would recommend opening both the editors in parallel on a development machine.

Anyways, I had to add to change this tag in the csproj file...
<TargetFrameworks>net452;netcoreapp1.1</TargetFrameworks>

Notice the change from the <targetframework> to <targetframeworks>, an additional 's' for multiple Frameworks support with .Net Core.

One more thing to care about, you might need to give the specific version of .Net Framework that you are using in other projects, like in above example it is 4.5.2

This is it... Happing Coding...