Tuesday, 31 October 2017

How To Fix Windows 10 Slow Performance

While searching around for some tweaks, I came across a thread on Microsoft Community formums and one suggestion was to change the settings for the Paging File to fix Windows 10 slow performance. By default, in all versions of Windows, it is set to allow the system to manage it.

ALSO READ: TECKLYFE – FIX WINDOWS 10 SLOW INTERNET SPEEDS

How To Fix Windows 10 Slow Performance

If you’re noticing Windows 10 slow performance, try changing the Initial Size and Maximum Size to the Recommended File Size for both.
  • Open Start Menu and find the Control Panel. Click on it.
  • Here in the Control Panel, go to the Search field at the top-right of the window and type Performance. Now hit Enter.
  • Now find Adjust the appearance and performance of Windows.
  • Go to the Advanced tab and click on Change in the Virtual Memory section.
windows-10-slow-performance-issue-1
  • Now uncheck the option Automatically manage paging file size for all drives.
  • Select the default C: drive where Windows 10 is installed and then choose Custom Size. Then change Initial Size and Maximum Size to the recommended values by Windows which is in the Total Paging File Size For All Drives section.
  • NOTE: Your recommended value will be different than the image below, use whatever your’s shows
windows-10-slow-performance-issue
  • Now click Set and then hit OK to save the settings.
  • Reboot the computer for the changes to take effect and fix the Windows 10 slow performance.
Leave your feedback in the comments if this showed improvement for you or not.
If you experience worse performance, you can go back into this setting and change it back to System Managed Size

Video Walkthrough

Option 2: How To Fix Windows 10 Slow Performance

This tip is an oldie but goodie. These settings have been around since Windows XP, but you can still apply them to Windows 10. These will help visually speed things up in Windows 10, especially if you have an older video card or a machine with less RAM. You can read the article Tech Tip: Visually Speed Up Windows 8 (7/Vista/XP).
performance
– Search for your Control Panel
– Click on System
– Click on Advanced System Settings in the left column
– Under the Advanced tab, click on Settings in the Performance box
I always uncheck the following:
– Animate windows when minimizing and maximizing
– Animations in the taskbar
– Enable Peek (sometimes I leave this one on)
– Fade or slide menus into view
– Fade or slide ToolTips into view
– Fade out menu items after click
– Slide open combo boxes

https://www.tecklyfe.com/possible-fix-windows-10-running-very-slow/

Create RESTful WCF Service

Introduction

Windows Communication Foundation (WCF) is an SDK for developing and deploying services on Windows. WCF provides a runtime environment for your services, enabling you to expose CLR types as services, and to consume other services as CLR types. In this article, I am going to explain how to implement restful service API using WCF 4.0 . The Created API returns XML and JSON data using WCF attributes.

What is REST?

Based on the Roy Fielding theory "Representational State Transfer (REST), attempts to codify the architectural style and design constraints that make the Web what it is. REST emphasizes things like separation of concerns and layers, statelessness, and caching, which are common in many distributed architectures because of the benefits they provide. These benefits include interoperability, independent evolution, interception, improved scalability, efficiency, and overall performance."
Actually only the difference is how clients access our service. Normally, a WCF service will use SOAP, but if you build a REST service, clients will be accessing your service with a different architectural style (calls, serialization like JSON, etc.).
REST uses some common HTTP methods to insert/delete/update/retrieve information which is below:
  1. GET - Requests a specific representation of a resource
  2. PUT - Creates or updates a resource with the supplied representation
  3. DELETE - Deletes the specified resource
  4. POST - Submits data to be processed by the identified resource

Why and Where to Use REST?

Few days back, I was writing a service which was supposed to be accessed by heterogeneous language/platform/system. It can be used by iPhone, Android, Windows Mobile, .NET web application, JAVA or PHP. Using web service, it was bit complex for me to expose it to everyone using uniform system. Then we decided to use REST, which was easily espoused over cloud. This was a great example which shows the capability of SIMPLE RESTful SERVICE :). Below are some points which will help you to understand why to use the RESTful services.
  1. Less overhead (no SOAP envelope to wrap every call in)
  2. Less duplication (HTTP already represents operations like DELETEPUTGET, etc. that have to otherwise be represented in a SOAP envelope).
  3. More standardized - HTTP operations are well understood and operate consistently. Some SOAP implementations can get finicky.
  4. More human readable and testable (harder to test SOAP with just a browser).
  5. Don't need to use XML (well, you kind of don't have to for SOAP either but it hardly makes sense since you're already doing parsing of the envelope).
  6. Libraries have made SOAP (kind of) easy. But you are abstracting away a lot of redundancy underneath as I have noted. Yes, in theory, SOAP can go over other transports so as to avoid riding atop a layer doing similar things, but in reality just about all SOAP work you'll ever do is over HTTP.

Step by Step Guide

Generally, a developer is scared to use WCF because of a lot of confusing configuration. I will try to use minimum configuration so that it will be easier to understand for us. We will develop Restful WCS API in 6 steps. So let’s start now.

STEP 1

First of all launch Visual Studio 2010. Click FILE->NEW->PROJECT. Create new "WCF Service Application".

STEP 2

Once you create the project, you can see in solution that By Default WCF service and interface file are already created. Delete By default created file as we will create our own interface and WCF service file.

STEP 3

Now right click on solution and create one new WCF service file. I have given name to the service file as “RestServiceImpl.svc”.

STEP 4

As I explained at the start of the article that we will be writing an API which can return data in XML and JSON format, here is the interface for that. In IRestServiceImpl, add the following code:

In the above code, you can see two different methods of IRestService which are XMLData and JSONDataXMLData returns result in XML whereas JSONData in JSON.

STEP 5

Open the file RestServiceImpl.svc.cs and write the following code over there:

STEP 6

Now let’s move to configuration part which is the last one. There will be two basic parts of the configurations file which we must have to understand.
<services> 
This part contains information about the End Point. Below are the code details.
Click to enlarge image
<behaviors>
This part contains details about service and endpoint behavior.

And that’s it. Our Restful WCF service is ready for test purposes.

Service Ready to Test Now

Now I launch the application in the browser to see the result. I launch this service in Internet Explorer and my URL is now http://localhost:35798/RestServiceImpl.svc. Now if I use http://localhost:35798/RestServiceImpl.svc/xml/123 URL, I get the following response on the browser which is an XML format and that was my task to achieve.

Now if I use http://localhost:35798/RestServiceImpl.svc/json/123 URL, I get the following response on the browser which is an XML format and that was my task to achieve.

Sunday, 29 October 2017

Difference between WCF and Web API and WCF REST and Web Service

The .Net framework has a number of technologies that allow you to create HTTP services such as Web Service, WCF and now Web API. There are a lot of articles over the internet which may describe to whom you should use. Now a days, you have a lot of choices to build HTTP services on .NET framework. In this article, I would like to share my opinion with you over Web Service, WCF and now Web API. For more information about Web API refers What is Web API and why to use it ?.

Web Service

  1. It is based on SOAP and return data in XML form.
  2. It support only HTTP protocol.
  3. It is not open source but can be consumed by any client that understands xml.
  4. It can be hosted only on IIS.

WCF

  1. It is also based on SOAP and return data in XML form.
  2. It is the evolution of the web service(ASMX) and support various protocols like TCP, HTTP, HTTPS, Named Pipes, MSMQ.
  3. The main issue with WCF is, its tedious and extensive configuration.
  4. It is not open source but can be consumed by any client that understands xml.
  5. It can be hosted with in the applicaion or on IIS or using window service.

WCF Rest

  1. To use WCF as WCF Rest service you have to enable webHttpBindings.
  2. It support HTTP GET and POST verbs by [WebGet] and [WebInvoke] attributes respectively.
  3. To enable other HTTP verbs you have to do some configuration in IIS to accept request of that particular verb on .svc files
  4. Passing data through parameters using a WebGet needs configuration. The UriTemplate must be specified
  5. It support XML, JSON and ATOM data format.

Web API

  1. This is the new framework for building HTTP services with easy and simple way.
  2. Web API is open source an ideal platform for building REST-ful services over the .NET Framework.
  3. Unlike WCF Rest service, it use the full featues of HTTP (like URIs, request/response headers, caching, versioning, various content formats)
  4. It also supports the MVC features such as routing, controllers, action results, filter, model binders, IOC container or dependency injection, unit testing that makes it more simple and robust.
  5. It can be hosted with in the application or on IIS.
  6. It is light weight architecture and good for devices which have limited bandwidth like smart phones.
  7. Responses are formatted by Web API’s MediaTypeFormatter into JSON, XML or whatever format you want to add as a MediaTypeFormatter.

To whom choose between WCF or WEB API

  1. Choose WCF when you want to create a service that should support special scenarios such as one way messaging, message queues, duplex communication etc.
  2. Choose WCF when you want to create a service that can use fast transport channels when available, such as TCP, Named Pipes, or maybe even UDP (in WCF 4.5), and you also want to support HTTP when all other transport channels are unavailable.
  3. Choose Web API when you want to create a resource-oriented services over HTTP that can use the full features of HTTP (like URIs, request/response headers, caching, versioning, various content formats).
  4. Choose Web API when you want to expose your service to a broad range of clients including browsers, mobiles, iphone and tablets.

Friday, 27 October 2017

LINQ Single vs SingleOrDefault vs First vs FirstOrDefault

Many people get confused about the difference between SingleSingleOrDefaultFirst, and FirstOrDefaultmethods in Linq. Below is a chart explaining the difference between them and examples of each scenario.
 Single()SingleOrDefault()First()FirstOrDefault()
DescriptionReturns a single, specific element of a sequenceReturns a single, specific element of a sequence, or a default value if that element is not foundReturns the first element of a sequenceReturns the first element of a sequence, or a default value if no element is found
Exception thrown whenThere are 0 or more than 1 elements in the resultThere is more than one element in the resultThere are no elements in the resultOnly if the source is null (they all do this)
When to useIf exactly 1 element is expected; not 0 or more than 1When 0 or 1 elements are expectedWhen more than 1 element is expected and you want only the firstWhen more than 1 element is expected and you want only the first. Also it is ok for the result to be empty

Examples

First we create an Employee table for querying. We will test with three difference scenarios:
  • Employeeid = 1: Only one employee with this ID
  • Firstname = Robert: More than one employee with this name
  • Employeeid = 10: No employee with this ID
EmployeeidLastnameFirstnameBirthdate
1DavolioNancy12/8/1948 12:00:00 AM
2FullerAndrew2/19/1952 12:00:00 AM
3LeverlingJanet8/30/1963 12:00:00 AM
4PeacockMargaret9/19/1937 12:00:00 AM
5BuchananRobert3/4/1955 12:00:00 AM
6SuyamaMichael7/2/1963 12:00:00 AM
7KingRobert5/29/1960 12:00:00 AM
8CallahanLaura1/9/1958 12:00:00 AM
9DodsworthAnne1/27/1966 12:00:00 AM

Single()

Statement
Employee.Single(e => e.Employeeid == 1)
Expected ResultThere is only one record where Employeeid == 1. Should return this record.
Actual Result
Employeeid1
LastnameDavolio
FirstnameNancy
Birthdate12/8/1948 12:00:00 AM
Statement
Employee.Single(e => e.Firstname == "Robert")
Expected ResultThere are multiple records where Firstname == Robert. Should fail.
Actual ResultInvalidOperationException: Sequence contains more than one element
Statement
Employee.Single(e => e.Employeeid == 10)
Expected ResultThere is no record with Employeeid == 10. Should fail.
Actual ResultInvalidOperationException: Sequence contains no elements

SingleOrDefault()

Statement
Employee.SingleOrDefault(e => e.Employeeid == 1)
Expected ResultThere is only one record where Employeeid == 1. Should return this record.
Actual Result
Employeeid1
LastnameDavolio
FirstnameNancy
Birthdate12/8/1948 12:00:00 AM
Statement
Employee.SingleOrDefault(e => e.Firstname == "Robert")
Expected ResultThere are multiple records where Firstname == Robert. Should fail.
Actual ResultInvalidOperationException: Sequence contains more than one element
Statement
Employee.SingleOrDefault(e => e.Employeeid == 10)
Expected ResultThere is no record with Employeeid = 10. Should return default value.
Actual Resultnull

First()

Statement
Employee.OrderBy(e => e. Birthdate)
.First(e => e.Firstname == "Robert")
Expected ResultThere are multiple records where Firstname == Robert. Should return the oldest one.
Actual Result
Employeeid5
LastnameBuchanan
FirstnameRobert
Birthdate3/4/1955 12:00:00 AM
Statement
Employee.OrderBy(e => e. Birthdate)
.First(e => e.Employeeid == 10)
Expected ResultThere is no record with Employeeid = 10. Should fail.
Actual ResultInvalidOperationException: Sequence contains no elements

FirstOrDefault()

Statement
Employee.OrderBy(e => e. Birthdate)
.FirstOrDefault(e => e.Firstname == "Robert")
Expected ResultThere are multiple records where Firstname == Robert. Should return the oldest one.
Actual Result
Employeeid5
LastnameBuchanan
FirstnameRobert
Birthdate3/4/1955 12:00:00 AM
Statement
Employee.OrderBy(e => e. Birthdate)
.FirstOrDefault(e => e.Employeeid == 10)
Expected ResultThere is no record with Employeeid = 10. Should return default value.
Actual Resultnull

What Is Idempotence in Rest API









Idempotence is a funky word that often hooks people. Idempotence is sometimes a confusing concept, at least from the academic definition.
From a RESTful service standpoint, for an operation (or service call) to be idempotent, clients can make that same call repeatedly while producing the same result. In other words, making multiple identical requests has the same effect as making a single request. Note that while idempotent operations produce the same result on the server (no side effects), the response itself may not be the same (e.g. a resource's state may change between requests).
The PUT and DELETE methods are defined to be idempotent. However, there is a caveat on DELETE. The problem with DELETE, which if successful would normally return a 200 (OK) or 204 (No Content), will often return a 404 (Not Found) on subsequent calls, unless the service is configured to "mark" resources for deletion without actually deleting them. However, when the service actually deletes the resource, the next call will not find the resource to delete it and return a 404. However, the state on the server is the same after each DELETE call, but the response is different.
GET, HEAD, OPTIONS and TRACE methods are defined as safe, meaning they are only intended for retrieving data. This makes them idempotent as well since multiple, identical requests will behave the same.

http://www.restapitutorial.com/lessons/idempotency.html

Tuesday, 24 October 2017

HandleError attribute in mvc

In this video, we will discuss HandleError attribute in asp.net mvc. HandleErrorAttribute is used to display friendly error pages to end user when there is an unhandled exception. Let us understand this with an example. 

 

Step 1: Create a blank asp.net mvc 4 application.

Step 2: Add a HomeController. Copy and paste the following code. 
public ActionResult Index()
{
    throw new Exception("Something went wrong");
}

 

Notice that, the Index() action method throws an exception. As this exception is not handled, when you run the application, you will get the default "yellow screen of death" which does not make sense to the end user.
Yello screen of death 

Now, let us understand replacing this yellow screen of death, with a friendly error page.

Step 3: Enable custom errors in web.config file, that is present in the root directory of your mvc application. "customErrors" element must be nested under "<system.web>". For detailed explanation on MODE attribute, please watch Part 71 of ASP.NET Tutorial.
<customErrors mode="On">
</customErrors>

Step 4: Add "Shared" folder under "Views" folder. Add Error.cshtml view inside this folder. Paste the following HTML in Error.cdhtml view.
<h2>An unknown problem has occured, please contact Admin</h2>

Run the application, and notice that, you are redirected to the friendly "Error" view, instead of the generic "Yellow screen of death".

We did not apply HandleError attribute anywhere. So how did all this work?
HandleErrorAttribute is added to the GlobalFilters collection in global.asax. When a filter is added to the GlobalFilters collection, then it is applicable for all controllers and their action methods in the entire application. 

Right click on "RegisterGlobalFilters()" method in Global.asax, and select "Go To Definition" and you can find the code that adds "HandleErrorAttribute" to GlobalFilterCollection.
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new HandleErrorAttribute());
}

Is the friendly error page displayed for HTTP status code 404?
No, but there is a way to display the friendly error page.

In the HomeController, we do not have List() action method. So, if a user navigates to /Home/List, we get an error - The resource cannot be found. HTTP 404.

To display a friendly error page in this case
Step 1: Add "ErrorController" to controllers folder. Copy and paste the following code.
public class ErrorController Controller
{
    public ActionResult NotFound()
    {
        return View();
    }
}

Step 2: Right click on "Shared" folder and add "NotFound.cshtml" view. Copy and paste the following code.
<h2>Please check the URL. The page you are looking for cannot be found</h2>

Step 3: Change "customErrors" element in web.config as shown below.
<customErrors mode="On">
  <error statusCode="404" redirect="~/Error/NotFound"/>
</customErrors>