Wednesday, November 18, 2009

Strange missing feature in CSharpCodeProvider

Currently I'm developing a database application for my university study that uses NHibernate. To enable lazy loading of results it is required for all members of mapped class to be virtual, so NHibernate can create proxy that will be replaced by actual object when required. And if class has events they also must be virtual because actually this is the same fields and methods.

On the other hand I've decided to generate those classes from XML definitions. On of the reasons for this is that this classes seems to be just and a set of properties, but they need to implement INotifiyPropertyChanged. I've decided to write a code generator using System.CodeDom members and to gain some experience at working with members of this namespace. However I've found that CSharpCodeProvider doesn't generate virtual events! Despite the fact that I set the appropriate attribute the event is still normal so NHibernate couldn't generate proxy. I've search through CSharpCodeProvider with Reflector and found that in the method GenerateEvent that generates code of event there is no call of a method OutputVTableModifier that inserts "virtual" keyword. VBCodeGenerator also doesn't generate virtual events.

So I wonder whether this is an intentional decision or the bug in the current .NET framework version (I have 3.5 SP 1). Actually I wasn't determined enough to search for this in the older version of .NET.

Thursday, July 30, 2009

ComboBox In Silverlight 3 And Binding

While recent exploration of Silverlight 3 I've stuck with the problem with following code:

<ComboBox SelectedItem="{Binding Path=Region}" ItemsSource="{Binding Path=Region.ParentCollection}" DisplayMemberPath="Name" />

It's not a good piece of code from design point of view: city references region in which it is located and region has a reference to collection of all available regions. And the problem with that code is that combo box shows list of regions but it doesn't select region in the box. Setting SelectedIndex to constant value also cause ArgumentOutOfRangeException. After several trials and errors I've found that if I firstly assign ItemsSource attribute in XAML and only then SelectedItem ComboBox works correctly! It seems that order of attributes in Silverlight 3 XAML matters. I've never seen that order of attributes may be important. As I know XSD doesn't allow to define strict order of attributes (but this can be done for the elements), so this looks very ridiculous.

Or may be my tricky and ugly code was the cause of such strange behaviour of framework? Hope in future I'll found answer to this question.

Wednesday, June 03, 2009

Force Assembly To Use Another Version Of Library In .NET

It is known that signed assemblies in .NET can reference only other sugned assemblies. And moreover they reference specific version of assembly. This could lead to problems such as I encountered while working with NHibernate: for lazy loading it uses Castle.Core.dll of version 1.0.3, and my application also uses Castle.Windsor, which requires the same dll but of version 1.1.0. As a result application failed because it wans't able to find an assembly of correct version: there could be only one file with name Castle.Core.dll in a directory! I've tried to use private probing paths which reference directories with required versions of library, but this didn't helped me (or I was doing something wrong). All in all I found three solutions for this problem:
  1. Add all required version of referenced assembly to the GAC. So application will load proper versions of assemblies. This solution has two main drawbacks: firstly, adding assemblies to GAC requires administrator privilegies on system, which could be a blocker for some cases, especially for portable apps. Secondly, application will load two assemblies so it will use more resources and moreover similiar static types will exist in two instances and this could be a big problem if they allocate a lot of resources. However this solution iw great if two version of assembly are backword uncompatibile.
  2. Download source code of projects and recompile them with configuration I need. However this includes additional work to compile files which isn't as easy as it wants and moreover this situation can happen with closed source applications, so while sometimes this could be an good solutions in some cases this is just impossible.
  3. You can tell .NET runtime to use specific version of assembly instead of referenced in manifest. This is done by this code in *.config file
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<!-- NHibernate dynamic proxy wants Castle.Core version 1.0.3, but Windsor uses 1.1.0-->
<assemblyIdentity name="Castle.Core" publicKeyToken="407dd0808d44fbdc"/>
<bindingRedirect oldVersion="1.0.3.0" newVersion="1.1.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

Of coures this solution requires that new version is backward compatibile with older version. But this solution is much more simlier in deployment and there is no risk of having duplicates of "heavy" static resources. And what is more this allows to force usage of new version of assembly without any recompiling.

Sunday, May 31, 2009

Merging Two Blogs

I've used to have two blogs: this and about software. However this seems to be rather unmeaningfull work, so I've decided to merge two blogs. I've imported all posts from the old blog and deleted it. So now I will write anything only here.

Tuesday, May 26, 2009

Summer session

I wasn't writing here for much time, but I hope I will fix that soon :) At least today I've written posts to my software blog and here. This summer sessions seems to be the easiest of all that I've already had. I only have to pass two rather simple exams. So almost all summer is free from studying! And now I'm applying to study in Summer Scholl in OpenWay Group in Saint Petersburg. If I will succed then this will be a study course for a 6 weeks but studying seems to be very intersting and usefull there.

Visual C++ Debug Executables On Nondeveloper Machines

Well, If you need only facts then: Debug builds in Visual C++ include dependency on Microsoft.VC90.DebugCRT.dll which is installed with Visual C++ environment. This builds will not run on client machines even with Visual C++ Runtine Redistributable installed. Only Release build will run on such machines.
And here is the history how did I found this.
Recently I found that I couldn't start my application written in C++ on my netbook. At first I thought that this was the problem with hardware: program was compiled on 64-bit OS and machine and I was trying to run it on 32-bit machine. However I've tried to run program on another 32-bit machines and an attempt was succesfull. But on the forth machine it again failed to start. And by the way it provided a rather meaningless message which suggested to reinstall the problem. In .NET applications such an error is displayed if *.config file of executable has en error in XML syntax. but the was no any *.config file in this application.
I've assumed that there are missing dll file. So I've installed the only dependency that could be in this simple application: Microsoft Visual C++ 2008 Runtime Reditributable. That didn't helped. I've installed 2008 SP1 version. The same result. So I've tried to read the manifest file and found the dependency on Microsoft.VC90.DebugCRT.dll. I've compiled my application as a Release build and manifest included dependency on Microsoft.VC90.CRT.dll and application was starting on all required machines! It seems that debug vresion of C Runtime is distributed only with developer environment.
So the summary: Visual Stusio (or at least Visual C++ Express) is required to be installed to run Debug builds of programs.

Sunday, April 19, 2009

My Additional Work Experience As a Receptionist

I'm currently working as a network administrator in a small real estate agency. However during this financial crisis my agency encounters great difficulties and management need to cut salaries. There are also some positional movemevements between workspaces. During these movements I moved to the workplace of a receptionist. We have two workplaces for them but there is only one receptionist at a time. However she couldn't do all her work always alone so now I help out receptionist to do their because. That's not very hard for me, but is a great experience for me and help to the receptionist.

Saturday, March 07, 2009

NonLatin symbols in MySQL

While developing my recent program in databases I found that my program on C# doesn't work correctly with onlatin chracters. It happend with recent version MySql.Data connector assembly from MySql. But there was no such problem when I've used old ByteFX.Data assembly. But old connector has some other error (at least when it works with server 5.2). After digesting a problem I found how to make my program work correctly with nonlatin characters. The solution is obvious: program must use a unicode, so here I'll show that to change in configurations of database and cliet software.
  1. Encoding of connection must be Unicode. For this in connection string add this parameter: CharSet=utf8;
  2. Encoding of database also must be Unicode. In my.cnf file following variables must be set to utf8: default-character-set and chracter-set-server
  3. However this helped me only on Linux machine, on Windows MySQL crashed after string with such prameters. So on Windows I found another possible solution: specify charset only for database: CREATE DATABASE dbName CHARACTER SET = utf8;. This solution is more flexible than previous because it affects only your database and not whole server.