Monday 29 February 2016

Error Handling and authentication with Interceptors in angular

About:

             Interceptors are basically service factory which we register with $httpProvider. They are used to intercept $http requests before server call or after server returned the response, so that we can process the config object, request and responses of the $http calls and modify them according to our neeeds. These are mostly used in Global Error Handling and Authentication of the requests.

There is 4 interceptors available:

  • request: This method is called before server request is made with "config" object. We can modify config object
  • requestError: This will call if the request can't be sent to the server or interceptor rejects the request.
  • response: This interceptor will be called after server returns response. arguments contains the response object, we can modify the response.
  • responseError: This interceptor will be called if the server returns error. or request is rejected by the interceptor.

Sample script for interceptors:


Creating a service with interceptor definitions:

Here is a service which defines 2 interceptors.
  1. In request interceptor we are checking the authorization and if not authorized rejecting the request.
  2. In responseError interceptor we are calling a function defined on $rootScope , which will open error model on the UI.


Registering httpInterceptor service with $httpProvider service:

We need to register our Interceptor service with $httpProvider, so that our interceptors called on every $http calls. we just need to push our service to $httpProvider.interceptors array.


We are done! It'll call our interceptor on every $http call. 

Uses:

  1. Global error handling of the application. We don't need to write error logic for every $http request
  2. Global Authentication handling. We don't need write authorization logic before every request
   Please feel free to comment if you need any information.


Saturday 27 February 2016

Creating service broker sample scripts

About Service Broker:

          Service brokers allows users to send and queue messages using server database engines. These messages processed asynchronously so users can write sophisticated programs. And can improve the performance since we don't need to wait for the message to process. We can send messages between different databases which will help us in inter database communication. We can register Stored procedure with the queue so that they will execute when we get the message in queues which will simplify the programming.

Components of service broker:


  1. Message Type: Message Type defines a name for specific kind of messages, and kind of validation for the message. If your message if perfect, setting validation OFF will improve performance. 
  2. Contract: Contract defines which message types are used in conversation. And which side can send message of that type. Each conversation follows a contract. 
  3. Queue: Queues holds incoming messages for a specific service. We should have different queues for different services. We can register activation procedures for queues which will be executed when message is received. To improve the performance we need to turn off the retention of the messages after processing, unless you need exact message.
  4. Service: A service is a name specified for a task which service broker performs. Service broker used service name to deliver messages to specific queue. we need to specify which queue the service should use while creating the service.

Creating Service Broker:

    Let's create service broker to communicate between 2 databases.

  1. SenderDB will send messages.
  2. RecieverDB will receive and process messages in a stored procedure.

Sender DB :


ReceiverDB:



Once you run both scripts your are done with the set up of all components of service broker.


Communication Using Service broker:

Sending messages:


  1.  For sending message we need to create a conversation dialogue on a service.
  2. Then we can send multiple message with send command


Receiving messages:

  1. All sent messages will be saved in queue which we registered for the receive service.
  2. we can read the messages from the service like below.


PS: If service broker is not enabled in database, using following script to enable it.

ALTER DATABASE RecieverDB SET ENABLE_BROKER
GO

Please feel free to comment if you need any clarification. or scripts.

Friday 26 February 2016

OpenCover Installation

About OpenCover:

            Opencover is a code coverage tool. Which shows % coverage and also the code which is not        covered in the tests. 

Installation Procedure:

  1.  Install the OpenCover console https://github.com/opencover/opencover/releases
  2. Install the OpenCover VisualStudio extension https://visualstudiogallery.msdn.microsoft.com/6950a046-8919-4935-8542-c6f37956f688/view/Discussions
  3.   After restarting Visual Studio click Tools -> Options -> Find OpenCover.UI Options and change OpenCover path to the install location of OpenCover console – my path is C:\Users\raghu\AppData\Local\Apps\OpenCover\OpenCover.Console.exe
  4. You should have a menu now in Visual Studio called “OpenCover”, click that menu and click “OpenCover test explorer”
  5.    Click the refresh icon in the upper left hand corner of the test explorer, this should show all the tests.                                                     
  6.   Highlight all the tests -> right click -> cover with OpenCover (wait for a bit, there is no loading screen it just freezes)
  7.  You should now have an “OpenCover Results” window, and your code should have green or red dots next to each line.

Please feel free to comment if you need any more information.