Friday, February 14, 2014

Working with JSON in c# using using Newtonsoft.Json

JSON stands for "Java Script Object Notation".
JSON you need a lightweight, open, text-based platform independent data exchange format for transferring data back and forth between the client and server. Although XML works well for many application scenarios but it has an extra payload that is XML tags.So to overcome this drawback ,JSON was introduced.
 In this example i will be using a library to access a WCF Rest Service using Newtonsoft.json.dll.
Here is the simple code to access a JSON array Response in c#
Just include Newtonsoft.Json and Newtonsoft.Json.Linq namespaces

Code:


 
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Net;

using System.Web.Script.Serialization;

using System.Net.Json;

using System.IO;

using Newtonsoft.Json;

using Newtonsoft.Json.Linq;

using Jsonparse;

 namespace Jsonparse

 {

 class Program

 {

 static void Main(string[] args)

 {

 WebClient c = new WebClient();

var data=c.DownloadString("http://localhost:50766/Service1.svc/json/GetDispatchOrders");

//ordtrnh is my custom class declared in Jsonparse namespace

 List

myDeserializedObjList = (List)Newtonsoft.Json.JsonConvert.DeserializeObject(data, typeof(List));

 foreach(ordtrnh o in myDeserializedObjList) 

 { 

 Console.WriteLine("Customer Id is {0}",o.custid); 

 Console.WriteLine("Order Date is {0}", o.orddate);

 Console.WriteLine("Customer Area is {0}", o.parea); 

Console.WriteLine("----------------------------------------------");

 } 

 Console.ReadLine(); 

 } 

 } 

}
Output:
You can download newtonsoft.json.dll from   Here

Wednesday, February 12, 2014

The maximum message size quota for incoming messages (65536) has been exceeded.(Error in WCF Test Client)

Sometimes when we invoke a method in WCF Test Client we get an error even though same restfull service when invoked in browser works fine.In WCF Test Client we are likely to get an error

"The maximum message size quota for incoming messages (65536) has been exceeded" after using the WCF Test Client utility to invoke a method on my service." 

There is nothing wrong with the service this is just the configuration change in WCF Test Client. In the wcf test client, after you add the service, you can notice there is a "config file" node at the end of the service tree: right click it and select "edit with SvcConfigEditor". You will get the exact configuration editor for the service side - just go to the binding settings and change the MaxReceivedMessageSize and save the changes.

Just put these values

maxBufferSize=”2147483647″
maxReceivedMessageSize=”2147483647″

Authentication issues in WCF(IntegratedWindowsAuthentication, Anonymous', but the binding only supports specification of exactly one authentication scheme. Valid authentication schemes are Digest, Negotiate, NTLM, Basic, or Anonymous.) on remote server

While hosting your Restful WCF Service on shared hosting you are more likely to get the error

IIS specified authentication schemes 'IntegratedWindowsAuthentication, Anonymous', but the binding only supports specification of exactly one authentication scheme. Valid authentication schemes are Digest, Negotiate, NTLM, Basic, or Anonymous. Change the IIS settings so that only a single authentication scheme is used.

To overcome this error we have to make a small change in our web.config file as
<system.servicemodel="">
    <bindings>
      <webhttpbinding>
        <binding>
          <security mode="None">
            <transport clientcredentialtype="Windows">
          </transport></security>
        </binding>
      </webhttpbinding>
    </bindings>
  </system>



This will disable security of your service & your service doesn't need authentication

Thursday, February 6, 2014

Difference between dll and exe

One of the most common interview questions asked by a window developer is

Difference between exe and dll

1.EXE is an extension used for executable files while DLL is the extension for a dynamic link library. 2.An EXE file can be run independently while a DLL is used by other applications. 3.An EXE file defines an entry point while a DLL does not. 4.A DLL file can be reused by other applications while an EXE cannot. 5.A DLL would share the same process and memory space of the calling application while an EXE creates its separate process and memory space. (The DLL is an inprocess component but exe is outprocess component. When system launches an exe, a new process is created , for DLL a new process is not created).

Tuesday, December 24, 2013

The type or namespace name 'AjaxControlToolkit' could not be found in the global namespace

The type or namespace name 'AjaxControlToolkit' could not be found in the global namespace (are you missing an assembly reference?)

Most of the times when we are using Ajax Control Toolkit  we are getting an error when compiling our web application I've added the toolkit countless times before but for some reason, I keep getting this. How can I resolve this issue so I can use the toolkit? It is recommended to add reference to the AjaxControlToolkit.dll(right click on the bin directory, and choose "Add Reference"), and then AjaxControlToolkit.dll will be added to the bin directory automatically. But even after adding the reference to AjaxControlToolkit the project was complaining that "The type or namespace name 'AjaxControlToolkit' could not be found (are you missing a using directive or an assembly reference?)". How to solve this. The answer is that the project is still using Microsoft ASP.NET 2.0 AJAX Extensions 1.0 so it can not use .net v3.5 or above. So how to fix? Turns out that the toolkit relies on 2 additional assemblies, these are:
System.Web.Extensions.dll
System.Web.Extensions.Design.dll
See in your Refrences if both of these are present. You can download these files from here

Saturday, September 21, 2013

How to make a directory as git repository


Many a times we have  non directory and we want to make them as git repository.In such a situation open git bash and follow following steps
git init

git add .

git commit -m 'message'

git remote add origin <url of remote repository>

git push -u origin master

Working with Git

Step 1. Clone your repository to your local system
first create a new directory which you want to be as repository
Open Git Bash move to the directory by using  cd path/to/my/repo
Now clone the repository using

$ git clone https://url of your remote repository/newuserme/repository.git
you will get a message like this
Cloning into 'repository'...
Password
warning: You appear to have cloned an empty repository.

Step 2. Explore your repository and fix a problem

$ ls -al 
This will give the list of files in repository.

Step 3. Create a README, add it locally, and push it to the remote server

create readme file  with content about file and save it in local folder .This file is a text file.
Now check the status of file on Git Bash

$ git status 

Now add the Readme file using 

$ git add README

commit the changes using following command

$ git commit -m "Here should always be some meaning full message".

Now we can push the changes to the remote server using following command

$ git push -u origin master

It will ask for you for password.
Password: 
Note:

Sometimes while commiting you we get an error like 
*** Please tell me who you are.

Run

git config --global user.email "you@example.com"

git config --global user.name "Your Name"

to set your account's default identity.

Omit --global to set the identity only in this repository.

In such a situation enter these two commands

 git config --global user.email "here you should put your email address"

  git config --global user.name "Your Name"


Now if someone else working on same code has updated the code then we can get the updated code using git Pull command

$ git pull origin master