At long last, a .NET 4.0 version of InformationBox was published on CodePlex!
As you might expect, it now supports the optional and named parameters making it a little easier to read the code.
You can grab the latest source code release from here: http://infobox.codeplex.com/SourceControl/list/changesets
Break it, fix it
A few thoughts about WCF, LINQ and .NET in general
25 April 2012
08 August 2011
DateTimePicker and DateTime.MaxValue
For some unknown reason, the Winforms DateTimePicker control does not allow for dates higher than 9998-12-31. Trying to set the DateTimePicker MaxDate property of DateTime.MaxValue will result in an exception. This is a limitation imposed by the component itself.
Good news is there is a small trick to overcome this limitation. It requires to update the static MaxDateTime property to an arbitrary value (DateTime.MaxValue in our case). Lets put up some reflection code for that purpose:
Just copy and paste this code into the startup method. Basically it needs to run before any DateTimePicker control is created.
Please note that running this code can have side effects because it is not known why this limitation is present in the first place. Use it at your own risk.
Good news is there is a small trick to overcome this limitation. It requires to update the static MaxDateTime property to an arbitrary value (DateTime.MaxValue in our case). Lets put up some reflection code for that purpose:
var dtpType = typeof(DateTimePicker); var field = dtpType.GetField("MaxDateTime", BindingFlags.Public | BindingFlags.Static); if (field != null) { field.SetValue(new DateTimePicker(), DateTime.MaxValue); }
Just copy and paste this code into the startup method. Basically it needs to run before any DateTimePicker control is created.
Please note that running this code can have side effects because it is not known why this limitation is present in the first place. Use it at your own risk.
13 September 2010
Create UserControl with inline content
Let's say you want to create a user control that contains some literal text:
<uc1:InlineContent> This is some standard text message </uc1:InlineContent>Here is the simplest UserControl that matches this description:
using System.Web.UI; [ParseChildren(ChildrenAsProperties = true, DefaultProperty = "Content")] public partial class InlineContent : System.Web.UI.UserControl { [PersistenceMode( PersistenceMode.EncodedInnerDefaultProperty)] public string Content { get; set; } }
06 September 2010
Adding a delay before processing Textbox events
Typical scenario: You provide the user with a TextBox he can use to type a filter over a big chunk of data that takes a lot of time to process. Most of the time, you would handle the TextChanged event of the TextBox and issue a filter operation over the data source using the character(s) available in the TextBox. Typical problem: The user complains because he has to wait between the key strokes even if he already knows a filter that would allow the search to take place in a matter of a few seconds. Solution: Introduce a delay between the last key stroke and the processing using a timer. Walkthough: Create a new Winforms project with a Form. Add a TextBox (named textBox), a ListBox (named listBox). To mimic a very slow data source, we will create a dummy class:
public class VerySlowDataAccess { private static string[] data = null; static VerySlowDataAccess() { data = new[] { "AAAAAA1", "AAAAAA2", "AAAAAA3", "AAAAAA4", "AAAAAA5", "AAAAAA6", "AAAAAA7", "BBBBBB1", "BBBBBB2", "BBBBBB3", "BBBBBB4", "BBBBBB5", "BBBBBB6", "BBBBBB7", "BBBBBB8" }; } public static string[] GetItems(string filter) { Thread.Sleep(3000); return data.Where(d => d.StartsWith(filter)).ToArray(); } }
Now we can build our very simple form to highlight the concept. Go back the form, add a timer attribute using the System.Threading.Timer and not the System.Windows.Forms.Timer (very important).
private System.Threading.Timer timer;
Then create a thread safe method to populate the ListBox based on the textbox value. By thread-safe, I actually UI-thread safe. public void LoadData() { if (this.InvokeRequired) { this.Invoke(new Action(LoadData)); } else { this.listBox.Items.Clear(); this.listBox.Items.AddRange(VerySlowDataAccess.GetItems(textBox.Text)); } }
Next step is to initialize the timer in the Form constructor. We will hook it up with our LoadData method and set the timeout to Infinite to deactivate it (for now):
public MainForm() { InitializeComponent(); timer = new System.Threading.Timer((c) => LoadData(), null, Timeout.Infinite, Timeout.Infinite); }
Last but not least, as you may expect, we will add the handler for the TextChanged event of the TextBox.
private void textBox_TextChanged(object sender, EventArgs e)
{
// 250ms is the maximum time between key strokes before the delegate is called (LoadData in that case)
timer.Change(250, Timeout.Infinite);
}
So, what is happening here? Basically what we do is simply reset the timer every time the text is changed. Then after the 250 ms delay and if no key was pressed, the delegate will be called, thus refreshing the ListBox. This works because we always set the timer's repeat time to Infinite, it will fire only once after a key stroke sequence. The delay is arbitrarily set to 250 ms. Based on my own experience, it is sufficient for a regular user (with Internet-grade typing skills) and it is not so long as to be noticeable. Possible improvements:
- Prevent the load to be triggered again until it is finished. A thread-safe boolean in the LoadData method can do the job here.
- Encapsulate all this behavior into a black-box control.
02 February 2010
Visual Studio 2010 Training Kit
For those who are looking for a good place to start with .NET 4.0 and Visual Studio 2010, you can download from Microsoft a complete training kit containing a whole lot of first class information about the next major .NET release and its sidekick. :)
Here is the link to the download page : http://www.microsoft.com/downloads/details.aspx?FamilyID=752CB725-969B-4732-A383-ED5740F02E93&displaylang=en
Here is the link to the download page : http://www.microsoft.com/downloads/details.aspx?FamilyID=752CB725-969B-4732-A383-ED5740F02E93&displaylang=en
15 December 2009
Things I love about Visual Studio 2010: IntelliTrace
IntelliTrace is the next step in evolution for debuggers. It allows you to track everything that is happening during a debug session. It can track actions like clicking on a button, reading for a file, executing an ADO.NET call (including the actual call !) and a lot of other things.
You can think of IntelliTrace as some sort of log for all actions of the program.

You can also record the call stack for each event but it will slow down the debugging experience and you won't be able to use the "Edit and Continue" feature anymore, but it will allow to have much more details about the execution context of each event.
You can think of IntelliTrace as some sort of log for all actions of the program.

You can also record the call stack for each event but it will slow down the debugging experience and you won't be able to use the "Edit and Continue" feature anymore, but it will allow to have much more details about the execution context of each event.
27 October 2009
SSL problem when installing TFS 2010
I had this error when trying to install TFS 2010 Beta 2 on a shiny new Windows Server 2008 R2 :
So first thing was to remove the SSL binding from the report server (using the Reporting Services Configuration Manager), but it did not change anything from the TFS configuration checker point of view.
The fact is this checker does not care about whether SSL is actually in use for the report server, it simply checks IF SSL can be enabled in the report server. That means that you must completely disable SSL and this can only be done by modifying the .config file of the report server.
Just navigate to your report server folder (usually \MSRS10.MSSQLSERVER\Reporting Services\ReportServer), open the rsreportserver.config file for editing and locate the parameter named SecureConnectionLevel and set its value to 0 to deactivate security).
Re-run the configuration checker and it should not complain anymore.
TFxxxxxx: SQL Server Reporting Services is configured to require a secure connection, however no HTTPS URL is configured with a valid certificate
So first thing was to remove the SSL binding from the report server (using the Reporting Services Configuration Manager), but it did not change anything from the TFS configuration checker point of view.
The fact is this checker does not care about whether SSL is actually in use for the report server, it simply checks IF SSL can be enabled in the report server. That means that you must completely disable SSL and this can only be done by modifying the .config file of the report server.
Just navigate to your report server folder (usually \MSRS10.MSSQLSERVER\Reporting Services\ReportServer), open the rsreportserver.config file for editing and locate the parameter named SecureConnectionLevel and set its value to 0 to deactivate security).
Re-run the configuration checker and it should not complain anymore.
02 June 2009
Entity Framework 4.0 and POCO
One of the thing that annoyed me most in Entity Framework 1.0 was the lack of support for POCO. I like to use a specific vertical layer for entities, and I like those entities to have only properties and to be POCO to say it simply. The problem in EF 1.0 is that entities are forced to inherit from a class. I could also use the IPOCO interfaces but it was a lot of code for little benefit.
The great thing I love in EF 4.0 (even if it is a beta) is that I can generate the edmx file without generating the classes. So basically, I have a EDMX file containing all the XML stuff (conceptual model, physical model and mappings) and I create my classes that Object Services will use. That way, I can put real POCOs in my entity layer.
This is an entity that I can create and use with EF (and the corresponding EDMX) :
EF will manage the related entities the same way it does with generated entities. That means, you can also benefit from the lazy-loading or deferred-loading mecanism, and change tracking.
As a side note, EF 4.0 nows supports complex types (even in the designer).
The great thing I love in EF 4.0 (even if it is a beta) is that I can generate the edmx file without generating the classes. So basically, I have a EDMX file containing all the XML stuff (conceptual model, physical model and mappings) and I create my classes that Object Services will use. That way, I can put real POCOs in my entity layer.
This is an entity that I can create and use with EF (and the corresponding EDMX) :
public class Client
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Address Address { get; set; }
}
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Address Address { get; set; }
}
EF will manage the related entities the same way it does with generated entities. That means, you can also benefit from the lazy-loading or deferred-loading mecanism, and change tracking.
As a side note, EF 4.0 nows supports complex types (even in the designer).
29 May 2009
WCF tuning tips
To obtain significantly better response time and decreased the load on the service, you apply a set of best practices depending on your needs.
1. If you do not need sessions in your services, you may want to disable them to reduce the memory consumption of the service.
2. If you are transfering large amounts of data between client and server, the minimum is to enable MTOM enconding. MTOM is an optimized encoding for transmitting binary data. And even better than MTOM, you can also use streaming for the data transfers. Streaming avoids loading the complete message in memory which is what happens when you do not use streaming. Performance will be vastly increased when dealing with large amounts of binary data.
3. If you have control over client and server, you may also want to choose a more efficient but less interoperable binding (like NetTcpBinding for example). This will avoid the overhead that comes with all the WS-* protocols.
4. Be carefull when modifying the instance and concurrency mode. A single instance and a single thread will avoid concurrency issues but will only serve one request at a time. Multiple instances and multiple threads per instances will enable much more requests to be processed simultaneously but it will need a really good concurrency management system. Depending on the constraints, the "good" solution lies between those two models.
These are the basic checks you can apply on your services.
Happy coding :)
1. If you do not need sessions in your services, you may want to disable them to reduce the memory consumption of the service.
2. If you are transfering large amounts of data between client and server, the minimum is to enable MTOM enconding. MTOM is an optimized encoding for transmitting binary data. And even better than MTOM, you can also use streaming for the data transfers. Streaming avoids loading the complete message in memory which is what happens when you do not use streaming. Performance will be vastly increased when dealing with large amounts of binary data.
3. If you have control over client and server, you may also want to choose a more efficient but less interoperable binding (like NetTcpBinding for example). This will avoid the overhead that comes with all the WS-* protocols.
4. Be carefull when modifying the instance and concurrency mode. A single instance and a single thread will avoid concurrency issues but will only serve one request at a time. Multiple instances and multiple threads per instances will enable much more requests to be processed simultaneously but it will need a really good concurrency management system. Depending on the constraints, the "good" solution lies between those two models.
These are the basic checks you can apply on your services.
Happy coding :)
14 May 2009
Databinding and XtraReport
I had a problem recently using object data sources in my XtraReports. I was assigning an object hierarchy to the DataSource property and was getting weird exceptions.
The problem is XtraReport does not handle well one object as a data source, it prefers to have a collection of objects.
The simple fix was to use a BindingSource as the data source and assign my object to the BindingSource's DataSource property. And yes, I should have been using a BindingSource from the start, I know :)
The problem is XtraReport does not handle well one object as a data source, it prefers to have a collection of objects.
The simple fix was to use a BindingSource as the data source and assign my object to the BindingSource's DataSource property. And yes, I should have been using a BindingSource from the start, I know :)
Subscribe to:
Posts (Atom)