Saturday, May 28, 2011

How to Add validations controls Programmatically

I was looking for some server side validation message which can done without posting back to server. I tried doing javascript and writing that script to literal control and I have been thinking of doing a better way. But here is the cool way to do this.

// Dynamically adding Validation control
RequiredFieldValidator Validator = new RequiredFieldValidator();
Validator.ErrorMessage = "No data to download for your request.";
// Validation group must be specified as to which group you need to bind
Validator.ValidationGroup = "Group1";
Validator.IsValid = false;
Validator.Visible = false;
Page.Form.Controls.Add(Validator);

All you need to do is Depending on your need you need to change message and validation group. We need to set up validation group properly.


Happy coding Smile

Wednesday, May 18, 2011

How to add a ServiceThrottlingBehavior to a WCF Service?

When working with WCF especially when middle-tier client applications uses Windows Communication Foundation, you should always think about performance and take some major design decisions and tuning parameters.

By adding ServiceThrottlingbehavior in web.config we can achieve high performance using WCF. Below is the sample serivceThrottleconfiguration settings in web.config in .NET 4.0 Framework.

 <behaviors>
      <serviceBehaviors>
        <behavior name="CommonService">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
          <dataContractSerializer maxItemsInObjectGraph="2147483647" />
          <serviceThrottling maxConcurrentCalls="16" 
                             maxConcurrentInstances="116"   
                             maxConcurrentSessions="100"   />
        </behavior>
      </serviceBehaviors>
    </behaviors>

The main purpose for the throttling settings can be classified into the following two aspects:


  1. Controlled resource usage: With the throttling of concurrent execution, the usage of resources such as memory or threads can be limited to a reasonable level so that the system works well without hitting reliability issues.

  2. Balanced performance load: Systems always work in a balanced way when the load is controlled. If there are too much concurrent execution happening, a lot of contention and bookkeeping would happen and thus it would hurt the performance of the system.

In WCF 4, the default values of these settings are revised so that people don’t have to change the defaults in most cases. Here are the main changes:


  • MaxConcurrentSessions: default is 100 * ProcessorCount

  • MaxConcurrentCalls: default is 16 * ProcessorCount

  • MaxConcurrentInstances: default is the total of the above two, which follows the same pattern as before.

“ProcessorCount” is used as multiplier for the settings. So on a 4-proc server, you would get the default of MaxConcurrentCalls as 16 * 4 = 64. Thus the consideration is that, when you write a WCF service and you use the default settings, the service can be deployed to any system from low-end one-proc server to high-end such as 24-way server without having to change the settings. So CPU uses count as the multiplier.

Please note, these changes are for the default settings only. If you explicitly set these settings in either configuration or in code, the system would use the settings that you provided. No “ProcessCount” multiplier would be applied.

Tuesday, May 17, 2011

Web.config transformations in .NET 4.0

The web.config has now been refactored, and with ASP.Net 4 a lot of the settings that were previously found in the web.config file have now been moved to the machine.config file. This significantly reduces the size of the file, which I think is a great bonus.

Web.config transformations cater for moving your application between your relevant environments (e.g. DEV, QA, PROD). The transformations work on the relevant configurations you setup.
To create your own Configuration build with configuration transformations, create a new ASP.NET Web Application in Visual Studio 2010. Next, in the menu select "Build" and then "Configuration Manager". In the "Active Solution Configuration" drop down, select "New". Name the relevant configuration, for example I'm calling mine "DEV" and copying the settings from the "Debug" configuration.

Make sure "Create new project configurations" is selected. Once you click okay, you will see your Web.config file now has a "+" next to it in your solution explorer.

If you don't see the "+", build you solution, right click the web.config file and select "Add Config Transformations".
You will see for each of your build configurations there will be a "Web.[Build Configuration Name].config" file. If you open any of these files, you will see place holders for different sections of your original web.config file.

To change settings per your relevant build configuration, check out the following MSDN Article:Web.config Transformation Syntax for Web Application Project Deployment

Hope this helps.

Sunday, May 15, 2011

When to use .NET Framework Client Profile

The .NET Framework 4 Client Profile is a subset of the .NET Framework 4 that is optimized for client applications. It provides functionality for most client applications, including Windows Presentation Foundation (WPF), Windows Forms, Windows Communication Foundation (WCF), and ClickOnce features. This enables faster deployment and a smaller install package for applications that target the .NET Framework 4 Client Profile.

Check out following MSDN Page for Client Profile:.NET Framework Client Profile

How to Creating an HTML Signature in Outlook 2007

Here is the workaround to create HTML signatures for Outlook 2007. When you create a signature in Outlook 2007 it creates 3 separate files (.htm, .txt and .rtf). To create a more custom signature you can write it in HTML – this is especially useful for when dealing with graphics and advanced formatting.

How to create signature in Outlook 2007:

  1. In Outlook go to Tools > Options and the Mail Format tab.
  2. Click the Signatures button.
  3. Click the New button.
  4. Give your signature a name like ‘MySignature′
  5. Click on OK

Outlook doesn’t require any content to be added and will create the 3 individual files. Click OK and close Outlook

How to Locate your signature folder: Copy one of the following lines depending on what your operating system is.

  • Windows 7: %userprofile%\AppData\Roaming\Microsoft\Signatures
  • Vista: %userprofile%\AppData\Roaming\Microsoft\Signatures
  • XP-2003: %userprofile%\Application Data\Microsoft\Signatures

Click on Start and then Run – paste the line you copied into the Run box and hit Enter

You should see 3 files created for your "MySignature” file (in .htm, .txt and .rtf formats). If you can’t see the file extensions, go to Tools / Folder Options / View and untick ‘Hide extensions for known file types’, or right-click the file and select ‘Properties’ to determine the file type.

Replace the HTML file created by Outlook:
Take your HTML signature file and use it to replace the .htm file in your signature folder (i.e. save it as in this system folder, using the same filename as the .htm signature file created by Outlook ).

To start using your new signature:
Restart Outlook.

It worked for me on Outlook 2007 and Outlook 2010. Hope this is useful for you Smile