.NET Technical bits

Wednesday, May 11, 2011

Difference between WCF and ASP.NET Web Services

Data translation:
ASP.NET web service depends on defining data and relies on the XmlSerializer to transform data to or from a service.

There are few issues with XmlSerializer to XML from serialize .NET types.

• The translated XML will have only the public fields or properties of .NET types

• The implemented class from IDictionary (ex: hash table) cannot be translated

• This will have the classes which implement IEnumerable interface

The WCF uses the attributes DataContract attribute and DataMemeber attribute to translate .NET types in to XML.

[DataContract]
public class Member
{
       [DataMember]
       public int memberID;
       [DataMember]
       public string memberName;
       [DataMember]
       public decimal salary;
}

The DataContractAttribute can be used for class or a struct. DataMemberAttribute can be used for private/public field or property.

DataContractSerializer and XMLSerializer:

• Performance wise DataContractSerializer better than XMLserialization

• DataContratSerializer Explicitly shows the which fields or properties are serialized into XML but XMLSerializer is not

• Hash Table can be translated in DataContractSerializer

Creating WCF and Web Services:

When we create a web service the WebService attribute need to be added to the web service class and WebMethod Attribute need to be added for any web service class methods.
For Example:

[WebService]
public class MyService : System.Web.Services.WebService
{
      [WebMethod]
      public string InvokeMe(string message)
     {
            return message;
     }
}

To create a WCF service we will write this code:
[ServiceContract]
public interface IMyService
{
        [OperationContract]
        string InvokeMe(string message);
}

public class MyService : IMyService
{
        public string InvokeMe (string message)
       {
              return message;
       }
}

The interface defines a WCF service contract using the ServiceContract attribute and the OperationContract tells which of the methods of the interface defines the operations of the service contract. The service type in WCF will be referred to as a class that implements the service.

Hosting the Services:
ASP.NET web services are created and compiled and will be hosted in IIS. This hosted will have the service file with extension .asmx and will have assembly with the compiled class. The hosted .asmx url can be added as a web reference to a .NET project so that the class and methods from service will be accessible in the project.
WCF Service can be hosted within IIS or WindowsActivationService using the steps below.

• Compile the service type into a class library.

• Copy the service file with an extension .SVC into a virtual directory and assembly into bin sub directory of the virtual directory.

• Copy the web.config file into the virtual directory.

Tuesday, April 26, 2011

Asp.NET Chart - How to specify Data

The chart area(s), series, and data points can be specified declaratively (either by entering the declarative mark-up by hand or via the Properties window) or programmatically in the code-behind class. Typically the series and chart area(s) are specified declaratively and the data points are populated programmatically by querying a database or some other dynamic data store. Let's start by discussing how to display a chart whose chart area, series, and data points are all specified declaratively.


<asp:Chart ID="chtNBAChampionships" runat="server">
<Series>

<asp:Series Name="Championships" YValueType="Int32" ChartType="Column" ChartArea="MainChartArea">

<Points>

<asp:DataPoint AxisLabel="Celtics" YValues="17" />

<asp:DataPoint AxisLabel="Lakers" YValues="15" />

<asp:DataPoint AxisLabel="Bulls" YValues="6" />

<asp:DataPoint AxisLabel="Spurs" YValues="4" />

<asp:DataPoint AxisLabel="76ers" YValues="3" />

<asp:DataPoint AxisLabel="Pistons" YValues="3" />

<asp:DataPoint AxisLabel="Warriors" YValues="3" />

</Points>

</asp:Series>

</Series>

<ChartAreas>

<asp:ChartArea Name="MainChartArea">

</asp:ChartArea>

</ChartAreas>

</asp:Chart>

Note that the Chart control has a <Series> section and a <ChartAreas> section, which define the series and the chart areas, respectively. The <ChartAreas> section in the above mark-up defines a single ChartArea named MainChartArea. The <Series> section defines a single Series named Championships. This Championship series is configured to render as a column and is displayed in the MainChartArea. Next, its data points are defined via the <Points> collection. There are seven data points, each data point displaying the NBA team name via the AxisLabel property and the number of championships in the YValues property.

After defining this mark-up, visit this page through a browser. You should see a chart in your browser similar to the screen shot below:

How to add a Microsoft Chart control to an ASP.NET Page

To add charts to a web page simply drag the Chart control from the Toolbox onto the page. Doing so adds the Chart control's declarative mark-up to the page:

<asp:Chart ID="Chart1" runat="server">

<Series>

<asp:Series Name="Series1" IsVisibleInLegend="False">

<EmptyPointStyle IsVisibleInLegend="False" />

</asp:Series>


</Series>


<ChartAreas>


<asp:ChartArea Name="ChartArea1">


</asp:ChartArea>


</ChartAreas>


</asp:Chart>

Note that the Chart control is identified in the declarative mark-up via the tag prefix and name . This prefix (asp) is mapped to the Chart Controls assembly in Web.config:

<controls>


<add tagPrefix="asp" namespace="System.Web.UI.DataVisualization.Charting" assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />


</controls>


Additionally, an HTTP Handler named ChartHttpHandler is mapped to requests to chartImg.axd. As we'll see in a bit, when visited through a browser the Chart control renders an <img> element with a src attribute of ChartImg.axd. As a result, the browser makes a request to this file (ChartImg.axd) to display the chart image. This request is routed to the ChartHttpHandler HTTP Handler which generates the image contents and returns that binary data to the browser for display. This mapping between the ChartImg.axd path and the ChartHttpHandler HTTP Handler is implemented via the following Web.config mark-up:

<handlers>

<remove name="ChartImageHandler" />
<add name="ChartImageHandler" preCondition="integratedMode" verb="GET,HEAD,POST" path="ChartImg.axd" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />


</handlers>

Both of the above configuration additions are automatically added to Web.config when you add a Chart control to a web page for the first time in your project.