Archive for September, 2010
Updated: Syntax Highlighting for ScrewTurn Wiki
ScrewTurn Wiki is an excellent free wiki engine that I love and that I’ve written a source code syntax highlighter plugin for.
The plugin literally just hooks in the client-side SyntaxHighlighter by Alex Gorbatchev. Alex has published a new version some months ago. The version 3 has better support for copy&paste without the need for buttons in the toolbar, and some added elegance in its internal structure and extensibility. The new version of the plugin simply uses this version; there’s not much else to be said about it.
If you’re using the ScrewTurn wiki plugin with your own downloaded version of the SyntaxHighlighter, it’d be best to update to version 3 as well. You can use ScrewTurn’s auto-update functionality to obtain the new version of the plugin.
Links
Squeezing some memory for classic ASP
Some colleagues of mine are maintaining a large business application that uses the technology stack of classic ASP 3.0 and COM components written in Visual Basic 6. Changing the application itself to use more recent technology is not an option at the moment. Being a totally non-cutting-edge task, it turned out quite interesting in the end.
Memory limitations
There’s one really problematic aspect in running this app: some features are quite hungry for memory. With a lot of concurrency going on, memory consumption has become the reason to add more servers to the server farm for some times now. Each of the servers is running a 32bit version of Windows Server, and is equipped with 4GB of RAM.
Now on a 32bit OS, the maximum amount of usable memory is 4GB in total, and half of that is reserved for the kernel. That makes 2GB left for all applications altogether.
IIS will not allocate all of this to applications. There is a memory limit that throttles the memory usage to 60% of what’s available (see here for more background, thanks Jason Follas) This percentage setting can be set in the machine.config file: . Increasing this limit to 75% or 80% is doable on a dedicated web server (the key word is “dedicated” here. No additional services, please.)
Using the 4 gigabyte tuning (“/4GT”, formerly known as “/3G” boot switch), it is possible to let Windows use only 1GB for the OS kernel, and yield 3GB for applications. While this increases the memory available for the IIS worker processes, the OS becomes quite limited; running a load test after applying the setting is paramount (and I always found it scary).
These optimizations are good for some extra percent – but we were looking for a solution that would get the problem resolved for good. And that does not go without jumping the 4GB limitation on the server.
PAE looked promising, but with 64bit processors and operating systems well adopted, PAE looked like a bridging legacy technology, and moving to a 64bit environment a more forward-facing step.
Classic ASP on 64bit
There is no 64bit version of VB6, and there never will be. So if an ASP application needs to instantiate components written with VB6, it needs to run in a 32bit process. IIS 7 and IIS 7.5 support switching a worker process to 32bit on the Application Pool. This means that individual applications can be run as 32bit processes. But is that a good idea, compared to simply running on a 32bit operating system?
On a 64bit machine, there is of course a limit in how much memory can be assigned to a 32bit process that is imposed by the length of the address pointer, this limit is 4GB. But in contrast to the 32bit OS where this applies to the whole operating system, these 4GB are available to every process individually. So instead of 1.5 GB, there are 4 GB of memory available out of the box, without any advanced settings or modification.
Now to utilize the full amount of memory that a machine has to offer, you can run multiple of these processes. When a load balancing solution is already in place, you can simply replace the previously used physical machines by web sites on the same server, each with a different IP address. Each of the web site runs in a different application pool, in its own 32bit process. Each of these processes manages its own 4GB of memory. As IIS does not allocate all of this memory, it’s safe to run four web sites on a server with 16GB of RAM.
Application Request Routing as local load balancer
If no existing load balancer is in place, IIS Application Request Routing (“ARR”) can be used to distribute requests between the web sites on the server locally. With this model, the web sites that run the ASP applications are bound against local IP addresses: in addition to the external IP address on a physical network adapter, a loopback adapter is installed. A range of private IP addresses (like 192.168.42.x/255.255.255.0) is assigned to the loopback adapter. In addition to the web sites for the ASP application, there needs to be one web site that is bound against the external IP address. This web site should point to an empty directory so that there is no interference between resources available on that web site, and URLs that are handled by the application web sites.
In the configuration of ARR, a web farm addresses each of the web sites on the server as an individual server. ARR creates a default routing rule that routes all incoming requests to the web farm that consists of the local web sites. There is a problem with this rule however: because the application web sites are local, ARR will recognize requests coming into these sites too. This creates a closed loop. ARR needs to ignore requests that come into the local web sites: a condition is added to the default routing rule that it only kicks in when the server IP address is not within the local network address range used for the application web sites.
If the classic ASP session is used, clients need to be sent to the same web site every time; the ASP session is stored in the context of the worker process. This is achieved by activating Client Affinity in ARR – ARR sets a cookie itself, and uses this cookie to determine the web site to send clients to.
Format all files in project
Yesterday I discovered an awesomely useful Visual Studio macro: it applies that code formatting rules to all files in the whole solutions. Code formatting includes indentation, spacing, tabs vs. spaces: all the things that people tend to get irate about. By default, these formatting rules are applied only when you write new code. To apply them to an existing page, there’s the command Edit --> Format Document with the keyboard chord Ctrl-K, Ctrl-D. Now this macro goes and opens all files and applies this command – a very good idea to do before a major commit.
Sub FormatProject()
For Each Proj As Project In DTE.Solution.Projects
For Each Item As ProjectItem In Proj.ProjectItems
Dim window As EnvDTE.Window
Try
window = Item.Open()
window.Activate()
DTE.ExecuteCommand("Edit.FormatDocument", "")
Catch ex As Exception
End Try
Next
Next
End Sub
Found as a comment by saraford to this MSDN post from 2005.
So are you a MEF type of guy?
When I first looked into MEF, I could not help but share the sentiment that others had also expressed: it felt a little unclean. Not as dirty as a GOTO, but there was some RADish smell about it.
Any dependency injection (“DI”) framework does at least one thing: resolve the location of a piece of code so that other pieces of code do not have to know where it is. A classical DI framework like Spring.NET or Unity share a standard approach:
- Every service (a class that does something, and not only contains state) implements an interface.
- The exact type of the service – its name and assembly – and initialization parameters are added to a registry. While XML files are often used for defining this registry, there are other flavors as well; in any case, there is a central and explicit definition of what the application consists of.
- Everyone who uses the service exposes a dependency on it via its interface. A property of a constructor parameter is used by the DI framework to pass an instance of the service.
Some DI framework use attributes to define which property is relevant for the DI container, some don’t. Some use XML registry files, others offer a catalog definition by code. A variety of frameworks is available in production quality. So the choice of the perfect DI framework often becomes a highly subjective matter, largely influenced by a personal style. My favorite flavor is Spring.NET, no attributes, XML registry, and autowire by name.
Style aside, when a project has been developed with a dependency injection framework in place, it is likely that:
- It lends itself to unit testing. A DI framework promotes the idea of decoupling units of functionality. This makes writing tests after the implementation significantly easier. (Now let’s not argue if it’s better to write tests before implementation and accept that a lot of developers just do it that way.)
- There is decent separation of concerns. A key principle of DI is that you pass dependencies as interface implementations – this alone makes you think about what the correct interfaces are.
- You know what is inside the application. The registry can be read like the yellow pages of your application: what services exist, and even – if you stick to a verbose style without autowiring – where they are used.
Now comes MEF. With MEF, there is no registry; it just searches through all that is there and dynamically composes its catalog. In addition, MEF does not only let you use objects as the resource that can be located by the framework, but also property values, and even methods. This versatility makes MEF a hugely productive piece of infrastructure.
It makes the applying DI easy in situations where you don’t control the lifetime of objects. Think of WCF. WWF. Silverlight controls. With a classical DI framework, applying ServiceLocator is the only way out – MEF does not require any special patterns.
The application can be extended without changing a shared resource (the DI registry). This resolves a major pain point when you develop in an environment that you do not own – either something that explicitly offers a plug-in model, or when you provide customizations to a third-party system.
MEF is peer to peer Dependency Injection
The downsize however is that MEF does not enforce or encourage better testability, class design, or discoverability of functions. And this is where the smell wafts in: where a classical DI framework can act as a catalyst around which Good Code crystallizes. MEF does not require any coding patterns to support it, and hence does not have this potential.
But that’s not a bad thing either. Because what is gained is the ability to decouple the implementation of functionality and where it is located, and that’s the primary and ultimate goal of the inversion of control pattern. With MEF, this goal can be more easily achieved in scenarios that would otherwise be complex, or require support from additional patterns like Service Locator. Now it’s a common observation that people prefer things that are easy over those that are hard, it’s likely that you’re going to see more applications that utilize DI with MEF that otherwise would not.
DI frameworks exert a positive force on the code structure. But that’s not what they’re there for.
While the usage of a DI framework can lead to an overall higher code quality, there are much stronger drivers for these goals. The best way to get to an application that lends itself to unit testing is to write unit tests. For a testable class design, it does not matter that much if the first version of the class has been written in a testable fashion as long as developers are not afraid to refactor it while they are writing the tests.
In the role of a development manager who’s worried about poor separation, I’d recommend looking at the affluent and effluent coupling code metric: classes should have some dependencies and low complexity, otherwise they’re likely to do too much (or have no dependencies and a lot of types that are dependent on it: then it’s a low-level service). NDepend is a great tool for measuring this kind of metrics. NDepend can also find out MEF abuses for you; let’s say you don’t want methods to be used as exports.
And then there is one big advantage of MEF: it’s something like the batteries-included, official DI framework for .NET. This is quite powerful because it’s going to be covered in training materials, code samples, and maybe even developer certification tests. When you hire a developer into your product, you won’t have to train them on your specific flavor of framework, or worry if there has been any exposition to the technique at all.
Also found this: An export provider that allows defining MEF exports with attributes, in an external XML file.
NUnit for Silverlight: Skipping 2.5.7
I just looked at porting NUnit 2.5.7 for Silverlight, but decided on skipping this release. Inside the NUnit framework itself, there are hardly any changes except some minor refactoring. The only significant addition is a TestContext class that provides information about the test that’s currently running. Sweet, but even Charlie Poole describes it as “experimental” at the moment, and it requires support from the actual test runner.
Instead, I’ll finally figure out the licensing, get the code to a robust hosting platform, and clean up some of the code. Alright?
Today in Android
Posted by Christian in Uncategorized on September 6, 2010
Two completely unrelated events happened today to my current favorite toy: The first is the arrival of Angry Birds. Yes, it’s just a game that’s been on the iPhone for a while now, but: it’s a successful iPhone game that gets ported over to Android now. And if there’s one thing that Android is really lacking it’s classy games. Please go ahead, PopCaps of the planet, and bring them over (I’d like to have BookWorm, if you please). And the birds aren’t even alone, quite a lot of high quality apps have landed in the market recently; all major news apps seem to be there now.
The other thing was that I changed my carrier (again) so I can have a flat fee into landlines, and some more data (5GB per month now) – that makes 30€. The SIM card arrived today, but after I installed it, my contacts were all messed up. The My Contact Card did not load at all, and some of the other contact were now sharing one and the same e-mail address: not good. The solution was to delete the contact storage data on the phone and then re-sync from Google and Exchange (between discovering the problem and the solution of course there was some severe amount of time). Deleting the contact storage is done like this (HTC Desire, Android 2.1):
Settings ->Applications -> Manage Applications -> Contacts Storage -> Clear Data.
The procedure seems safe, nothing was lost on the way, and the My Contact Card was working again afterwards.