Thursday, June 6, 2013
SignalR – Real-time application development
Friday, May 3, 2013
Service Reference Generation using svcutil.exe
Duplicate objects being generated.
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="Person", Namespace="http://tempuri.org/")]
public partial class Person : object, System.Runtime.Serialization.IExtensibleDataObject
{
private System.Runtime.Serialization.ExtensionDataObject extensionDataField;
private string NameField;
public System.Runtime.Serialization.ExtensionDataObject ExtensionData
{
get
{
return this.extensionDataField;
}
set
{
this.extensionDataField = value;
}
}
[System.Runtime.Serialization.DataMemberAttribute(EmitDefaultValue=false)]
public string Name
{
get
{
return this.NameField;
}
set
{
this.NameField = value;
}
}
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://tempuri.org/")]
public partial class Person
{
private string nameField;
///
[System.Xml.Serialization.XmlElementAttribute(Order=0)]
public string Name
{
get
{
return this.nameField;
}
set
{
this.nameField = value;
}
}
}
Overcoming the problem
Monday, August 20, 2012
Entity Framework 5 with enums
Entity Framework 4 and earlier version limitations
Enums or Enumration Types are a very useful feature in .NET framework, that lets you define a collection of logical options as a specific type. Although this feature is a part of the language, it was not supported by the ADO.NET Entity Framework. In previous version of Entity Framework 4 and earlier, there was no possible way that you could define a scalar property as an enum type in any of your entities. It was possible to however explicitly associate integral type scalar properties to an enum via an explicit cast to the desired enum type.
The caveat with this explicit cast is that, a developer could cast any integral entity scalar property type to an enum type provided the enum being cast to has the integer value defined as its underlying value, meaning that you would need to make sure you performing the cast over the correct enum type. This is not a major issue if you have a very few enums defined in your application, but would a be a point of confusion when there are more than a few enums with similar names.
Other approaches would include creating additional properties that encapsulate the entity property within by returning an enum based on the value of the property within the entity class. Which by its very nature can only be accomplished if the data model is instructed to use custom POCO classes.
Entity Framework 5 and Enum Support
Fortunately ADO.NET Entity Framework 5 will officially support the ability to define enum types or use existing enum types as part of your entities scalar properties. Listed below are the very basic steps to add a simple enum property to you entity, I have a very basic sample databasa scheme to illustrate this. You can download the sample solution from here, which also has a local database with this schema.
Step 1: Defining the sample database schema
The above schema represents a relational database that holds order information related to a customer and product. An order in the order table can be one of two states which is either “Delivered” or “Pending Delivery”. Hence this state of the order is represented in the Status column which is of type int within the Order table.
Step 2: Generating an Entity Data Model from the Schema
Note that you could also generate the database script that could be deployed as a database by modeling the entities first. However for this scenario I will be generating the Entity Data Model over the existing database schema. Listed below are the steps for this. In order to generate your Entity Data Model using an existing database,
- Right Click on the project you need to include the Entity Data Model and create an ADO.NET Entity Data Model with a name that best suites your data model and click Next.
- Select the Generate from database option and click Next.
- Define your connection, provided a connection string name and click Next.
- Select the Tables relevant to the model, provide a valid namesapace for the entities and click Finish.
Your Entity Data Model should look like the following,
Step 3: Setting entity property as an enum
With ADO.NET Entity Framework 5 you are able to define new enum types that best suite your usecase, or you any existing enum type definitions that’s already part of the project. In order to link the Order tables Status property as an enum,
- Select the property and right click on it and select Convert to Enum from the context menu, which will bring up the following dialog, where I have filled in the required information.
- If you desire to associate an enum type which is already part of the project, you could do so by checking the Reference external type and giving the fully qualified name for the enum type.
- You could always modify or add options to your enum type by locating the enum created under the Model Browser –> Enum Section as shown below.
Step 4: Using the enum types as part of the entity.
Upon having the enum configured to use a an enum type, its just a matter of writing code against entities the same way you would against a regular old objects that contains enum types within. Listed below is the code sample for doing just that.
// Delevered orders
Console.WriteLine("Delevered Orders");
WriteOrders(orders.Where(o => o.Status == OrderStatus.Delevered));
// Pending orders
Console.WriteLine("Pending Orders");
WriteOrders(orders.Where(o => o.Status == OrderStatus.Pending));
You can download the sample solution from here of that described in the blog post and go though the application.
Sunday, July 15, 2012
WCF Client Request / Response Message Inspection
Very recently I encountered a requirement for inspection of WCF messages passed to and from a service. This feature was required on the client side as the client applications requirement was to store these messages as log entries in the database. Although WCF does not support this out-of-the-box, it was pretty darn easy to implement it just by implementing two (out of many) interfaces in the WFC framework.
Before I go any further I need to mention that this blog post “Capture XML In WCF Service” helped me out a lot, although it talks about the message inspection on the service host itself, where as this post is about message inspection on the client side. I have further made a few enhancements over the code. So lets get started.
Intercepting WCF messages
In order to inspect client messages going in and coming out of the client we need to implement the interface contract IClientMessageInspector, as seen below,
////// Class to perform custome message inspection as behaviour. /// public class MessageInspectorBehavior : IClientMessageInspector { public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState) { // Do nothing. } public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel) { return null; } }
Upon attaching this to the client runtime, WCF will ensure the two methods listed above is called when a request is sent to and a response is received from the service. So here is where we will write our custom code which will see later in this post.
The next important point is that we need to attach this inspector to the client runtime, and this can be done by the creating our own custom service behavior by implementing the IEndpointBehavior interface, as seen below (Note that I have implemented the interface to the same class that implements the IClientMessageInspector interface),
////// Class to perform custome message inspection as behaviour. /// public class MessageInspectorBehavior : IClientMessageInspector, IEndpointBehavior { public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState) { // Do nothing. } public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel) { return null; } public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters) { // Do nothing. } public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime) { // Do nothing. } public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) { // Do nothing. } public void Validate(ServiceEndpoint endpoint) { // Do nothing. } }
Next is that we need to integrate the message inspector to the behavior and this is done in the ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime) method as seen below,
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
// Add the message inspector to the as part of the service behaviour.
clientRuntime.MessageInspectors.Add(this);
}
Now we have a custom inspector attached to a custom behavior, so how do we get the the request and response messages out of the inspector for logging? Well there are few ways to do this, but my preference was to embed an event handler in the inspector so users can subscribe to if required and be notified of when a request or response message is inspected. Here is the code that does just that,
////// Class to perform custome message inspection as behaviour. /// public class MessageInspectorBehavior : IClientMessageInspector, IEndpointBehavior { // Acts as the event to notify subscribers of message inspection. public event EventHandler OnMessageInspected; public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState) { if (OnMessageInspected != null) { // Notify the subscribers of the inpected message. OnMessageInspected(this, new MessageInspectorArgs { Message = reply.ToString(), MessageInspectionType = eMessageInspectionType.Response }); } } public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel) { if (OnMessageInspected != null) { // Notify the subscribers of the inpected message. OnMessageInspected(this, new MessageInspectorArgs { Message = request.ToString(), MessageInspectionType = eMessageInspectionType.Response }); } return null; } // Rest of the class code... }
MessageInspectionArgs class and eMessageInspectionType enum are custom implementations for passing event arguments to the users for identifying the event related information. The code for these definitions are as seen below,
////// Enum representing message inspection types. /// public enum eMessageInspectionType { Request = 0, Response = 1 }; ////// Class to pass inspection event arguments. /// public class MessageInspectorArgs : EventArgs { ////// Type of the message inpected. /// public eMessageInspectionType MessageInspectionType { get; internal set; } ////// Inspected raw message. /// public string Message { get; internal set; } }
Finally its time for integrating it to the client application. Listed below is the code for that. Its pretty concise and easy to implement with very little or no effort.
class Program
{
static void Main(string[] args)
{
string request = string.Empty;
string response = string.Empty;
// Instantiate the service.
ServiceClient sc = new ServiceClient();
// Instanticate the custom inspector behaviour.
MessageInspectorBehavior cb = new MessageInspectorBehavior();
// Add the custom behaviour to the list of service behaviours.
sc.Endpoint.Behaviors.Add(cb);
// Subscribe to message inpection events and provess the event invokation.
cb.OnMessageInspected += (src, e) =>
{
if (e.MessageInspectionType == eMessageInspectionType.Request) request = e.Message;
else response = e.Message;
};
// Call the service.
var x = sc.GetData(1);
// Display or log the results.
Console.WriteLine(string.Format("Request\nMessage: {0}\n\nResponse\nMessage: {1}", request, response));
Console.ReadKey();
}
}
You can download the sample code from here. Let me know your feedback on suggestions or even improvements for that matter.
Tree View Checkbox JQuery Selection
Adding JQuery Hierachical Selection
Introduction to C# and R Integration using R-(D)COM Server
Introduction
Quantitative analysis in the financial industry space plays a major role by providing services with the use of numerical, statistical and quantitative techniques. Such services may include investment management, portfolio optimization, risk management, derivative pricing, etc. In many of the cases mentioned above statistics is a subject area that provides invaluable concepts/theories in order to aid the process of analysis. Similar to using a language when developing a software, modeling statistical concepts/theories can also be done with the help of language.One such language that complements subject of statistics is the R Language. However R in its very own nature is a nothing more than a language to perform statistical analysis. This creates the background for integrating R with C# whereby combining the power of a .NET language and its related frameworks together with a powerful statistical language.
Purpose
In this post I aim to integrate C# with R using the R-(D)COM Server which functions as the bridge between the two languages. I have come across few articles, tutorials and quite a number of forum threads detailing the integration process between C# and R. However it was not very straight forward when I tired it out for my self, hence I thought of sharing a detailed explanation with the intention of helping you save some time if you ever are to try the same.Prerequisites
1. R Language: The latest version of R for windows can be downloaded from The Comprehensive R Archive Network aka CRAN site.2. R-(D)COM Server: This can be downloaded from same CRAN site by clicking on the Other link on the left and selecting the R-(D)COM Server link.
3. Visual Studio: Visual C# Express can be downloaded from here.
Solution
This solution can be performed on both Windows environments Windows 7 and 2008 R2. Ill keep it simple by listing things you need to do step-wise, and detail where ever needed.Step 1: Install R
Start the R setup as administrator, go with the default selections and complete the R installation.Step 2: Install rscproxy.dll
The rscproxy.dll is a required dll in order to communicate with the R-(D)COM Server and by default the native R installer will not include this. To install the .dll, open R as administrator and type in the following command.> install.packages(“rscproxy”)
Select a mirror and click OK. This will install the rscproxy.dll as part of the R library.
Copy the rscproxy.dll from the installed location %PROGRAMFILES%/R/R-2.14.1/library/rscproxy/libs/i386 to the %PROGRAMFILES%/R/R-2.14.1/bin/i386 directory.
Step 3: Configure R_HOME and path Environment Variables
Add a new system environment variable named R_HOME to point to the root directory of the R installation. To do this open the command prompt –> type sysdm.cpl –> go to the Advanced tab –> click Environment Variables… –> click New under the System variables panel as seen below.Edit the path environment variable and add location to the i386 directory in the bin directory of R as seen below.
Step 4: Install R-(D)COM Server
Start the R-(D)COM Interface setup as administrator, go with the default selections and complete the installation.Step 5: Verify R and R-(D)COM Server
By default the R-(D)COM Server setup installs a set of test files to verify and test connections between the (D)COM Server and R. To perform the basic test navigate to Start –> All Programs –> R –> (D)COM Server –> Server 01 – Basic Test. When the test dialog appears click on Start R. You should see a the initialization proceed and a basic test performed as shown below.Step 6: Integrating C# and R using R-(D)COM Server
This tutorial will limit the example to a very basic Console Application which evaluates a very basic R command.2. Add the following code to the R-(D)COM Server references as shown below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using STATCONNECTORSRVLib;
using System.Runtime.InteropServices;
namespace R_ConsoleApp
{
class Program
{
static void Main(string[] args)
{
StatConnector connector = new StatConnector();
try
{
connector.Init("R");
// Create vector x.
connector.EvaluateNoReturn("x <- c(2,3,5,1,4,4)");
// Basic calculations.
Console.WriteLine(String.Format("sum(x): {0:0.00}", (double)connector.Evaluate("sum(x)")));
Console.WriteLine(String.Format("mean(x): {0:0.00}", (double)connector.Evaluate("mean(x)")));
Console.WriteLine(String.Format("sd(x): {0:0.00}", (double)connector.Evaluate("sd(x)")));
Console.WriteLine(String.Format("median(x): {0:0.00}", (double)connector.Evaluate("median(x)")));
}
catch (COMException ex)
{
// Gets the text if any error occured.
Console.WriteLine(string.Format("Unexpected COM Interop Error: {0}", connector.GetErrorText()));
}
finally
{
if (connector != null) connector.Close();
}
Console.ReadKey();
}
}
}
I will include combinations of R integrated with ASP.NET where you could leverage the power of a great web framework in order to do some really neat stuff. Please leave your feedback on any issues you encounter and I will get back to you as soon as I can.Monday, July 2, 2012
Asp.Net Multiple Row Edit GridView Control
About the ASP.NET Gridview
ASP.NET Gridview Limitaions
<ItemTemplate>
<asp:Label ID="lblProductName" Visible='<%# !(bool) IsEditMode %>' runat="server" Text='<%# Eval("ProductName") %>' />
<asp:TextBox ID="txtProductName" Visible='<%# IsEditMode %>' runat="server" Text='<%# Eval("ProductName") %>' />
</ItemTemplate>
The IsEditMode property in the above code segment is a page wide variable which will be set to true upon the user requesting to edit rows. This method has several drawbacks as follows, - Changes all rows to edit mode. Cannot selectively specify which rows to edit.
- All controls are placed within the ItemTemplate tag of the GridView thus making it difficult to clearly differentiate between item and edit controls.
- If many GridViews are available the developer is required to take responsibility of managing global properties that enable the edit mode of each grid.
Enabling ASP.NET Gridview Mulitiple Row Edit
Step 1: Adding the check box column for row selection
///<summary>
/// The selector check box template colum class.
/// </summary>
public class CheckBoxTemplate : ITemplate
{
private ListItemType Type { get; set; }
///<summary>
/// Initializes a new instance of the class.
/// </summary>
///The item type.
public CheckBoxTemplate(ListItemType type)
{
this.Type = type;
}
public void InstantiateIn(Control container)
{
switch (Type)
{
case ListItemType.Header: goto default;
case ListItemType.Item: goto default;
default:
CheckBox chkSelector = new CheckBox();
chkSelector.Checked = false;
// The selector attribute is used by the JS code in MultiEditGridView.js file.
chkSelector.InputAttributes.Add("selector", Type == ListItemType.Header ? "headerCheckBox" : "rowCheckBox");
// Call the appropriate function in the MultiEditGridView.js file based on the checkbox style.
chkSelector.InputAttributes.Add("onClick", Type == ListItemType.Header ? "MultiEditHeaderCheckBoxSelect(this)" : "MultiEditRowCheckBoxSelect(this)");
container.Controls.Add(chkSelector);
break;
}
}
}
Step 2: Switching the selected rows to edit mode
protected override ICollection CreateColumns(PagedDataSource dataSource, bool useDataSource)
{
ArrayList columnCollection = (ArrayList)base.CreateColumns(dataSource, useDataSource);
// Appends an additional column to the beginining of the grid if multi edit is enabled.
if (EnableMultiEdit)
{
SelectorTemplateFiled.HeaderTemplate = new CheckBoxTemplate(ListItemType.Header);
SelectorTemplateFiled.ItemTemplate = new CheckBoxTemplate(ListItemType.Item);
columnCollection.Insert(0, SelectorTemplateFiled);
}
return columnCollection;
}
Step 3: Providing an overloaded DataBind() method
protected override GridViewRow CreateRow(int rowIndex, int dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState)
{
// Enbles the edit template if any of the checkbox indexes are selected.
if (EditFlag && EditIndexes.Contains(rowIndex))
{
return base.CreateRow(rowIndex, dataSourceIndex, rowType, DataControlRowState.Edit);
}
else
return base.CreateRow(rowIndex, dataSourceIndex, rowType, rowState);
}
Step 4: Additional Information
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
// Register the JS on page.
Page.ClientScript.RegisterClientScriptResource(typeof(CustomControls.MultiEditGridView), "CustomControls.MultiEditGridView.js");
}
// Add the MultiEditGridView.js as a web resource.
[assembly: WebResource("CustomControls.MultiEditGridView.js", "text/javascript")]
Step 5: Using the MultiEditGridView control
Summary
<script type="text/javascript" src="Scripts/jquery-1.4.4.min.js"></script></pre>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Products.aspx.cs" Inherits="MultiEditGridViewDemo.Products" %>
<%@ Register Assembly="CustomControls" Namespace="CustomControls" TagPrefix="cc" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Multi Edit Grid View Demo</title>
<script src="Scripts/jquery-1.4.4.min.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<cc:MultiEditGridView ID="MultiEditGridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Product Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%# Eval("Name") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product Category">
<ItemTemplate>
<asp:Label ID="lblCategory" runat="server" Text='<%# Eval("Category") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCategory" runat="server" Text='<%# Eval("Category") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product Description">
<ItemTemplate>
<asp:Label ID="lblDescription" runat="server" Text='<%# Eval("Description") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtDescription" runat="server" Text='<%# Eval("Name") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Is Available">
<ItemTemplate>
<asp:CheckBox ID="chkIsAvailable" runat="server" Checked='<%# Eval("IsAvailable") %>' Enabled="false">
</asp:CheckBox>
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="chkIsAvailable" runat="server" Checked='<%# Eval("IsAvailable") %>'>
</asp:CheckBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</cc:MultiEditGridView>
<br />
<asp:Button ID="btnEdit" runat="server" OnClick="btnEdit_Click" Text="Edit" />
<asp:Button ID="btnUpdate" runat="server" OnClick="btnUpdate_Click" Text="Update" />
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MultiEditGridViewDemo
{
public partial class Products : System.Web.UI.Page
{
public bool IsEdit { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridData(false);
}
}
private void BindGridData(bool isEnableEdit)
{
List productList = new List {
new Product{ ID=1, Name="Product 1", Category="Category 1", Description="Product 1 - Category 1", IsAvailable=false },
new Product{ ID=1, Name="Product 2", Category="Category 4", Description="Product 2 - Category 4", IsAvailable=true },
new Product{ ID=1, Name="Product 3", Category="Category 3", Description="Product 3 - Category 3", IsAvailable=true },
new Product{ ID=1, Name="Product 4", Category="Category 1", Description="Product 4 - Category 1", IsAvailable=false },
new Product{ ID=1, Name="Product 5", Category="Category 5", Description="Product 5 - Category 5", IsAvailable=true },
new Product{ ID=1, Name="Product 6", Category="Category 6", Description="Product 6 - Category 6", IsAvailable=true },
new Product{ ID=1, Name="Product 7", Category="Category 2", Description="Product 7 - Category 2", IsAvailable=false },
new Product{ ID=1, Name="Product 8", Category="Category 5", Description="Product 8 - Category 5", IsAvailable=true },
new Product{ ID=1, Name="Product 9", Category="Category 3", Description="Product 9 - Category 3", IsAvailable=true },
new Product{ ID=1, Name="Product 10", Category="Category 3", Description="Product 10 - Category 3" , IsAvailable=false }
};
MultiEditGridView1.DataSource = productList;
MultiEditGridView1.DataBind(isEnableEdit);
}
protected void btnEdit_Click(object sender, EventArgs e)
{
BindGridData(true);
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
BindGridData(false);
}
}
public class Product
{
public int ID { get; set; }
public string Name { get; set; }
public string Category { get; set; }
public string Description { get; set; }
public bool IsAvailable { get; set; }
}
}
About Me
-
Unknown
I am a software developer with over 7+ years of experience, particularly interested in distributed enterprise application development where my focus is on development with the usage of .Net, Java and any other technology that fascinate me.
