This user hasn't shared any biographical information
Link whole directories into Visual Studio Projects
Posted in Coding on December 1, 2010
You can include a file into a Visual Studio project in two ways: copy it into the project directory, or link it. Copying the file into the project directory is Visual Studio’s default option: it’s what happens when you drag a file from Explorer into VS’s project explorer, or when you use the dialog Project -> Add existing item. This is fine unless you wish to share files between multiple projects. An example where I needed to share a whole lot of files between different projects is NUnit for Silverlight: the same code is used by three projects for different versions of Silverlight and the Silverlight unit testing framework.
To link a file into Visual Studio, you can use the Project -> Add existing item dialog: the “Add” button has a dropdown menu, and the second option is “Add as link”. This will leave the file where it is and add a relative link to the Visual Studio project file. In the project file (which is BTW a MSBuild file), this looks like that:
<Compile Include="..\..\..\src\VersionInfo.cs">
<Link>VersionInfo.cs</Link>
</Compile>
If you add multiple files, Visual Studio will create one item like this for each file. To add all files in a directory, mark all files in the the Project -> Add existing item dialog, and link them. This is fine as long as you don’t delete any files or add new ones – Visual Studio will not pick up these changes. New files in the directory won’t be in the project, and deleting/renaming files will cause the build to fail.
A solution is to modify the project file manually. The Include directive supports wildcards. So to link all C# code files in a directory, you can do this:
<Compile Include="..\..\..\src\NUnit.Silverlight.Framework\*.cs" />
Nice, but in the case of NUnit for Silverlight, there were C# code files in subdirectories. Using the Compile directive as above, the files are included, but all appear in the root folder of the project – and if a file with the same name exists in the parent directory and the subdirectory, it won’t work. However, there is support for metadata keywords in MSBuild. For example, the %(Filename) keyword is a placeholder for each match in a wildcard. See this article for reference: MSBuild: By Example—Introducing Well-Known Metadata. Now we can include subdirectories:
<Compile Include="..\..\..\src\NUnit.Silverlight.Framework\*.cs" />
<Compile Include="..\..\..\src\NUnit.Silverlight.Framework\Attributes\*.cs">
<Link>Attributes\%(FileName)</Link>
</Compile>
<Compile Include="..\..\..\src\NUnit.Silverlight.Framework\Constraints\*.cs">
<Link>Constraints\%(FileName)</Link>
</Compile>
<Compile Include="..\..\..\src\NUnit.Silverlight.Framework\Exceptions\*.cs">
<Link>Exceptions\%(FileName)</Link>
</Compile>
As you see, I need to link three subdirectories. Using a * placeholder in the directory name and the %(RecursiveDir) keyword should enable adding these with a single statement like this:
<Compile Include="..\..\..\src\NUnit.Silverlight.Framework\*\*.cs">
<Link>%(RecursiveDir)%(FileName)</Link>
</Compile>
… but that does not work. Never mind, including subdirectories one by one is enough for me right now.
NUnit for Silverlight 2.5.8
Finally, another drop of NUnit test framework for Silvelight has been finished:
- It is based on NUnit version 2.5.8, a step up from 2.5.5.
- The complied assemblies are strong-named. I’ve included the key in the source code so that if you want to compile yourself, you’ve got the exact same version.
- The project is now hosted on Google Code. I’ve removed the links to download the sources and binaries from Dropbox.
- I reviewed the licenses to see what the license for the complete oevre could be. The answer is that there are in fact two licenses: NUnit has its own license; the source code contains the terms. This license applies to the
Nunit.Silverlight.FrameworkandNunit.Silverlight.Framework.Testsassemblies. The metadata provider for the Silverlight unit testing framework goes back to Jeff Wilcox, who has put this piece under MS-PL. So the MS-PL applies to theNUnit.Silverlight.Metadataassembly. - In the test runner, data tests now have names that reflect the parameter values, and are available for the original version of the unit testing framework for Silverlight 3.
The next thing to come will be support for Windows Phone 7.
Download
Please download from nunit-silverlight on Google Code.
Silverlight is a failed attempt (of sorts)
Posted in Coding on November 4, 2010
As someone living in the Microsoft client programming world, it’s very hard not to get emotionally drawn into the Silverlight vs. HTML5 discussion in the aftermath of the PDC 2010. And so do I.
Silverlight is a failed attempt, and HTML5 is completely taking over its role.
I think the above statement is true for at least one of the original drivers behind the product. Silverlight was announced and marketed as an alternative to Flash for video playback and streaming. Silverlight version 1 could hardly do anything other – that probably means that powerful stakeholders had pushed the video capabilities of the platform to a higher priority than most anything else. If I had to guess the business driver I’d say I’d say they wanted their piece of the Youtube business model – ad-supported video delivery. Flash was at the time the only medium capable of delivering against that goal, so Microsoft put a lot of effort into making Silverlight the better video player. Just look at all the work they’ve put into codecs, DRM, and streaming. They went public with streaming the 2008 Olympics – you can’t really launch any bigger.
Well, that plan did not really fly so well. Silverlight could not become dominant over Flash, and now Flash is being driven out of the market – by the means of adding video playback capabilities directly into the browser. So in conclusion, Silverlight is a complete fail in what the people who pay for it regarded its primary use case.
Even with version 2, Microsoft’s vision for Silverlight was much more about media and gaming than about larger-scale applications. Again, this story is hardly a tale of triumph. Everyone was extremely excited when Linerider was ported to Silverlight, but that was kind of a solitary success (and the productive version is still in Flash).
For line of business application with a rich UI, Microsoft’s proposition was a WPF application with Click-Once-Deployment. The success of Silverlight as a platform for internal business applications seemed to come as a surprise, and some effort has been put into making the platform better suited for that purpose since.
So when Microsoft now announces they’re adjusting the vision and strategy for Silverlight, you could also read that of a confession of previous failure.
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
Posted in Coding on September 30, 2010
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?
Posted in Coding on September 8, 2010
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 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.
Like the Yellow Pages for NHibernate
Posted in Coding on August 11, 2010
I’ve just listened to Scott Hanselman‘s podcast Hanselminutes Episode 225: Learning about NHibernate 3. with Jason Dentler.If you’re already into NHibernate, you won’t really learn so much new. But if you think about familiarizing yourself with NHibernate, it’s a must-listen.
Scott points out correctly that one thing where NHibernate does not shine is that it’s kind of hard to get a complete view of all the different projects and documentation spread out across the Internet. In reaction, Jason produces the most complete list of resources that I’ve seen in one place. In 35 minutes you’ll know most about where to look, and what to expect from NHibernate. The show notes contain links to most of the mentioned web sites.