Skip to main content

Posts

Performance improvement with StringBuilder class

Often we do large string concatenation in our code without giving a thought to performance. Consider following example of string concatenation. string s1 = "orange"; string s2 = "red"; s1 += s2; System.Console.WriteLine(s1); // outputs "orangered" s1 = s1.Substring(2, 5); System.Console.WriteLine(s1); // outputs "ange r" String objects are immutable, meaning that they cannot be changed once they have been created. Methods that act on strings actually return new string objects. In the previous example, when the contents of s1 and s2 are concatenated to form a single string, the two strings containing "orange" and "red" are both unmodified. The += operator creates a new string that contains the combined contents. The result is that s1 now refers to a different string altogether. A string containing just "orange" still exists, but is no longer referenced when s1 is concatenated. Therefore, for performance reasons, lar...

Strings

The @ Symbol The @ symbol tells the string constructor to ignore escape characters and line breaks. The following two strings are therefore identical: string p1 = "\\\\My Documents\\My Files\\"; string p2 = @"\\My Documents\My Files\"; // Copy one character of the string (not possible with a System.String) sb[0] = sb[9]; System.Console.WriteLine(sb); // displays 9123456789 } }

Method returning multiple values

If you want methods to return more than one value, declare the parameters as OUT or REF. Methods with OUT or REF are identical at compile time but different at run time. Hence you cannot overload a method one with OUT where as other with REF. You can overload method by declaring one with OUT or REF and another without it. The out keyword causes arguments to be passed by reference. This is similar to the ref keyword, except that ref requires that the variable be initialized before being passed. To use an out parameter, both the method definition and the calling method must explicitly use the out keyword. Same applied for REF For example: class OutExample { static void Method(out int i, out int j) { i = 44; j= 22; } static void Main() { int value1; int value2; Method(out value1, out value2); // value1 is now 44 //value2 is now 22 } } Although variables passed as an OUT arguments need not be initialized prior to being passed, the calling method is required to assign a value before the met...

Tracing in ASP.Net

1. To enable tracing for the application, use the element in the Web.config file or machine.config file. <trace enabled="true" pageOutput="true false" /> 2. To enable tracing for a single page, use the attributes of the @Page directive of that page. <%@ Page Trace="true" Language= ... %> 3. In your code, add custom trace messages where appropriate using the Write and Warn methods of the Trace object. In Visual Basic .NET: Trace.Write("Custom Trace", "Beginning User Code...") Trace.Warn("Custom Trace", "Array count is null!") In C#: Trace.Write("Custom Trace", "Beginning User Code..."); Trace.Warn("Custom Trace", "Array count is null!"); Optionally, use the IsEnabled property provided by the current page's TraceContext object (accessed by using the Trace property of the Page object). In Visual Basic .NET: If Trace.IsEnabled Then strMsg = "Tracing is en...

Data size limit with Web Service Extension(WSE)

If you are using web service extension, watch out for size restriction based on the version of WSE 1. WSE 1.0 There is no size restriction on this. So if your web service and client with WSE 1.0, you won't have any problem. 2. WSE 2.0/3.0 In WSE 2.0/3.0, Microsoft introduced maxRequestLength which limits the amount of data transferred per request for a web service. By default this will be 4 MB (4096KB). You can set it to unlimited by setting it -1 as follows in web.config. <httpRuntime executionTimeout="90" maxRequestLength="-1" useFullyQualifiedRedirectUrl="false" minFreeThreads="8" minLocalRequestFreeThreads="4" appRequestQueueLimit="100"/> or as follows in app.config for a windows based application. < microsoft.web.services2> < messaging > < maxRequestLength > -1 < /maxRequestLength > < /messaging ...

Application deployed on a web farm - View state validation.

By default, ASP.Net 2.0 encrypts the data in the ViewState with a private key generated on the server. This key is unique to the server. So in a scenario where there is more than one server e.g. in a web farm, each server will generate its own key. This is where the trouble starts. Consider there are 3 servers A, B and C. Say users first request is served by server A. Now when user posts the data back, say the request is sent to server B. Server B will now try to validate the data in ViewState with its own key. As the key is different to the one with which the data was encrypted, it will fail validation and will raise "Unable to validate data" error. To avoid this, configures MachineKey element in Web.Config for a cohosted website or in Machine.Config for dedicated server. Please see http://msdn2.microsoft.com/en-us/library/ms998288.aspx on how to configure machinekey. DebugGuru

Testing performance of ASP.Net Application

I have been looking at various ways of testing the performance of ASP.Net applications. Here are few tools that might be useful. 1. Application Test Center by Microsoft If you have Microsoft Visual Studio 2005 enterprise version, then you will have this software included. Please see http://msdn.microsoft.com/msdnmag/issues/05/06/ExtremeASPNET/ on how to use this great tool. 2. BadBoy This is another good software. Please visit http://www.badboy.com.au/ to download the software. This software can be used for free if you are a non profit organisation. 3. LoadRunner If you are a commerical organisation and have a good size budget, this is the software for you. Please visit http://www.mercury.com/us/products/performance-center/loadrunner/ for more information. This software gives you lot more control and has good reporting tools as well. You need special training to use this software or you can have a consultant doing the job for you. Happy performance testing. Thanks DebugGuru