Sunday, March 17, 2013

Debugging In VS 2012

Visual Studio have brought some new facilities which makes the developers to debug their code in a more effective manner. Thought of blogging some of some fancy features which seems very tiny things but makes our lives easier.

1. Labeling Break Point

Now you can add labels for your break points which in a way supports to categorize the breakpoints. By using this features we can enable or disable list of break points as we want based on the category.
You can write click on a break point & then click on Edit Label.
image







Then you will get the following dialog where you can add the label. It will list down the existing labels so that you can reuse it.
image
After you label your breakpoints you can get the list of breakpoints in your solution in the breakpoint window. Breakpoint window can be found in
Debug –>Windows->Breakpoints.
Now VS 2012 have this fancy featues where we can search any tool using the quick search. If you all are not sure about where exacly the tool if just type the tool you need in the quick search box VS will find it for you. Quite Cool :).
image
In the breakpoint list you can see the breakpoints with the labels so that the developer can enable or disable them based on the debugging context.
image

2. Conditional Breakpoints

Suppose you have a for loop in your code where you want to debug & see the code of the 17th item in the collection of which your are iterating. In the previous VS versions we have to debug the code iteratively until it reaches the 17th item. No need to go it to that hassle anymore VS 2012 has come up with the new feature which give the developer the facility of adding conditional breakpoints. So that the breakpoint hits when the condition is true only.
Right click on the breakpoint & then select condition.
image
Then you can add the condition you want in to the dialog box.
image
Then the breakpoint will only hits when the i=17. No need to iterate through the whole collection on debug.

Happy Debugging.

Distinct Objects in Linq

When we working with collection of objects & when we need to select the distinct objects out of it, linq Distinct() does not work as we expect.That supports to get the distinct of some know types like,string, Guid, int and etc. What if we need to get the distinct members of a collection where collection is made up of complex object type. You can use the following code snippet.
Suppose the Class is Defined as follows where each member object will have a unique name for it.
public class Member
{
public string UniqueName{get;set;}
public int MemberLevel{get;set;}
public string HeirachyName{get;set;}
}

List<Member> memberCollection=new List<Member>();

if we want to get the distinct members from the above collection.
List<Member> DistinctMember=memberCollection.GroupBy(x=>x.UniqueName).Select(x => x.ElementAt(0)).ToList();
This will select the distict member objects from the collection.

Sunday, March 3, 2013

Contract First Development in WCF 4.5

WCF 4.5 supports contract first development. Means it allows to create all the data contract classes can be auto generated using the WSDL. one of the advantage in using the is if we start developing the service from the data contracts & then generating the WSDL we might end up in creating from data contracts with .net specific data types. But when using contract first development as WSDL is xml based only xml based data types are allowed to create so no .net specific contract will be created. This feature can be very useful when WSDL file is produced through design phase either by a solution architect or through mutual parties that agree on a contract prior to development and build phases.  When this is the case, either the contract can be created for a future service implementation or contracts can be created so that developers can begin coding for the client side of an interface prior to the endpoint being available.

The contract-first tool is integrated into Visual Studio 2012 as a build task. The code files generated by the build task are created every time the project is built, so that the project can easily adopt changes in the underlying service contract.

We’ll see step by step how we can create the data contracts using a WSDL file.

Step 1:

Create a WCF service library project.Add a sample WSDL file in to the project. This is a sample WSDL for a data contract named Product with some properties.

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="ServiceSchema"
targetNamespace="http://tempuri.org/ServiceSchema.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/ServiceSchema.xsd"
xmlns:mstns="http://tempuri.org/ServiceSchema.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
<xs:complexType name="Product">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="1" name="Name" type="xs:string" default="2.2" />
<xs:element minOccurs="0" maxOccurs="1" name="Category" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="1" name="Code" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="1" name="Quantity" type="xs:double" />
<xs:element minOccurs="0" maxOccurs="1" name="Price" type="xs:double" />
<xs:element minOccurs="0" maxOccurs="1" name="Orders" type="xs:double" />
</xs:sequence>
</xs:complexType>
</xs:schema>

Step 2:


Contract-first options can be configured in the Properties menu of a WCF project. To enable contract-first development, select the Enable XSD as Type Definition Language check box in the WCF page of the project properties window.


image


You can set the advanced properties by clicking the advanced button & configure them in the advanced properties window.


 


image


Step 3:


After setting the properties build the project by pressing F6. Then the data contract will be available in the project.


image


 


Auto Generated code will be as follows. The file is created in the <project directory>/obj/<build configuration>/XSDGeneratedCode/ directory by default.


//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.17929
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace ContractFirstDemo.ContractTypes
{
    using System.Xml.Serialization;
    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("MSBuild", "4.0.30319.17929")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://tempuri.org/ServiceSchema.xsd")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace="http://tempuri.org/ServiceSchema.xsd", IsNullable=true)]
    public partial class Product
    {
        private string nameField;
        private string categoryField;
        private string codeField;
        private double quantityField;
        private bool quantityFieldSpecified;
        private double priceField;
        private bool priceFieldSpecified;
        private double ordersField;
        private bool ordersFieldSpecified;
        public Product()
        {
            this.nameField = "2.2";
        }
        /// <remarks/>
        [System.ComponentModel.DefaultValueAttribute("2.2")]
        public string Name
        {
            get
            {
                return this.nameField;
            }
            set
            {
                this.nameField = value;
            }
        }
        /// <remarks/>
        public string Category
        {
            get
            {
                return this.categoryField;
            }
            set
            {
                this.categoryField = value;
            }
        }
        /// <remarks/>
        public string Code
        {
            get
            {
                return this.codeField;
            }
            set
            {
                this.codeField = value;
            }
        }
        /// <remarks/>
        public double Quantity
        {
            get
            {
                return this.quantityField;
            }
            set
            {
                this.quantityField = value;
            }
        }
        /// <remarks/>
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool QuantitySpecified
        {
            get
            {
                return this.quantityFieldSpecified;
            }
            set
            {
                this.quantityFieldSpecified = value;
            }
        }
        /// <remarks/>
        public double Price
        {
            get
            {
                return this.priceField;
            }
            set
            {
                this.priceField = value;
            }
        }
        /// <remarks/>
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool PriceSpecified
        {
            get
            {
                return this.priceFieldSpecified;
            }
            set
            {
                this.priceFieldSpecified = value;
            }
        }
        /// <remarks/>
        public double Orders
        {
            get
            {
                return this.ordersField;
            }
            set
            {
                this.ordersField = value;
            }
        }
        /// <remarks/>
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool OrdersSpecified
        {
            get
            {
                return this.ordersFieldSpecified;
            }
            set
            {
                this.ordersFieldSpecified = value;
            }
        }
    }
}

Hope this gives you a starting point to start developing WCF with Contract first development. You can find some posts in the below URLS.



 


Happy Coding