Wednesday, July 4, 2018

How to manage Global Level Exception's

We can handle the Global Level Exceptions by creating the custom exception filter in our application. To create custom exception filter do the following things.
1) Create a Class in the App_Start folder inherit the IExceptionFilter interface.
2) Override the OnException() method.
3)  Create the object of the System.Exception Class.

public class LogFilter : IExceptionFilter
    {
                public void OnException(ExceptionContext context)
        {
                    Exception ex = context.Exception;
            if (!(ex is HttpException)) //ignoring the file not found exception
            {              
             //Do your stuff.....!!!
            }
        }
    }

4) Register the new Custom Filter into the Filter.Config.cs

filters.Add(new LogFilter());

5) Register the filter in Global.asax.cs file.

FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

Note*- For using this we must enable the custom exception in the Web.config file and use the [Handle Error] action filter on either on the controller or action method.

Handling Errors in MVC

We can handle the errors in the MVC application by just enabling the custom errors tab in the web.config file.
<customErrors mode="On" defaultRedirect="Error">
 This will only handle the Internal Server Error Exceptions like HTTP Status Code 500 and won't handle the HTTP Status Code Exceptions like 404,405 etc for that we need to do the following.
   <customErrors mode="On" defaultRedirect="Error">
      <error statusCode="404" redirect="Error/NotFound" />
    </customErrors>
Here Error is the Controller Name and NotFound is the name of the Action Method after that we need to set the [HandleError] action filter on either the Controller or the Action Method as per our requirement.

Friday, June 22, 2018

Error [1001] While Installing Windows Service

If you encountered an error 1001 while installing a Windows Service then the solution for that is.
1) just check if you have UnInstalled the Windows Service.
2)  if after UnInstalling also the error remains then.
3) One more option is that Open Task Manager and under the Service Section tab look for Your Service Name then select the service go to properties => go to details option.
4) kill the process tree and try to install the service again

Tuesday, June 19, 2018

Creating Setup.exe for Windows Service

To create the Setup.exe for Windows Service using the Install Shield Tool available in the Visual Studio. Please follow the steps below for creating the setup.
1) File=>New => Project => Templates => Other Project Types => Setup & Deployment.
2) Select the Install Shield option.
3) Project Assistant window will open.
4) The first two tabs Application Information & Installation Requirements where you need to fill all the company name, product name related information.
5) In the Application Files tab, this is the main tab where you will add your .exe file. In the left side, you will find the [Program Files Folder] expand it you will see the folder with the Company Name  [INSTALLDIR] created (with the company name which you have given in the application information tab).
6) Select the Company Name [INSTALLDIR]  then click on Add Files.
7) A new pop up will open browse the location of the. .exe  file where you have kept it and copy paste all the files inside it.
8) Then select the. .exe file right click on it go to properties under the COM & .NET Settings tab check the Installer Class checkbox and click on apply.
9) The next steps [Application Shortcuts, Application Registry, Installation Interview] are simple you can set them as per your needs.
10) After setting up all the things we need to build the setup so that the .exe file is generated.
11)  Change the Solution Configuration to Single Image instead of CD_ROM which is selected default and then build the service and your .exe file is generated.
12) Click on the Open Release option to locate the newly generated .exe file.

Wednesday, February 21, 2018

Few things to remember on WebApi


1) Use the Attribute routing which is the best and easy way I guess you can use for routing and the best part is it avoids the confusion.
2) Use the [RoutePrefix("api/v1/new")] attribute on the controller so that you don't need to declare "api/v1/new"  inside each and every method you write.

    public class NewController : ApiController
    {
        [Route("api/v1/new/UserDetails")]
        [HttpGet]
        public HttpResponseMessage UserDetails()
        {
            return Request.CreateResponse(HttpStatusCode.OK, "Hello People.");
        }
    }
AFTER
    [RoutePrefix("api/v1/new")]
    public class NewController : ApiController
    {
        [Route("UserDetails")]
        [HttpGet]
        public HttpResponseMessage UserDetails()
        {
            return Request.CreateResponse(HttpStatusCode.OK, "Hello People.");
        }
    }
3) Never use the Http verbs (GET,POST,PUT,DELETE) in your method names like GetUserDetails instead use UserDetails().
4) Make use of the http tags like so it makes difficult for others to identify which verb you are using for your WebApi

        [Route("UserDetails")]
        [HttpGet] //like this one
        public HttpResponseMessage UserDetails()
        {
            return Request.CreateResponse(HttpStatusCode.OK, "Hello People.");
        }
5) Try to use the HttpResponseMessage as the return type.
6) WebApi by default returns the JSON format.
7) We can also override the RoutePrefix by using (~) symbol for ex-
    [RoutePrefix("api/v1/new")]
    public class NewController : ApiController
    {
        [Route("~/api/v1/old/Data")]
        [HttpGet]
        public HttpResponseMessage Data()
        {
            return Request.CreateResponse(HttpStatusCode.OK, "Hello People.");
        }   
   
     }

Now instead of using the http://localhost:1234/api/v1/new/Data use this http://localhost:1234/api/v1/old/Data because the root has been overridden
8) We can also add the parameters to the route and also give the default values
        [Route("DataByID /{ID}")] //Passing parameter
        [HttpGet]
        public HttpResponseMessage DataByID(int ID = 2) //Default value 2 will be passed if not specified any value.
        {
            if (ID == 1)
            {
                return Request.CreateResponse(HttpStatusCode.OK, "Hello People.");
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest, "No Result Found");
            }

        }

9) We can also add the constraint on the WebApi methods
        [Route("DataByID /{ID:int}")] //The ID must be integer or else the method will not execute
        [HttpGet]
        public HttpResponseMessage DataByID(int ID)
        {
            if (ID == 1)
            {
                return Request.CreateResponse(HttpStatusCode.OK, "Hello People.");
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest, "No Result Found");
            }

        }

Progress Bar in Angular Js


When I was working on the progress bar I went through lots of examples on the net but all of them where create it from scratch. But I found one library on the git hub created by Wes Cruver which is very easy and useful to create the progress bar actually you don't need to do anything extra you just need to add the library and inject into the file and that's it rest of the things will be taken care on its own.
Libraries to add -



GitHub Link -


Dependency Injection -

['angular-loading-bar']

<html data-ng-app="app">
<head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.9/angular.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <link rel='stylesheet' href='//cdnjs.cloudflare.com/ajax/libs/angular-loading-bar/0.9.0/loading-bar.min.css' type='text/css' media='all' />
    <script type='text/javascript' src='//cdnjs.cloudflare.com/ajax/libs/angular-loading-bar/0.9.0/loading-bar.min.js'></script>

    <script>
            var app = angular.module('app', ['angular-loading-bar']);
            app.controller('demoCtrl', ['$scope', '$http', function ($scope, $http) {          
                $http.get('https://rallycoding.herokuapp.com/api/music_albums').then(function (response) {
                    $scope.items = response.data;
                }, function (error) {
                    $scope.items = null;
                })}]);
    </script>

</head>

<body data-ng-controller="demoCtrl">
    <table>
        <tr>
            <th>Title</td>
            <th> Artist</td>
            <th> Url</td>
            <th>Image</td>
            <th>Thumbnail Image</td>
        </tr>
        <tr data-ng-repeat="item in items">
            <td data-ng-bind="item.title"></td>
            <td data-ng-bind="item.artist"></td>
            <td data-ng-bind="item.url"></td>
            <td data-ng-bind="item.image"></td>
            <td data-ng-bind="item.thumbnail_image"></td>
        </tr>
    </table>
</body>
</html>


Self referencing loop detected with type 'System.Data.Entity.DynamicProxies.XXXXXX'

under the edmx.context.cs add the below two lines  inside the edmx constructor

this.Configuration.LazyLoadingEnabled = false;
this.Configuration.ProxyCreationEnabled = false;


public YourDBEntities(): base("name=YourDBEntities")
{
       this.Configuration.LazyLoadingEnabled = false;
       this.Configuration.ProxyCreationEnabled = false;

}

Git Commands

Git Version   To check the git version Git -v       Git Clone To clone the repository, use the following command: Git clone [u...