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)...

  Message=One or more errors occurred.
   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;  

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...

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... 

Wednesday, October 30, 2013

Whats new in VS 2013 - Peek Definition

Along with some other awesome features of VS 2013. One of my favorites is peek definition.

This feature should have been a part of Visual Studio since day one, but since it was not there, no one really asked about it.

So here is how it really works.

In the previous versions of Visual Studio we had to use the "Go to Definition (F12)"  menu option on the right click menu or the context menu to view the code of some other function, but the problem with it was that the current view was updated and the developer had to track back to the previous location using 'CTRL' + '-' key or maybe closing the file in case the function was in another file

Now VS 2013 gives an option to peek into the definition of another function by opening another smaller window on top of the current code editor window to peek into the code.

Right Click Menu for Peek

Peek Window

Here, the developer can not only view the function but can also see other functions in the same code file by scrolling up and down in the same code file. A developer can also do all sorts of tasks that one can think of in a code editor window, because it is a fully functional code editor window itself.

The developer can get rid of that window by simply pressing the "escape" key.

Monday, October 28, 2013

What's new in Visual Studio 2013 - Hanselman Blog Post

I wanted to shed some light on this, but Mr. Scott Hanselman already did this and out of respect only I am going to share only the link of his blog where he has shared the videos of the new features of Visual Studio .Net 2013.

Here is a link to his blog, which shows top new features in Visual Studio 2013.


Enjoy the videos and enjoy the new features of Visual Studio .Net 2013

Friday, October 25, 2013

Converting Comma separated values to Temporary Table in T-SQL

Today, I want to share a technique that we have to use often in SQL while using with .Net. We have to pass a comma separated string into a Stored Procedure and use it as a table (somehow) to manipulate the results.

Since we had to pass the comma separated string to a stored procedure many times. We created a SQL Function for it and then we only had to call it.

Here is how the code for the T-SQL function looks like...

CREATE FUNCTION [dbo].[GetIDsTableFromIDsList]
      @IDsList VARCHAR(MAX)
        DECLARE @Pos 

        SET @IDsList = LTRIM(RTRIM(@IDsList)) + ','
        SET @Pos = CHARINDEX(',', @IDsList, 1)

        IF REPLACE(@IDsList, ',', '') <> ''
                WHILE @Pos > 0
                        SET @ID = LTRIM(RTRIM(LEFT(@IDsList, @Pos - 1)))
                        IF @ID <> ''
                                INSERT  INTO @IDsTable
                                        ( [ID] )
                                VALUES  ( CAST(@ID AS INT) )
                        SET @IDsList = RIGHT(@IDsList, LEN(@IDsList) - @Pos)
                        SET @Pos = CHARINDEX(',', @IDsList, 1)


This function receives a simple comma separated list of IDs and loops over the list and then after inserting all the values into a temporary table. It returns the temporary table