tag:blogger.com,1999:blog-41533758736398042012024-03-08T11:08:57.370+02:00Caught In A WebShare knowledge on web development like: ASP.NET, AJAX, Web 2.0, JavaScript, CSS, HTML, JSON, XML and moreRotem Bloomhttp://www.blogger.com/profile/13853376077098966515noreply@blogger.comBlogger49125tag:blogger.com,1999:blog-4153375873639804201.post-61262069399608377802008-06-25T17:32:00.002+03:002008-06-25T17:39:18.072+03:00My blog was forward to Microsoft (MS Israel Community)Hi All,<br />I forward my blog to MS Israel Community.<br /><br />My new blog URL is: <a href="http://blogs.microsoft.co.il/blogs/rotemb/">Rotem Bloom's Blog</a>.<br /><br />I want to say many thanks to Guy Burstein that helps me open the new blog.Rotem Bloomhttp://www.blogger.com/profile/13853376077098966515noreply@blogger.com2tag:blogger.com,1999:blog-4153375873639804201.post-43211425221846320472008-06-18T09:57:00.000+03:002008-06-18T10:00:25.186+03:00Assembly Information work under .NET 2.0Hi All,<br />The Assembly Information tool is now also work under .NET 2.0.<br /><br />You can download the setup from codeplex on the link below:<br /><a href="http://www.codeplex.com/AssemblyInformation/Release/ProjectReleases.aspx?ReleaseId=14490">Assembly Information work under .NET 2.0</a>Rotem Bloomhttp://www.blogger.com/profile/13853376077098966515noreply@blogger.com4tag:blogger.com,1999:blog-4153375873639804201.post-28359849525499984602008-06-17T09:11:00.000+03:002008-06-17T09:24:06.506+03:00WCF Practices: Single point for WCF proxy callsHi All,<br />Let's say you want that all your WCF proxy calls will come from one function. This could be very useful and save a lot of code.<br />For example you can write your proxy exception handling in one place.<br /><br />Here is the code example that work on .NET framework 3.5 only:<br /><br />// The public function that execute the WCF proxy call<br />public void WCFProxyCall()<br />{ <br /> CreateConferenceRequest r = new CreateConferenceRequest();<br /> <br /> Func<CreateConferenceRequest, CreateConferenceResponse> createFunc = a =><br /> proxy.CreateConference(a);<br /> <br /> CreateConferenceResponse ci = ProxyWrapperOperation<CreateConferenceRequest, CreateConferenceResponse>(createFunc, r);<br />}<br /><br /><br />// This is the single point function for the entire WCF proxy calls<br />// Here you can write code for all your WCF proxy calls.<br />private TResult ProxyWrapperOperation<T, TResult>(Func<T, TResult> operation, T parameter) <br />{<br /> TResult result;<br /> try<br /> {<br /> result = operation(parameter);<br /> }<br /> catch (TimeoutException timeProblem)<br /> {<br /> proxy.Abort();<br /> throw;<br /> }<br /> catch (FaultException unknownFault)<br /> {<br /> throw;<br /> }<br /> catch (CommunicationException commProblem)<br /> {<br /> proxy.Abort();<br /> throw;<br /> }<br /> <br /> return result;<br />}Rotem Bloomhttp://www.blogger.com/profile/13853376077098966515noreply@blogger.com0tag:blogger.com,1999:blog-4153375873639804201.post-89860762004704511392008-06-15T10:33:00.000+03:002008-06-15T10:36:26.730+03:00.NET Assembly Information Tool is now on codeplexHi All,<br />I wrote a nice .NET tool that display .net assembly information like:<br />1) Compilation mode Debug\Release.<br />2) .NET Assembly full name<br />3) .NET Assembly references<br /><br />You must have .NET framework 3.5 install in order to work with .NET Assembly Information tool.<br /><br />How to use:<br />After the installation you can point on any .NET Assembly (DLL), press right click and press on the new menu called: "Assembly Information".<br />A popup window with useful information on your Assembly will pops. <br /><br />You can find it under:<br /><a href="http://www.codeplex.com/AssemblyInformation">.NET Assembly Information</a>Rotem Bloomhttp://www.blogger.com/profile/13853376077098966515noreply@blogger.com28tag:blogger.com,1999:blog-4153375873639804201.post-76062762388484605252008-05-12T18:29:00.000+03:002008-05-13T09:20:37.071+03:00svcutil with /r does not work for MessageContractHi,<br />I was trying to generate WCF proxy (Client) using svcutil.exe from Visual Studio 2008. I also need to use the /r parameter of the svcutil.exe so the generated proxy code will not include the objetcs from my other internal assemblies.<br />The command I use is something like:<br />svcutil.exe /async http://localhost:8080/MyService /r:"C:\My.dll" /tcv:Version35 /out:MyClient.cs<br /><br />I saw that if my .NET MessageContract objects on My.dll still exists on my generated proxy, so I have to change these objects to DataContract objects and then the generated proxy was correct.<br /><br />You also need to remember that if your internal objects (on My.dll) contain objects form other internal assembly, You have to include this dll also in your svcutil command. So you new command will look like:<br />svcutil.exe /async http://localhost:8080/MyService /r:"C:\My.dll" /r:"C:\MyOtherObjects.dll" /tcv:Version35 /out:MyClient.cs<br /><br />Your,<br />RotemRotem Bloomhttp://www.blogger.com/profile/13853376077098966515noreply@blogger.com0tag:blogger.com,1999:blog-4153375873639804201.post-2186359912680773212008-05-05T16:43:00.000+03:002008-05-05T16:49:55.698+03:00Increasing WCF Client Applications PerformanceSome times your WCF client object are created on the Middle-Tier of the application.<br />This scenario leads for creating many WCF client objects quickly and, as a result, experience increased initialization requirements.<br /><br />There are two main approaches to increasing the performance of middle-tier applications when calling services:<br />1. Cache the WCF client object and reuse it for subsequent calls where possible.<br />2. Create a ChannelFactory object and then use that object to create new WCF client channel objects for each call.<br /><br />Issues to consider when using these approaches include:<br /><br /> - If the service is maintaining a client-specific state by using a session, then you cannot reuse the middle-tier WCF client with multiple-client tier requests because the service's state is tied to that of the middle-tier client.<br /><br />- If the service must perform authentication on a per-client basis, you must create a new client for each incoming request on the middle tier instead of reusing the middle-tier WCF client (or WCF client channel object) because the client credentials of the middle tier cannot be modified after the WCF client (or ChannelFactory) has been created.<br /><br />- While channels and clients created by the channels are thread-safe, they might not support writing more than one message to the wire concurrently. If you are sending large messages, particularly if streaming, the send operation might block waiting for another send to complete. This causes two sorts of problems: a lack of concurrency and the possibility of deadlock if the flow of control returns to the service reusing the channel (that is, the shared client calls a service whose code path results in a callback to the shared client). This is true regardless of the type of WCF client you reuse.<br /><br />- You must handle faulted channels regardless of whether you share the channel. When channels are reused, however, a faulting channel can take down more than one pending request or send.<br /><br />Link to MS article:<br /><a href="http://msdn.microsoft.com/en-us/library/aa738757.aspx">Middle-Tier Client Applications</a>Rotem Bloomhttp://www.blogger.com/profile/13853376077098966515noreply@blogger.com0tag:blogger.com,1999:blog-4153375873639804201.post-38403744896856409092008-05-05T16:15:00.000+03:002008-05-05T16:17:20.817+03:00Best Practices: How to Dispose WCF clientsUse of the using statement (Using in Visual Basic) is not recommended for Dispose WCF clients. This is because the end of the using statement can cause exceptions that can mask other exceptions you may need to know about.<br /><br />using (CalculatorClient client = new CalculatorClient())<br />{<br /> ...<br />} // <-- this line might throw<br />Console.WriteLine("Hope this code wasn't important, because it might not happen.");<br /><br />The correct way to do it is:<br />try<br />{<br /> ...<br /> client.Close();<br />}<br />catch (CommunicationException e)<br />{<br /> ...<br /> client.Abort();<br />}<br />catch (TimeoutException e)<br />{<br /> ...<br /> client.Abort();<br />}<br />catch (Exception e)<br />{<br /> ...<br /> client.Abort();<br /> throw;<br />}Rotem Bloomhttp://www.blogger.com/profile/13853376077098966515noreply@blogger.com0tag:blogger.com,1999:blog-4153375873639804201.post-26246028032550685542008-04-17T15:54:00.000+03:002008-04-17T15:58:52.633+03:00How to add custom http header in WCFIf you want to add custom http header web service calls, there is a simple way to do it in WCF. Here is the code:<br /><br />MessageHeader header<br /> = MessageHeader.CreateHeader(<br /> "My-CustomHeader",<br /> "http://myurl",<br /> "Custom Header."<br /> );<br /><br />using (Service1Client client = new Service1Client())<br />{<br /> using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))<br /> {<br /> OperationContext.Current.OutgoingMessageHeaders.Add(header);<br /><br /> HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty();<br /> httpRequestProperty.Headers.Add("myCustomHeader", "Custom Happy Value.");<br /> httpRequestProperty.Headers.Add(HttpRequestHeader.UserAgent, "my user agent");<br /> OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;<br /><br /> }<br />}Rotem Bloomhttp://www.blogger.com/profile/13853376077098966515noreply@blogger.com3tag:blogger.com,1999:blog-4153375873639804201.post-21960366423103370342008-04-15T17:54:00.000+03:002008-04-15T18:15:48.349+03:00ReaderWriterLockSlim Performance - Is it really so slim??!!!Hi,<br />On the .NET 3.5 Microsoft came up with new object named ReaderWriterLockSlim in order to achieve synchronised access to shared resources and also to improve the profermance issues that the old object (System.Threading.ReaderWriterLock) has.<br /><br /><a href="http://blogs.msdn.com/pedram">Pedram Rezaei's</a> wrote on his blog great post on Performance Comparison of ReaderWriterLockSlim.<br />On his performance tests he found that ReaderWriterLockSlim object is slower then the Monitor object.<br /><br />Pedram Rezaei's conclusion:<br />"A reader-writer lock allows for multiple concurrent reads to enter a read-lock all at the same time and depending on the scenario, it can significantly improve the overall performance of your application when used instead of a mutual exclusive lock. A read-lock takes a shared lock and a write-lock takes an exclusive lock.<br /><br />The new ReaderWriterLockSlim correctly gives priority to write requests therefore only use when reads are frequent and writes are less common."<br /><br />You can take a look on his post here: A <a href="http://blogs.msdn.com/pedram/archive/2007/10/07/a-performance-comparison-of-readerwriterlockslim-with-readerwriterlock.aspx">Performance Comparison of ReaderWriterLockSlim with ReaderWriterLock</a>Rotem Bloomhttp://www.blogger.com/profile/13853376077098966515noreply@blogger.com2tag:blogger.com,1999:blog-4153375873639804201.post-34472174075534442702008-04-14T16:48:00.000+03:002008-04-14T16:56:46.135+03:00Open VSS 2005 client without enter user name and passwordHi,<br />If you want to set VSS (Visual Source Safe) 2005 client default user name and password without type them all the time, you have to do the following steps:<br />1) right click on VSS 2005 client icon<br />2) select properties<br />3) on the "target" at the end add your desire user name and pssword as follow: <br />-Y<user name>,<password><br />So you target shuold look like something like:<br />"C:\Program Files\Microsoft Visual SourceSafe\ssexp.exe" -YMyUser,123456.<br /><br />This will open your source safe client with current user without opening the user password screen.Rotem Bloomhttp://www.blogger.com/profile/13853376077098966515noreply@blogger.com1tag:blogger.com,1999:blog-4153375873639804201.post-90448753941315385502008-04-13T09:47:00.000+03:002008-04-13T09:50:29.272+03:00LINQ to SQL Visual LINQ Query Builder Visual Studio 2008 Add-InVisual LINQ Query Builder is an add-in to Visual Studio 2008 Designer that helps you visually build LINQ to SQL queries. Functionally it provides the same experience as, for instance the Microsoft Access Query Builder, but in the LINQ domain.<br />You can read more here:<br /><a href="http://code.msdn.microsoft.com/vlinq/">LINQ to SQL Visual LINQ Query Builder</a>Rotem Bloomhttp://www.blogger.com/profile/13853376077098966515noreply@blogger.com0tag:blogger.com,1999:blog-4153375873639804201.post-52125638389659752242008-02-03T07:56:00.000+02:002008-02-03T07:59:09.980+02:00.NET Reflector add-ins.NET Reflector has a lot of useful add-ins. The list of all the add-ins can be found in CodePlex under the link below:<br /><a href="http://www.codeplex.com/reflectoraddins">.NET Reflector add-ins</a>Rotem Bloomhttp://www.blogger.com/profile/13853376077098966515noreply@blogger.com0tag:blogger.com,1999:blog-4153375873639804201.post-13584233529565313392007-11-04T22:22:00.000+02:002007-11-04T23:04:36.255+02:00Stored Procs performance are better then Dynamic SQL. Is it A myth?People still thinks Stored Procedures are faster, even if there is much evidence that shows otherwise.<br /><br />Andres Aguiar's wrote is his blog that: "Fortunately, when they go and ask the LinQ for SQL/Entities team they get the same answer than they get from me. They are not.<br />"<br /><br />To add more evidence, a couple of weeks ago he was in Redmond in a meeting with the ADO.NET team, and a Tech Lead from that team wrote the code below and said 'this is the fastest way to execute this SQL sentence with the .NET framework today'.<br /><br />SqlCommand cmd = sqlConnection.CreateCommand();<br />cmd.CommandText = @"<br /> SELECT Sales PersonId, FirstName, HireDate<br /> FROM SalesPerson as sp<br /> INNER JOIN Employee AS e ON sp.SalesPersonID = e.EmployeeID<br /> INNER JOIN Contact AS c ON e.EmployeeID = c.ContactID<br /> WHERE e.HireDate < @date";<br />cmd.Parameters.AddWithValue("@date", date");<br /><br />DbDataReader r = cmd.ExecuteReader();<br /><br />Why is this _faster_ than a stored procedure? Because the SQL Server engine could select a better execution plan depending on the value of the @date parameter.<br /><br />Even in that room there was people that were surprised.<br /><br /><strong>So here are some points why not using stored procedures:</strong><br />1. Stored Procedures are written in big iron database "languages" like PL/SQL (Oracle) or T-SQL (Microsoft). These so-called languages are archaic, and full of the crazy, incoherent design choices that always result from the torturous evolution of ten years of backwards compatibility. You really don't want to be writing a lot of code in this stuff. For context, JavaScript is a giant step up from PL/SQL or T-SQL.<br /> <br />2. Stored Procedures typically cannot be debugged in the same IDE you write your UI. Every time I isolate an exception in the procs, I have to stop what I am doing, bust out my copy of Toad, and load up the database packages to see what's going wrong. Frequently transitioning between two totally different IDEs, with completely different interfaces and languages, is not exactly productive. <br /><br />3. Stored Procedures don't provide much feedback when things go wrong. Unless the proc is coded interally with weird T-SQL or PL/SQL exception handling, we get cryptic 'errors' returned based on the particular line inside the proc that failed, such as Table has no rows. Uh, ok? <br /><br />4. Stored Procedures can't pass objects. So, if you're not careful, you can end up with a zillion parameters. If you have to populate a table row with 20+ fields using a proc, say hello to 20+ parameters. Worst of all, if I pass a bad parameter-- either too many, not enough, or bad datatypes-- I get a generic "bad call" error. Oracle can't tell me which parameters are in error! So I have to pore over 20 parameters, by hand, to figure out which one is the culprit. <br /><br />5. Stored Procedures hide business logic. I have no idea what a proc is doing, or what kind of cursor (DataSet) or values it will return to me. I can't view the source code to the proc (at least, without resorting to #2 if I have appropriate access) to verify that it is actually doing what I think it is-- or what the designer intended it to do. Inline SQL may not be pretty, but at least I can see it in context, alongside the other business logic. <br /><br /><strong>So why use Stored Procedures at all? Conventional wisdom says we do it because:</strong><br />1. Stored procedures generally result in improved performance because the database can optimize the data access plan used by the procedure and cache it for subsequent reuse. <br /><br />2. Stored procedures can be individually secured within the database. A client can be granted permissions to execute a stored procedure without having any permissions on the underlying tables. <br /><br />3. Stored procedures result in easier maintenance because it is generally easier to modify a stored procedure than it is to change a hard-coded SQL statement within a deployed component. <br /><br />4. Stored procedures add an extra level of abstraction from the underlying database schema. The client of the stored procedure is isolated from the implementation details of the stored procedure and from the underlying schema. <br /><br />5. Stored procedures can reduce network traffic, because SQL statements can be executed in batches rather than sending multiple requests from the client. <br /><br />one more interesting fact on Stored Procs:<br />Everyone who thinks stored procedures are pre-compiled, say "Aye!". Whoa, what a noise! For all of you who said "Aye!" a few seconds ago: open SqlServer's Books Online (v7 or v2000, doesn't matter), search for "cache execution plan". You'll find fine articles like "Execution Plan Caching and Reuse" and "SQL Stored Procedures". Let me just quote some lines from the "SQL Stored Procedures" article:<br /><blockquote>SQL Server 2000 and SQL Server version 7.0 incorporate a number of changes to statement processing that extend many of the performance benefits of stored procedures to all SQL statements. SQL Server 2000 and SQL Server 7.0 do not save a partially compiled plan for stored procedures when they are created. A stored procedure is compiled at execution time, like any other Transact-SQL statement. SQL Server 2000 and SQL Server 7.0 retain execution plans for all SQL statements in the procedure cache, not just stored procedure execution plans.<br /></blockquote><br /><br />So what do you think? Is it a myth? :-)Rotem Bloomhttp://www.blogger.com/profile/13853376077098966515noreply@blogger.com1tag:blogger.com,1999:blog-4153375873639804201.post-66243905307086784562007-11-04T22:02:00.000+02:002007-11-04T23:04:55.597+02:00.NET goodie #2: High Performance C# CodeThis article presents helpful tips for writing in-process .NET managed code that performs well. It's assumed that basic programming skills such as factoring control structures, pulling work outside of loops whenever possible, caching variables for reuse, use of the switch statement, and the like are known to the average reader. <br /><br /><strong>Working with Objects and Value Types</strong><br />Objects: A Double Whammy<br />Objects are expensive to use, partly because of the overhead involved in allocating memory from the heap (which is actually well-optimized in .NET) and partly because every created object must eventually be destroyed. The destruction of an object may take longer than its creation and initialization, especially if the class contains a custom finalization routine. Also, the garbage collector runs in an indeterministic way; there's no guarantee that an object's memory will be immediately reclaimed when it goes out of scope, and until it's collected, this wasted memory can adversely affect performance<br /><br /><strong>The Garbage Collector in a Nutshell</strong><br />It's necessary to understand garbage collection to appreciate the full impact of using objects. The single most important fact to know about the garbage collector is that it divides objects into three "generations": 0, 1, and 2. Every object starts out in generation 0; if it survives (if at least one reference is maintained) long enough, it goes to 1; much later, it transitions to 2. The cost of collecting an object increases with each generation. For this reason, it's important to avoid creating unnecessary objects, and to destroy each reference as quickly as possible. The objects that are left will often be long-lived and won't be destroyed until application shutdown.<br /><br /><strong>Lazy Instantiation/Initialization</strong><br />The Singleton design pattern is often used to provide a single global instance of a class. Sometimes it's the case that a particular singleton won't be needed during an application run. It's generally good practice to delay the creation of any object until it's needed, unless there's a specific need to the contrary - for instance, to pre-cache slow-initializing objects such as database connections. The "double-checked locking" pattern is useful in these situations, as a way to avoid synchronization and still ensure that a needed action is only performed once. Lazy initialization is a technique that can enhance the performance of an entire application through object reduction.<br /><br /><strong>Avoiding Use of Class Destructors</strong><br />Class destructors (implemented as the Finalize() method in VB.NET) cause extra overhead for the garbage collector, because it must track which objects have been finalized before their memory can be reclaimed. I've never had a need for finalizers in a purely managed application. <br /><br /><strong>Casting and Boxing/Unboxing Overhead</strong><br />Casting is the dynamic conversion of a type at runtime to another, and boxing is the creation of a reference wrapper for a value type (unboxing being the conversion back to the wrapped value type). The overhead of both is most heavily felt in collections classes, as they all - with the exception of certain specialized ones like StringDictionary - store each value as an Object. For instance, when you store an Int32 in an ArrayList, it is first boxed (wrapped in an object) when it is inserted; each time the value is read, it is unboxed before it is returned to the calling code. You can use generics to avoid Boxing/Unboxing.<br /><br /><strong>Trusting the Garbage Collector</strong><br />Programmers new to .NET sometimes worry about memory allocation to the point that they explicitly invoke System.GC.Collect(). Garbage collection is a fairly expensive process, and it usually works best when left to its own devices. The .NET garbage collection scheme can intentionally delay reclamation of objects until memory is available, and in particular longer-lived objects (those that make it to generation 1 or 2) may not be reclaimed for an extended period. Even a simple "Hello, world!" console application may allocate 15 MB or more of memory for its "working set." My advice: don't call GC.Collect() unless you really know what you're doing.<br /><br /><strong>Properties, Methods, and Delegates</strong><br />Avoiding Overuse of Property Getters and Setters<br />Most people don't realize that property getters and setters are similar to methods when it comes to overhead; it's mainly syntax that differentiates them. A non-virtual property getter or setter that contains no instructions other than the field access will be inlined by the compiler, but in many other cases, this isn't possible. You should carefully consider your use of properties; from inside a class, access fields directly (if possible), and never blindly call properties repeatedly without storing the value in a variable. All that said, this doesn't mean that you should use public fields!<br /><br /><strong>About Delegates</strong><br />Delegates are slower to execute than interface methods. Delegates are often used to introduce a level of indirection in code, but in almost all cases interfaces allow a cleaner design. Of course, it's impossible to completely shun delegates; the entire event-handling paradigm in .NET is based on them. Example 2 compares the performance of delegates and direct method calls.<br /><br /><strong>Minimizing Method Calls</strong><br />The .NET compiler is capable of performing many optimizations for release builds. One of them is called "method inlining." If method A calls method B and certain other conditions are met, such as the code in method B being small enough, the code from B will be copied into A during compilation. However, .NET won't or can't inline certain types of methods, such as virtual methods or methods over a certain size. Each method invocation/property access entails significant overhead, such as the allocation of a stack frame, etc. Of course, you should never repeatedly call a method for the same result on purpose, but you should also be mindful of the impact of method calls in general. <br /><br /><strong>Using the 'Sealed' Keyword</strong><br />Wherever extensibility is not required, you should use the sealed keyword. This makes your design easier to understand, as someone can tell at a glance if a certain class or method isn't meant to be extended or overridden. It also increases the chances that the compiler will inline code. <br /><br /><strong>Working with Collections</strong><br />Avoid Overuse of Collections<br />This might sound strange, but I'm not advocating working without data structures. The fact is, I've seen collections used many times when they don't actually simplify the code or provide any benefit at all. The single biggest avoidable use of collections is the use of an ArrayList when a simple array would suffice. It should be obvious that there's no way that calling a collection method - which may do significant work under the covers - can compare to something as simple as an array access for performance.<br /><br /><strong>for vs foreach</strong><br />The rumor abounds that the foreach loop is bad for performance. The truth is actually a little more complicated. Basically, foreach involves no performance penalty when used against arrays. However, when used against lists it involves the same overhead as creating an enumerator and using it within a try/catch block! Ildasm.exe may come in handy here to see what's going on. This isn't a complete killer, but if you do enough list access you may want to avoid it.<br /><br /><strong>Enumerators: Don't Go Overboard</strong><br />Just because the possibility exists for the enumerating of a collection doesn't mean you have to do it. For instance, the ArrayList class is useful as an array replacement; one of its best features is indexed element access. By using an enumerator with ArrayList, you hide its most useful features and introduce needless overhead. <br /><br /><strong>Avoid Overuse of Collection Wrappers</strong><br />A peek at the code in classes such as ArrayList shows that, to implement synchronized, fixed-size, and read-only versions of these, methods such as Synchronized() actually create a new wrapper list around the original one (this scheme was copied from Java). While this is nice from certain design standpoints, it degrades performance; the method-call overhead for each operation is multiplied. For a fixed-size, synchronized ArrayList, this overhead is tripled! Chances are, if you're working with a collection and need synchronization, the code using the collection is already synchronized. In any case, simply locking on the collection itself around every access turns out to be faster than using a synchronized wrapper.<br /><br /><strong>Working With Strings</strong><br />Don't Use String.Format() to Concatenate Strings<br />While string-formatting routines built into .NET are very useful for globalization and other tasks, they're not meant to be used for appending strings to each other.<br /><br /><strong>Use StringBuilder to Build Strings Inside Loops</strong><br />The StringBuilder class is basically an array list for string fragments; the StringBuilder takes care of expanding its internal char array, hiding this from the user. The use of this class is also specially optimized by the .NET compiler, making it impossible to duplicate this functionality with equivalent performance (for instance, by manipulating char arrays directly and converting to a string afterwards). <br /><br /><strong>Don't Use StringBuilder to Concatenate Small Numbers of Strings</strong><br />Many .NET developers who consider themselves well-versed in performance matters advocate the use of the StringBuilder class whenever possible. However, it's not the fastest approach for concatenating small numbers of strings. Actually, any number can be combined in a single statement, although the performance benefit tends to dwindle above five or six substrings. This is due to instantiation and destruction overhead for the StringBuilder instance, as well as method-call overhead involved in calling Append() once for every added substring and ToString() once the string is built. What are the alternatives? Plus-sign concatenation and the String.Concat() method are equivalent; I prefer plus signs for readability. Note that this cannot be used across loops because the entire concatenation must occur within a single statement.<br /><br /><strong>Don't Be Afraid to Use String Literals</strong><br />Many developers incorrectly assume that a new object is created for every string literal, and, therefore, avoid their use. In some cases, using string literals directly in your code is a better approach than using string constants! It can make the code easier to understand and has no adverse impact on performance. This is due to the use of the .NET interned string table; this table maintains a String instance for every known string and reuses this instance whenever the same character sequence is used as a literal. See the documentation of the String.Intern() method for more details.<br /><br /><strong>Threading</strong><br />In itself, this is a huge topic. Most easy-to-apply, synchronization-related optimizations focus on minimizing the amount of code executing inside synchronized blocks, which may involve moving some code from inside to outside such blocks. In some cases, thoughtful programming may allow elimination of synchronization entirely from a class (see "Immutable Objects"). <br /><br />Performing a multithreaded micro-benchmark is more complicated than running a simple loop. In the simplest method, multiple threads are spawned, each looping over the benchmarked code; the reading is not started until all threads are successfully executing the code, and is terminated before the threads are shut down. <br /><br /><strong>Thread Reuse</strong><br />Newbie programmers make the common mistake of spawning a new thread for every request or other action. This can result in worse performance than using a single-threaded approach; the relative performance degradation is worse the quicker the individual requests can be serviced. <br /><br /><strong>Writing Your Own Threading Code</strong><br />Even better than the .NET thread pool, with its dependence on delegates, is to write your own threading code. Instead of using QueueUserWorkItem(), you typically write your own queueing code to coordinate work items. This also allows other benefits such as priority-based queueing. <br /><br /><strong>Immutable Objects</strong><br />Immutable objects are objects in which the data cannot be changed. In most cases, this is achieved by setting all fields in constructor methods and providing only property getters and/or methods that retrieve data from the object, without any mutator logic whatsoever. Many classes in the .NET Common Type System are immutable: System.String, System. Drawing.Font, etc. In addition, care should be taken that any values returned from property accessors, etc. are immutable as well. Otherwise, this data may be copied to insure the integrity of the object itself. <br /><br /><strong>Data Copying</strong><br />This flies in the face of the advice given earlier, to minimize the use of objects. However, it's really the other side of the coin from immutable objects. Object copying allows you to use the data in a non-immutable object, but in a way that still completely avoids synchronization. The more highly multithreaded the environment, the more strategies like this make sense. <br /><br /><strong>Read-Write Locks</strong><br />Synchronization issues in managed code mirror those in databases. In some situations, optimistic concurrency strategies can be used; in some dirty reads are acceptable, etc. For situations in which a structure is seldom updated and often read, the ReaderWriterLock class can give significant performance benefits over simple synchronization. It allows either multiple read access or single write access at once.<br /><br /><strong>Minimizing Synchronized Blocks</strong><br />The use of the [MethodImpl Attribute(MethodImplOptions.Synchronized)]attribute should be avoided, as it always locks an entire method and is also non-standard C# usage. Instead, the lock keyword or one of the System.Threading classes should be used. Wherever possible, adjust the start of a synchronized section forward and the end backward. Do whatever you can to decrease the number of synchronized operations. <br /><br />Hope you find this article interesting. I think it really important to understand and know how to right efficient C# code.<br /><br />Yours,<br />Rock Rotem BloomRotem Bloomhttp://www.blogger.com/profile/13853376077098966515noreply@blogger.com2tag:blogger.com,1999:blog-4153375873639804201.post-85138231308507751422007-11-01T16:32:00.000+02:002007-11-01T16:45:05.308+02:00.NET goodie #1: Declaring variables inside or outside of a loop impact performance?Did you ever wonder......<br />Is there any difference in terms of performance between declaring a variable outside of a loop like this... <br /><br />string str = ""; <br /><br />for (int i = 0; i < 10; i++)<br /><br />{<br /><br /> str = "hello";<br />} <br /><br /><br />...versus declaring the variable within a loop and using it like this:<br /><br /><br />for (int i = 0; i < 10; i++)<br /><br />{<br /><br /> string str = "hello";<br />} <br /><br />So what do you think???!!! The asnwer in this case will be:<br /><br />The first example is actually slower, because it causes one more string allocation to occur. In c# the "creation" of variables is always moved to the top of methods in the compiled IL. Therefore your second example is exactly the same performace as:<br /><br /> string str;<br /><br /><br />for (int i = 0; i < 10; i++)<br /><br />{<br /><br /> str = "hello";<br />} <br /> <br /><br />Notice this does not assign an initial value to str of "".Rotem Bloomhttp://www.blogger.com/profile/13853376077098966515noreply@blogger.com1tag:blogger.com,1999:blog-4153375873639804201.post-22662655612285219122007-11-01T15:50:00.000+02:002007-11-01T16:00:08.152+02:00SQL Formatter, beautified SQL statementsHi,<br />There is a free SQL formatter that supports several databases like:<br />oracle, mssql, db2, mysql etc...<br /><br />You can take a large SQL statement with parameters and the "SQL Formatter" will beautified the SQL statement for you. It will add line breaks and more.<br /><br />You can found the cool SQL Formatter <a href="http://www.gudusoft.com/sqlformatter.aspx">here</a>.<br /><br />Great tool for SQL debugging.Rotem Bloomhttp://www.blogger.com/profile/13853376077098966515noreply@blogger.com0tag:blogger.com,1999:blog-4153375873639804201.post-74495940573443157992007-10-30T10:11:00.000+02:002007-10-30T10:17:55.080+02:00Cross site Ajax plugin for PrototypeThierry Schellenbach has implemented a Prototype plugin that allows you to do <a href="http://www.mellowmorning.com/2007/10/25/introducing-a-cross-site-ajax-plugin-for-prototype/">cross site remoting</a> using the dynamic script tag method that other frameworks such as Dojo and jQuery have supported for awhile.<br /><br />You can implement this by simply setting crossSite: true as a config parameter to Ajax.Request.<br /><br />Code example:<br /><br /> new Ajax.Request('myurl', {<br /> method: 'GET',<br /> crossSite: true,<br /> parameters: Form.serialize(obj),<br /> onLoading: function() {<br /> //things to do at the start<br /> },<br /> onSuccess: function(transport) {<br /> //things to do when everything goes well<br /> },<br /> onFailure: function(transport) {<br /> //things to do when we encounter a failure<br /> }<br /> });<br />the full information can be found on Thierry Schellenbach <a href="http://www.mellowmorning.com">blog</a>.Rotem Bloomhttp://www.blogger.com/profile/13853376077098966515noreply@blogger.com0tag:blogger.com,1999:blog-4153375873639804201.post-61706437594918348232007-08-07T12:00:00.000+03:002007-08-07T12:04:43.559+03:00JavaScript Image effect libraryChristian Effenberger wrote nice JavaScript library that let you apply an image mask (in the form of another image) to any image via unobtrusive CSS.<br /><br />You can take a peek:<br />http://www.netzgesta.de/corner/Rotem Bloomhttp://www.blogger.com/profile/13853376077098966515noreply@blogger.com0tag:blogger.com,1999:blog-4153375873639804201.post-63327749289459313762007-07-17T12:44:00.000+03:002007-07-17T12:48:56.607+03:00JavaScript accordion componentKevin Miller wrote nice accordion component that use protoype.js.<br />The component work both ways horizontal and vertical.<br /><br />Information can be found on the link below:<br /><a href="http://stickmanlabs.com/accordion/">accordion component</a>Rotem Bloomhttp://www.blogger.com/profile/13853376077098966515noreply@blogger.com0tag:blogger.com,1999:blog-4153375873639804201.post-83222483031277729642007-07-11T10:57:00.000+03:002007-07-11T11:41:12.122+03:00Tooltip effect using Script.aculo.usNick Stakenburg has written a nice simple tooltip built on Script.aculo.us called <a href="http://www.illustate.com/playground/scriptaculous/tooltip/">Effect.Tooltip</a><br /><br />Cool and very simple to useRotem Bloomhttp://www.blogger.com/profile/13853376077098966515noreply@blogger.com0tag:blogger.com,1999:blog-4153375873639804201.post-61701526815754234102007-06-24T15:15:00.000+03:002007-06-24T15:20:05.522+03:00DP_Debug JavaScript Debugging ExtensionsJim Davis has released a simple little JavaScript dumping library, <a href="http://www.depressedpress.com/Content/Development/JavaScript/Extensions/DP_DeBug/"> DP_Debug</a>, that supports circular and recursive references, most system objects and specifies certain otherwise ambiguous conditions.<br /><br />In addition the library allows for simple access to Query String and Cookie values and provides basic logging, code timing support and program integration support.<br /><br />Full information can be found:<br />http://www.depressedpress.com/Content/Development/JavaScript/Extensions/DP_DeBug/<br />:-)Rotem Bloomhttp://www.blogger.com/profile/13853376077098966515noreply@blogger.com0tag:blogger.com,1999:blog-4153375873639804201.post-60999104344116183062007-05-04T18:45:00.000+03:002007-05-04T18:53:45.791+03:00Jayrock JSON and JSON-RPC for .NET<a href="http://jayrock.berlios.de/">Jayrock</a> is a modest and an open source implementation of JSON and JSON-RPC for the Microsoft .NET Framework, including ASP.NET. What's so modest about it? Well, modest as in plain and basic and no work of genius.<br /><br />A developer creates a helloworld.ashx that contains your server side logic:<br /><br />C#:<br /><br /><br />namespace JayrockWeb<br />{<br />using System;<br />using System.Web;<br />using Jayrock.Json;<br />using Jayrock.JsonRpc;<br />using Jayrock.JsonRpc.Web;<br /><br />public class HelloWorld : JsonRpcHandler<br />{<br />[ JsonRpcMethod("greetings") ]<br />public string Greetings()<br />{<br />return "Welcome to Jayrock!";<br />}<br />}<br />}<br /><br />Then you can access the file asking for a proxy via helloworld.ashx?proxy and you will see a test page. From code you can now:<br /><br />JavaScript:<br /><br />var s = new HelloWorld();<br /><br />alert("sync:" + s.greetings());<br /><br />s.greetings(function(response) {<br />alert("async:" + response.result) });Rotem Bloomhttp://www.blogger.com/profile/13853376077098966515noreply@blogger.com0tag:blogger.com,1999:blog-4153375873639804201.post-17487468127275417502007-04-26T17:48:00.000+03:002007-04-26T18:00:14.126+03:00Validators inside UpdatePanel won't work as expectedHi,<br />If you work with controls such as .NET Validators placed inside an UpdatePanel you probably know they won't work as expected.<br /><br />Until Microsoft will solve this issue you can use the "ASP.NET AJAX Validators" that can be download from here:<br />1) Site 1: http://blogs.msdn.com/mattgi/attachment/1516974.ashx<br />2) Site 2: http://forums.asp.net/forums/storage/1007/1545781/Validators.zip<br /><br />More information on <a href="http://forums.asp.net/thread/1545781.aspx">asp.net forums</a><br /><br />I hope it helps :-)<br />Bye RotemRotem Bloomhttp://www.blogger.com/profile/13853376077098966515noreply@blogger.com0tag:blogger.com,1999:blog-4153375873639804201.post-58404237183788357082007-04-26T17:43:00.000+03:002007-04-26T17:47:39.922+03:00Controls that Are Not Compatible with UpdatePanel ControlsHi All,<br /><p>The following ASP.NET controls are not compatible with partial-page updates, and are therefore not supported inside an <a href="http://ajax.asp.net/docs/mref/T_System_Web_UI_UpdatePanel.aspx">UpdatePanel</a> control:</p><br /> <ul><br /> <li><br /> <p><br /> <a href="http://msdn2.microsoft.com/en-us/k5c13faz">TreeView</a> and <a href="http://msdn2.microsoft.com/en-us/07b8w058">Menu</a> controls.</p><br /> </li><br /> <li><br /> <p>Web Parts controls. For more information, see <a href="http://msdn2.microsoft.com/en-us/library/ab78a66e-9feb-4391-b3c3-8c07555e2308">ASP.NET Web Parts Controls</a>.</p><br /> </li><br /> <li><br /> <p><br /> <a href="http://msdn2.microsoft.com/en-us/ysf0192b">FileUpload</a> controls when they are used to upload files as part of an asynchronous postback. </p><br /> </li><br /> <li><br /> <p><br /> <a href="http://msdn2.microsoft.com/en-us/4w7ya1ts">GridView</a> and <a href="http://msdn2.microsoft.com/en-us/7z482d0y">DetailsView</a> controls when their EnableSortingAndPagingCallbacks property is set to <span class="keyword">true</span>. The default is <span class="keyword">false</span>.</p><br /> </li><br /> <li><br /> <p><br /> <a href="http://msdn2.microsoft.com/en-us/t863ehhh">Login</a>, <a href="http://msdn2.microsoft.com/en-us/t92zy5x0">PasswordRecovery</a>, <a href="http://msdn2.microsoft.com/en-us/s1xhe282">ChangePassword</a>, and <a href="http://msdn2.microsoft.com/en-us/6s8b6814">CreateUserWizard</a> controls whose contents have not been converted to editable templates.</p><br /> </li><br /> <li><br /> <p>The <a href="http://msdn2.microsoft.com/en-us/9ze89as6">Substitution</a> control.</p><br /> </li><br /> <li><br /> <p>Validation controls, which includes the <a href="http://msdn2.microsoft.com/en-us/z8fh4ax6">BaseCompareValidator</a>, <a href="http://msdn2.microsoft.com/en-us/fczazdzk">BaseValidator</a>, <a href="http://msdn2.microsoft.com/en-us/d7365skb">CompareValidator</a>, <a href="http://msdn2.microsoft.com/en-us/303z9x0x">CustomValidator</a>, <a href="http://msdn2.microsoft.com/en-us/ccs7ftb0">RangeValidator</a>, <a href="http://msdn2.microsoft.com/en-us/sffh7429">RegularExpressionValidator</a>, <a href="http://msdn2.microsoft.com/en-us/ycxs7t4x">RequiredFieldValidator</a>, and <a href="http://msdn2.microsoft.com/en-us/xd4c894c">ValidationSummary</a> control.</p><br /> </li><br /> </ul><br /> <p>Controls that are incompatible with partial-page rendering can still be used on a page outside <a href="http://ajax.asp.net/docs/mref/T_System_Web_UI_UpdatePanel.aspx">UpdatePanel</a><br />controls. Additionally, in some cases you can use the controls in a<br />specific way to make them compatible with partial-page updates. For<br />example, you can use the <a href="http://msdn2.microsoft.com/en-us/t863ehhh">Login</a>, <a href="http://msdn2.microsoft.com/en-us/s1xhe282">ChangePassword</a>, or <a href="http://msdn2.microsoft.com/en-us/t92zy5x0">PasswordRecovery</a> controls inside an <a href="http://ajax.asp.net/docs/mref/T_System_Web_UI_UpdatePanel.aspx">UpdatePanel</a><br />control if you can convert their contents to templates. (If you are<br />using Visual Studio, in Design view you can convert the controls by<br />using smart-tag menu commands such as <span class="ui">Convert to Template</span> or <span class="ui">Customize Create User Step</span>.)<br />When you convert these controls into editable templates, the validation<br />controls that are used in the control are defined declaratively by<br />using markup in the page. To make the validators compatible with an <a href="http://ajax.asp.net/docs/mref/T_System_Web_UI_UpdatePanel.aspx">UpdatePanel</a> control, set the <a href="http://msdn2.microsoft.com/en-us/7t054e90">EnableClientScript</a> property of the validators to <span class="keyword">false</span>.<br />This disables the client script that would ordinarily be used to<br />perform validation in the browser. As a result, during an asynchronous<br />postback, the validators perform validation on the server. However,<br />because only the content of the <span class="nolink">UpdatePanel</span> is refreshed, the validators can provide the kind of immediate feedback that is ordinarily provided by client script.</p><br /> <p>To use a <a href="http://msdn2.microsoft.com/en-us/ysf0192b">FileUpload</a> control inside an <a href="http://ajax.asp.net/docs/mref/T_System_Web_UI_UpdatePanel.aspx">UpdatePanel</a> control, set the postback control that submits the file to be a <a href="http://ajax.asp.net/docs/mref/T_System_Web_UI_PostBackTrigger.aspx">PostBackTrigger</a> control for the panel.</p><br /> <p>All other controls work inside <a href="http://ajax.asp.net/docs/mref/T_System_Web_UI_UpdatePanel.aspx">UpdatePanel</a> controls. However, in some circumstances, a control might not work as expected inside an <a href="http://ajax.asp.net/docs/mref/T_System_Web_UI_UpdatePanel.aspx">UpdatePanel</a> control. These circumstances include the following:</p><br /> <ul><br /> <li><br /> <p>Registering script by calling registration methods of the <a href="http://msdn2.microsoft.com/en-us/0skaxdwf">ClientScriptManager</a> control.</p><br /> </li><br /> <li><br /> <p>Rendering script or markup directly during control rendering, such as by calling the <a href="http://msdn2.microsoft.com/en-us/1463ysyw">Write(String)</a> method.</p><br /> </li><br /> </ul><br /> <p>If the control calls script registration methods of the <a href="http://msdn2.microsoft.com/en-us/0skaxdwf">ClientScriptManager</a> control, you could use corresponding script registration methods of the <a href="http://ajax.asp.net/docs/mref/T_System_Web_UI_ScriptManager.aspx">ScriptManager</a> control instead. In that case, the control can work inside an <a href="http://ajax.asp.net/docs/mref/T_System_Web_UI_UpdatePanel.aspx">UpdatePanel</a> control.</p><p> </p>Rotem Bloomhttp://www.blogger.com/profile/13853376077098966515noreply@blogger.com0tag:blogger.com,1999:blog-4153375873639804201.post-25790138792116960882007-04-14T14:14:00.000+03:002007-04-14T14:23:44.369+03:00Prototype Library that helps store JSON data in cookiesLalit Patel wrote JavaScript Library that helps to use JSON to store data in cookies.<br />The nice library is built on top of Prototype and gives you a simple API to put and get JSON values into cookies.<br /><br />Full information and download:<br /><a href="http://www.lalit.org/lab/jsoncookies/">Yummy JSON Cookies (using Prototype)</a><br /><br /><strong>Code Example:</strong><br />var jar = new CookieJar({ <br /> expires:3600, // seconds <br /> path: '/' <br />}); <br /><br />var dog = {name: 'Jacky', breed: 'Alsatian', age:5}; <br /> <br />jar.put('mydog', dog); <br />mydog = jar.get('mydog'); <br /> <br />alert("My dog's name is " + mydog.name); <br />alert("He is " + mydog.age + " years old"); <br />alert("He is an " + mydog.breed);<br /><br />Enjoy :-)Rotem Bloomhttp://www.blogger.com/profile/13853376077098966515noreply@blogger.com0