Sunday, January 30, 2011

Spell Checker Extension For VS.NET 2010

OK there is a really useful extension available for free to VS.NET 2010 named "Spell Checker". It shows misspelled words within commented sections with the traditional red squiggly line under the word as seen in Office products. Obviously correctly spelled words are not critical to an applications performance, but well formed internal documentation that reads well such as commenting is important.

You can download it for free by going to Tools -> Extension Manager and then select 'Online Gallery' from the left-hand side. In the search box type in the top-right hand corner, type in "spell checker" and search. The 1st extension created by Roman Golovin and Michael Lehenl (thank you) named "Spell Checker" is the one to download and install (shown below).



As you can see below, just hover the mouse over the misspelled word for the little box to display, which upon clicking will bring up the properly spelled words. There is also the ability to "Add to dictionary" which is nice, because often many words we use are technical and not in the dictionary.


It can also be downloaded directly from the link below:
Spell Checker Extension for VS.NET

Wednesday, January 26, 2011

Use LINQ To Get The SUM Or Total Of A Property Value On An Object Collection

If you have an object collection say List(Of <T>) and you want to add up all of the values of a single property of the proper type (Integer, Double, etc.), you can do this using LINQ and a lambda expression. We will use the SUM LINQ Extension Method directly on the collection that implements IEnumerable(T), and provide it an anonymous function that takes the property value to be added together. So here is the code using an object collection which I had already populated previously:

'Use the LINQ SUM Extension Method to iterate over the collection and extract the total.
Dim Total = MyDataCollection.Sum(Function(x) x.PropertyOnXToSumTogether)
The anonymous function takes a parameter which will represent the individual instance of the collection, and then uses the property designated to be iterated over and added to the total.

Using Fiddler With VS.NET And The Local Web Development Environment 'Cassini'

If you use Fiddler to monitor traffic to your web sites, you have probably found it useful for debugging issues like 404's or 403's etc. However if you want to have Fiddler show traffic from sites you are debugging through your local VS.NET web development server, then you need to add a '.' period after the word 'localhost' in the URL. Also make sure to add it prior to the designated port as displayed below.

After refreshing the page, you can see the output in Fiddler.


Monday, January 24, 2011

How To: Scroll To The Bottom of an ASP.NET Web Page After Postback

Not a post about bleeding edge technology here, but just a little technique I ran across here for ASP.NET web pages. I needed a way to have the focus shift to the bottom of a page after a button near the top of the screen was pressed. Unfortunately setting focus via Page.SetFocus(Me.BottomOfPageServerControl.ClientID) did kind of work, but only shifted to the top of the control, and the control had a large height. Therefore the scroll bar was still not at the bottom, and the resulting pertinent data in my situation was at the bottom of the page.

The solution? Place a server control at the bottom of the page with CSS properties that prevent it from physically being viewed, don't affect scroll bars, and yet can still have focus shifted to it.

So begin by placing an ASP.NET button (or any other small server control) at the bottom of your markup, and add some inline styling to be most compact and streamlined if you wish:

<!--Button will render, but not actually be visible to the client-->
<asp:Button ID="ui_btnPageBottom" runat="server" Text="Button" style="margin-left:-999px;" />
Notice above the styling places it off the page, but the button will not create any lengthy scroll bars, etc. Lastly, add the code server side on the 'Finally' of the button click event to shift focus to the bottom of the page:

Try
'Code
Finally
'Regardless of outcome, set focus to the bottom of the page
Page.SetFocus(Me.ui_btnPageBottom.ClientID)
End Try
That's it! I am sure there are at least 20 ways to do this, including a purely client side solution with JavaScript, but this was 2 lines of code to accomplish what I needed.

Thursday, December 9, 2010

Tip On Preventing Web Discover Service Error

I have been using .asmx web services for about 7 years now, and have not run into too many issues while consuming valid URLs in VS.NET. And although I am not creating new .asmx services today because I prefer (as should you) WCF services, I still have some that have yet to be converted.

One service in particular was giving me issue upon consuming in VS.NET 2010 with the following error message in a dialog box named "Web Discovery Service":

"Unable to download following files from . https://myservice08.mycompany.com:3567/MyService.asmx?wsdl Do you want to skip these files and continue?"

Well I found very little documentation on this issue (actually only 2 sites on Google), but I did manage to find the solution which on the surface appears quite obvious after explanation.

It started when I moved an upgraded copy of this service (Framework 4.0, IIS 7.5, etc) to a new server. However I had the "myservice" SSL certificate imported and applied to this service in IIS that was named "myservice08" (name was only for testing). I knew ahead of time that I would get certificate mismatch errors ("myservice" <> "myservice08") because the SSL cert didn’t match the sites host name, but I figured for testing I could bypass and be OK. Well this ended up being a showstopper, because not only did I get the warning about the SSL certificate not matching the host name upon consumption, I got the other error reported above as well (which is not nearly as indicative of the SSL mismatch being as issue). In fact I was really thrown off because the .asmx service would still come up in the browser showing the wsdl just fine.

So if you ever are working with legacy .asmx services and are receiving the same error I did upon consuming within VS.NET, make sure that the SSL cert name and the hostname of the service are identical.

Tuesday, November 30, 2010

How To Use Impersonation in ASP.NET Configuration With IIS 7.5 And Integrated Pipeline

While working on a few applications that have Impersonation="True" in my ASP.NET configuration, I ran into the following error upon deployment to an IIS 7.0 or 7.5 server:

"HTTP Error 500.24 - Internal Server Error An ASP.NET setting has been detected that does not apply in Integrated managed pipeline mode"

This is essentially a 'flag' warning that is raised when using Impersonation in your web.config and deploying to an IIS 7.0+ server using an Application Pool with Integrated Pipeline. If this configuration was mistakenly added, you can remove the configuration or set it to 'False' to fix the problem. There are (2) solutions for those that do need Impersonation="True".

1. Switch the Application Pool on IIS to use a 'Classic' Pipeline.
2. Add the following configuration to your web.config under the <system.webserver> section:

<system.webServer>
<!--When using 'Integrated Pipeline' on IIS on the server, and if your application does not rely on impersonating the requesting user in the 'BeginRequest' and 'AuthenticateRequest' stages (the only stages where impersonation is not possible in Integrated mode), but still requires Impersonation in other areas of the application, ignore this error (500 - Internal Server Error) by adding the following to your application’s web.config-->
<validation validateIntegratedModeConfiguration="false"/>
</system.webServer>

I actually used the explanation from the following helpful IIS Blog link (here) to create the XML comment above the configuration, and I reccomend reading it for a more in depth explination direct from an IIS Program Manager.

Monday, November 22, 2010

Book Review: Professional ASP.NET Design Patterns By Scott Millett

Let me begin by stating that Professional ASP.NET Design Patterns is a fantastic book that was worth every minute I spent reading it. The author, Scott Millett, is a great community leader and extends himself in several ways including spending time on the forums contributing to others into his strong insight of Domain Driven Design, Architecture, and Design Patterns. He has extended that helpfulness by writing this book that takes a dive into Design Patterns and Architecture from an ASP.NET UI centric view. However I would not get too fixated on the 'ASP.NET' in the title as probably more than half of this book could just as well have been called "Professional .NET Design Patterns" as it provides design patterns that are truly useful to all types of .NET applications once moving below the topmost UI layer. There are several chapters devoted to ASP.NET patterns including MVC which makes this still focused mostly on ASP.NET, but I would still recommend this book to WinForms and other SmartClient developers as well.

This book's target audience is broad and could reach to several different types of software engineers. It is probably suited best for Senior Engineers, Architects, Leads, or generally seasoned developers. It is not really an introductory book (this is a good thing; there are plenty of those books out there already), so if you don't know what acronyms like OOP, OOD, UI, BLL, or DAL mean at a minimum already then you may want to read something along the lines of an introduction to Object Oriented Programming book 1st to gain some traction. This is however a terrific book for those that do have a lot of experience with a traditional 3-layer logical architectures, and are looking to bridge the gap to more sophisticated architectures using Domain Driven Design and other implementations of either Martin Fowler's or the GoF design patterns within.

Scott does a wonderful job of layering the book (chapters) as you would an application. Each chapter takes either a single layer or design pattern and goes into detail on its responsibilities, relationship to other layers, and implementation with easy to follow along code samples. In fact I highly recommend downloading the code samples from the WROX website (WROX Code Download) The entire set of code samples are in C#, but don't let this slow up any VB.NET devs out there. I am actually a VB.NET developer (C# in the past) but we all know that you don't get too far in this industry without reading both so this should not be any problem.

The 1st third of the book (roughly) concentrates mostly on individual logical layers of an application and how they work together to build an application. Within each layer, there are examples of Design Patterns (both Fowler and GoF) that are used and shown why they are useful within that particular layer. There is also a section on IoC and DI which I really enjoyed and are reoccurring patterns in the layers throughout the book. The 2nd third of the book concentrates mostly on ASP.NET architectures and techniques like MVC, MVP, and AJAX patterns. The last third is devoted to a case study example that uses the knowledge gained from the previous chapters. The book reads and flows extremely well and was one of the reasons I enjoyed reading it so much.

I will also note that this is a great book for those of you familiar or have read the GoF book Design Patterns Elements of Reusable Object Oriented Software. As we all know code examples used to conceptually explain design patterns are not always critical, but Scott's book puts a fresh '.NET' perspective on several of the GoF patterns which is really nice. This helps to see how these patterns apply directly in .NET instead of taking the SmallTalk or C++ examples from the GoF book and translating them into .NET.

The book wraps up with a full case study example putting all of the chapters together (Agath's e-commerce store). This again strengthens the flow of the book with an extended example using everything learned from the previous chapters, This solution is included in the 'Chapter 14' folder in the downloadable code and is a nice reference to show everything from the book.

Well I will wrap this review up by saying this book is one for the shelf of 'Top Reference' books that go right next to the development machine. This is one of those books that you think, "How do I do that in the Repository Layer...", and then pick up the book to get a refresher. I would definitely recommend this book and keep an eye out for future books from Scott Millett. Nice Job!