Friday, July 25, 2014

Ignore SSL Certificate Check in PowerShell

Sometimes we might be using self signed or different domain SSL certificate for staging or QA specific websites. In Windows PowerShell, when HttpRequest is used to make requests to such websites, below error is thrown

The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.

This error means that the server certificate validation failed. If we want to ignore SSL validation check in PowerShell, we must write a call back for server certificate validation. To do this, just add below line of code before actual the HttpRequest call:

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }

Above line will always return true which means that SSL certificate validation was successful.

Monday, July 21, 2014

Partial Responses in WCF REST API

One of the nice to have feature in any REST API is partial responses. With the help of partial responses, we can request only those fields which are required as part of response.  By only requesting information that it will actually use, a client application can more efficiently use network, CPU and memory resources. By contrast, an application that retrieves a full API feed, parses it and then drops unused fields is likely to waste some resources along the way.

An example of partial response is Youtube API.

WCF framework can be used to build REST APIs which by default does not support partial responses. However, we can easily write extension in WCF services to manipulate the response message and send only partial response. One such extension is available @

This extension takes the fields parameter from the query string of requested URL and accordingly sends the required fields in the response. The fields parameter limits the data fields that are returned for the feed or for specific entries in the feed. It is case insensitive.

Following are the templates for fields parameter:

  • Immediate fields of an object – To select the immediate children nodes of an object, specify the required field names separated by comma.
    E.g.: recipeId,recipeName,photoUrl,SEOPageName
  • Fields of a nested object – To select fields of a nested object, specify the name of the nested object followed by comma separated list of fields enclosed in parentheses.
    E.g.: recipeId,recipeName,Tip(TipName)
  • Fields of a collection item – To select certain set of fields of all items in a collection, specify the name of the collection and a colon (:) followed by comma separated list of fields enclosed in parentheses. E.g.: recipeId,recipeName,Ingredients:(IngredientId,IngredientName)
Note: If the start node of service response is a collection then that particular collection name and colon should not be specified in fields parameter.

This is a WCF extension and can be easily integrated to any existing WCF service using web.config.
In web.config, add a new behavior extension and then use it the required endpoint behavior.


        <!-- Declare Extension -->
        <add name="partialResponse" type="PartialFields.PartialResponseProcessor, PartialFields" />
<behaviors> <endpointBehaviors> <behavior name="webby"> <!-- Use extension --> <partialResponse /> </behavior> </endpointBehaviors> </behaviors> </system.serviceModel> </configuration>

Monday, July 7, 2014

MongoDB and Phalcon PHP - CollectionManager Not Found

MongoDB is supported out of box in Phalcon PHP. Phalcon PHP has very simple yet limited Object-Document Mapper (ODM) library which is used for connecting to NoSQL databases like MongoDB.

To connect to MongoDB in Phalcon, a service must be added in DI container as follows:

$di->set('mongo', function() {
    $mongo = new MongoClient();
    return $mongo->selectDB("db_name"); //database name in MongoDB
}, true);

When executing the script related to MongoDB interactions in PhalconPHP, below error is thrown:
Service 'collectionManager' was not found in the dependency injection container

As each MongoDB document is a collection in Phalcon, a collection manager service is needed for initialization of models, keeping record of relations between the different models of the application etc.
To resolve this error, collectionManager service must also be added to DI container.
$di->set('collectionManager', function(){
    return new Phalcon\Mvc\Collection\Manager();
}, true);

So, while working with MongoDB in PhalconPHP, make sure that both mongo and collectionManager services are added to DI container.

Catch ThreadAbortException in ASP.Net

In ASP.Net, whenever a redirect or response end statement is executed, ThreadAbortException is thrown and it is logged in event viewer.

System.Threading.ThreadAbortException Message: Thread was being aborted. 
Source: mscorlib at System.Threading.Thread.AbortInternal() 
at System.Threading.Thread.Abort(Object stateInfo) 
at System.Web.HttpResponse.End() 

To avoid thread ThreadAbortException related messages in logging, call to Response.Redirect and Response.End methods should be wrapped in try-catch block. Simply catching this exception will not work as the exception is thrown again even after getting caught. So, in the catch block a call should be made to Thread.ResetAbort() method.

catch(ThreadAbortException ex)
    //Exception is again thrown after catch block.
    //To suppress it call ResetAbort method of System.Threading.Thread