JavaScriptSerializer set MaxJsonLength Property

I was working on WCF Service where I need to return large data in JSON format. We are using JavaScriptSerializer to perform operation. But we are facing following issue:

Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.

While searching, I ended up with following solution:

The default maximum length for MaxJsonLength is 102400 characters.

The value of the MaxJsonLength property applies only to the internal JavaScriptSerializer instance that is used by the asynchronous communication layer to invoke Web services methods. (MSDN: ScriptingJsonSerializationSection.MaxJsonLength Property)

You can set the MaxJsonLength property on your web.config:

<configuration>
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength=”2147483644″/>
</webServices>
</scripting>
</system.web.extensions>
</configuration>

If you are still receiving the error:

As a workaround, you can do the following within code

var serializer = new JavaScriptSerializer();

serializer.MaxJsonLength = Int32.MaxValue;

string jsonresult = serializer.Serialize(rowData);

Happy Coding 🙂

Advertisements

SOAP vs REST Web Services

SOAP REST
SOAP is a protocol. REST is an architectural style.
SOAP stands for Simple Object Access Protocol. REST stands for REpresentational State Transfer.
SOAP can’t use REST because it is a protocol. REST can use SOAP web services because it is a concept and can use any protocol like HTTP, SOAP.
SOAP uses services interfaces to expose the business logic. REST uses URI to expose business logic.
SOAP defines standards to be strictly followed. REST does not define too much standards like SOAP.
SOAP requires more bandwidth and resource than REST. REST requires less bandwidth and resource than SOAP.
SOAP defines its own security. RESTful web services inherits security measures from the underlying transport.
SOAP permits XML data format only. REST permits different data format such as Plain text, HTML, XML, JSON etc.
SOAP is less preferred than REST. REST more preferred than SOAP.

What is SOAP ?

The Simple Object Access Protocol (SOAP) is an attempt to define a standard for creating web service APIs. It is a pattern, a web service architecture, which specifies the basic rules to be considered while designing web service platforms. It typically uses HTTP as a layer 7 protocol, although this is not mandatory. The SOAP message itself consists of an envelope, inside of which are the SOAP headers and body, the actual information we want to send. It is based on the standard XML format, designed especially to transport and store structured data. SOAP may also refer to the format of the XML that the envelope uses.

What is REST ?

The Representational State Transfer (REST) is another architectural pattern (resource-oriented), an alternative to SOAP. Unlike SOAP, RESTful applications use the HTTP build-in headers (with a variety of media-types) to carry meta information and use the GET, POST, PUT and DELETE verbs to perform CRUD operations. REST is resource-oriented and uses clean URLs (or RESTful URLs).

SOAP API REST API
Supports data in the form of XML only Supports both XML and JSON format
Requires WSDL for the integration Preferred for mobile and web apps since JSON being Lighter the app runs smoother and faster
Use SOAP API in any language that supports Web services.

Happy Coding 🙂

How to detect browser name and version using javascript or jquery in asp.net

<html>
<head>
 <script>
 navigator.sayswho = (function () {
 var ua = navigator.userAgent, tem, M = ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
 
 if (/trident/i.test(M[1])) {
 tem = /\brv[ :]+(\d+)/g.exec(ua) || [];
 //return 'IE ' + (tem[1] || '');
 return { name: 'IE ', version: (tem[1] || '') };
 }
 
 if (M[1] === 'Chrome') {
 tem = ua.match(/\bOPR\/(\d+)/);
 //if (tem != null) return 'Opera ' + tem[1];
 if (tem != null) { return { name: 'Opera', version: tem[1] }; }
 }
 
 M = M[2] ? [M[1], M[2]] : [navigator.appName, navigator.appVersion, '-?'];

 if ((tem = ua.match(/version\/(\d+)/i)) != null) {
 M.splice(1, 1, tem[1]);
 }
 alert('Browser Name: ' + M.join(' and Version # '));
 return M.join(' ');
 })();
 </script>
</head>
</html>

Copy above code and paste it into notepad and save notepad as .html file. Now open .html file and you will see the result.

http://www.codeproject.com/Answers/898796/How-to-get-browser-name-in-javascript-or-jquery-in#answer2

Generating Unique Key in .Net using RNGCryptoServiceProvider

I need to create some unique keys. GUID is one option as they give Globally Unique identifier but they are big. If you want to issue unique number in your application which you want to give as AppKey or any reference number then GUIDs is obviously not a solution.

RNG means Random Number Generator

The .net Framework provides RNGCryptoServiceProvider class which Implements a cryptographic Random Number Generator (RNG) using the implementation provided by the cryptographic service provider (CSP). This class is usually used to generate random numbers.

Although we can use this class to generate unique number in some sense. Moreover while generating key we can make key more complicated by making it as alpha numeric rather than numeric only.

So, we used this function along with some character masking to generate unique key of any length (here I take 32 character long key).

Below is code sample:

private string RNGCharacterMask()

{

int maxSize = 32;

int minSize = 32;

char[] chars = new char[62];

string a;

a = “abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890”;

chars = a.ToCharArray();

int size = maxSize;

byte[] data = new byte[1];

RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();

crypto.GetNonZeroBytes(data);

size = maxSize;

data = new byte[size];

crypto.GetNonZeroBytes(data);

StringBuilder result = new StringBuilder(size);

foreach (byte b in data)

{

result.Append(chars[b % (chars.Length – 1)]);

}

return result.ToString();

}

Happy Coding… 🙂

Retrieve First Name and Last Name from string in asp.net

Retrieve First Name and Last Name from string in asp.net

VB.Net:

Dim name As String = “Joseph Van Andrews”

‘ Split string based on spaces
Dim names As String() = name.Split(New Char() {” “c})

‘ Seperate the first name from the rest of the string
Dim lastName as string = name.substring(names(0).length())

Dim nameString as string = “the First Name is: ” + names(0) + ” and the Last Name is: ” + lastName

response.write(nameString)

C#.Net:

string name = “ABC DEF XYZ”;

// Split string based on spaces
string[] names = name.Split(new char[] { ‘ ‘ });

// Seperate the first name from the rest of the string
string lastName = name.Substring(names[0].Length());

string nameString = “the First Name is: ” + names[0] + ” and the Last Name is: ” + lastName;

response.write(nameString);

Happy Coding 🙂