Wednesday, January 19, 2011

WCF vs ASP.NET Web services

Introduction

In this post I will explain the Difference between ASP.NET web service and programming WCF services like ASP.NET web services. It also discusses how we use the both technologies for developing the web services.

The development of web service with ASP.NET relies on defining data and relies on the XmlSerializer to transform data to or from a service.

Key issues with XmlSerializer to serialize .NET types to XML

■Only Public fields or Properties of .NET types can be translated into XML.
■Only the classes which implement IEnumerable interface.
■Classes that implement the IDictionary interface, such as Hash table can not be serialized.
The WCF uses the DataContractAttribute and DataMemeberAttribute to translate .NET FW types in to XML.

[DataContract]
public class Item
{
[DataMember]
public string ItemID;
[DataMember]
public decimal ItemQuantity;
[DataMember]
public decimal ItemPrice;

}

The DataContractAttribute can be applied to the class or a strcture. DataMemberAttribute can be applied to field or a property and theses fields or properties can be either public or private.


Important difference between DataContractSerializer and XMLSerializer.

■A practical benefit of the design of the DataContractSerializer is better performance over XMLserialization.
■XMLSerialization does not indicate the which fields or properties of the type are serialized into XML where as DataCotratSerializer Explicitly shows the which fields or properties are serialized into XML.
■The DataContractSerializer can translate the HashTable into XML.
Developing Service

To develop a service using ASP.NET we must add the WebService attribute to the class and WebMethodAttribute to any of the class methods.

Example

[WebService]
public class Service : System.Web.Services.WebService
{
[WebMethod]
public string Test(string strMsg)
{
return strMsg;
}
}

To develop a service in WCF we will write the following code

[ServiceContract]
public interface ITest
{
[OperationContract]
string ShowMessage(string strMsg);
}
public class Service : ITest
{
public string ShowMessage(string strMsg)
{
return strMsg;
}
}

The ServiceContractAttribute specifies that a interface defines a WCF service contract, OperationContract Attribute indicates which of the methods of the interface defines the operations of the service contract.

A class that implements the service contract is referred to as a service type in WCF.

Hosting the Service

ASP.NET web services are compiled into a class library assembly and a service file with an extension .asmx will have the code for the service. The service file is copied into the root of the ASP.NET application and Assembly will be copied to the bin directory. The application is accessible using url of the service file.

WCF Service can be hosted within IIS or WindowsActivationService.

■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.
Client Development

Clients for the ASP.NET Web services are generated using the command-line tool WSDL.EXE.

WCF uses the ServiceMetadata tool(svcutil.exe) to generate the client for the service.

Message Representation

The Header of the SOAP Message can be customized in ASP.NET Web service.

WCF provides attributes MessageContractAttribute , MessageHeaderAttribute and MessageBodyMemberAttribute to describe the structure of the SOAP Message.

Service Description

Issuing a HTTP GET Request with query WSDL causes ASP.NET to generate WSDL to describe the service. It returns the WSDL as response to the request.

The generated WSDL can be customized by deriving the class of ServiceDescriptionFormatExtension.

Issuing a Request with the query WSDL for the .svc file generates the WSDL. The WSDL that generated by WCF can customized by using ServiceMetadataBehavior class.

Exception Handling

In ASP.NET Web services, Unhandled exceptions are returned to the client as SOAP faults.

In WCF Services, unhandled exceptions are not returned to clients as SOAP faults. A configuration setting is provided to have the unhandled exceptions returned to clients for the purpose of debugging.

Monday, January 17, 2011

c# 3.0 New Features

Introduction:

Microsoft has introduced the C# 3.0 with many key features. It reduces the work of the developers to write many lines of code to achieve the target. It will decrease the code size as well as the additional overhead of the servers.

There are many features like Auto implemented property. Anonymous types, Partial methods, object initializes, implicitly typed local variables.

New Features

1. Auto Implemented Property
The property plays significant role to set and get the values. In the previous versions we were doing like need to set the values to the temporarily local variable. Now that work exactly reduced in the C# 3.0 version. Because it will be implicitly handled by the .Net compiler.

Let's see the new version, Auto implemented property.

[Access-modifier] data type [Name of the property]
{
get;
set;
}

For example we are going to create one property for send email.

public string FromID {get; set ;}
public string ToID {get; set ;}
public string Subject {get; set ;}
public string Message {get; set ;}

In previous version we have done like this.

private string _FirstName;
public string FirstName
{
get
{
return _FirstName;
}
set
{
_FirstName = value;
}
}

Here in C# 3.0, it became auto implemented property, that means in the runtime it will put one temp variable for assign the values by the compiler.

2. Implicit Typed local variable
The new data type introduced in the C# 3.0. Normally when you store the elements in the string with the integer value we need to do the type casting. The variable that will be declared with the var keyword and it will be inferred by the compiler at the time of execution.

var Total = 10 + 10;
var iTotal = 10 + 15.5;
var Name = "Vijay" + "Anand";
var Name = "Anand" + 10;

Here what happens when you declare the var Total = 10 + 10, it will do the addition.

On the other hand, when we declare this variable in the string what happens? Let's see

string name = 10 + 10;

The result will be 1010. Because it will do the concatenation, until unless do the type casting. Here the expression will be inferred by the compiler.

It can be used to create the array in the similar way.

There are some restrictions to use this feature.

We cannot do the increment and decrement operation like i++ or ++i
We cannot declare the NULL value to the var variable.
It use be declare and initialize on the same statement in the local scope.
We cannot initialize the multiple var variables like others. Int I, j, k;


Implicitly Typed Arrays

In the previous section, you have seen how to use the var keyword for implicitly typing variables. In addition to implicitly declaring variables, you can also use the var keyword for declaring arrays as well. For example, consider the following lines of code:

int[] numbers = new int[] { 1, 2, 3, 4, 5};
string[] names = new string[] { "Dave", "Doug", "Jim" };

By using the var keyword, you can rewrite the preceding lines of code as follows:

var numbers = new[] { 1, 2, 3, 4, 5};
var names = new[] { "Dave", Doug, "Jim" };

The compiler infers the type of the array elements at compile-time by examining the values from the initialization expression.


3. Anonymous Types
An anonymous type is one of the new features introduced in the C# 3.0 version. It is a read only property and can be used to assign the set of names constants with the values. These values cannot be changed the outside Anonymous type.

var Names = new
{
FirstName = "Vijay",
LastName = "Anand",
Age = "32"
};

It can be accessed with the following:

string _FirstName = Names.FirstName;

Normally when we define in the enum, there we cannot assign any values.

It will take the order from 0 and will be incremented by one for the every constants in the enum list.

Here the value can be assigned.

Friday, January 7, 2011

CSS Box Model

CSS Box Model


The box model represents how elements are interpreted and displayed in an HTML document. Each element is seen as a box which contains certain properties that describes how it is viewed within that document.

Common properties for elements include margin, border, and padding. The margin of a box represents the outside area of a box or the area between its border and other elements. The border is simply the boxes border. Its optional and can have different styles such as grooved, dotted, or just plain solid. Between the border and the actual content the box contains is the padding. It is like a margin for the content and the border.

When sizing boxes with styles, width and height represent the content's width and height. It does not include the dimensions of the margin, border, or padding. The top, right, bottom, and left properties of a box, however, measure from the extents of a boxes margin, not its contents.

Note: IE5 incorrectly includes margin, border, and padding in the width and height of an element.

There is another aspect of the box model that is not so intuitive, margin collapsing. This is when margins of elements above and below each other collapse into one size.

Friday, October 22, 2010

Using STSADM.EXE to Create Empty Site Collection

There are situations when we want to create empty site collection, like when your content deployment job is failing due to conflicting content. In these cases STSADM.EXE comes handy.

STSADM.EXE is the only means to create a completely empty site collection.


Syntax
STSADM.EXE -o createsite -url -ownerlogin domain\user -owneremail

Example
STSADM.EXE -o createsite -url http://servername:9009/sites/EmptySiteCollectionName -ownerlogin mydomain\praveen -owneremail abc@xyz.com

Remember, using the “Blank Site” template does NOT creates an completely empty site collection. The site collection created by “Blank Site” template create some content for default website.
You can see the difference if you create a site collection using both methods and then inspect the content of the created sites using SharePoint designer.

Wednesday, October 20, 2010

What's New in ASP.NET 4.0 – Better ViewState Control


In ASP.NET 2.0/3.5, you could disable ViewState for individual controls. However what you could not do is disable ViewState at a Page level and then enable it individually for controls on that page that require it. ASP.NET 4.0 changes that and gives more control to developers.

ASP.NET 4.0 introduces the ViewStateMode property that lets you disable ViewState at a Parent level and then enable it for the child controls that require it.
Let’s see this with an example. We will first create a page in ASP.NET 3.5 and disable ViewState on the Parent level and then observe the behavior of ViewState in child controls. We will then use the same example in ASP.NET 4.0 using the ViewStateMode property and observe the changes.
ViewState in ASP.NET 2.0/3.5
Create an ASP.NET application. Place two Label controls and one Button control on the page. Now disable view state on the Page by setting the ‘EnableViewState’ property to false. On the first Label control, set ‘EnableViewState’ property to true as shown below:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
EnableViewState="false" Inherits="_Default" %>
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>ViewState Demo in ASP.NET 3.5title>
head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="One" EnableViewState="true">asp:Label><br />
<asp:Label ID="Label2" runat="server" Text="Two">asp:Label><br /><br />
<asp:Button ID="Button1" runat="server" Text="PostBack" />
div>
form>
body>
html>
In the code behind file, write the following code which set’s the text of the Label controls:
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Label1.Text = "Label1 Changed";
Label2.Text = "Label2 Changed";
}
}
VB.NET
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If (Not IsPostBack) Then
Label1.Text = "Label1 Changed"
Label2.Text = "Label2 Changed"
End If
End Sub
Now what is ‘ideally’ expected out of this setup is that although ViewState is disabled for the entire page, yet Label1 should save ViewState and retain its new value ‘Label1 Changed’ after postback; since ViewState is explicitly enabled on it.
Run the application.
Before PostBack
Postback4-1
After PostBack
Postback1
You will observe that on hitting the button (causing a postback), Label1 does not retain the value (Label1 Changed) explicitly set on it. Not as we expected!
ViewState in ASP.NET 4.0
We will now create a similar web application in ASP.NET 4.0. The only difference is that here we will use the new ‘ViewStateMode’ property.
The ViewStateMode property accepts three values: Enabled, Disabled, andInherit.
Enabled - enables view state for that control and any child controls that are set to ‘Inherit’ or that have nothing set.
Disabled - disables view state
Inherit - specifies that the control uses the ViewStateMode setting from the parent control.
The markup looks similar to the following:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
ViewStateMode="Disabled" Inherits="_Default" %>
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 id="Head1" runat="server">
<title>View State Demo in ASP.NET 4.0title>
head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="One" ViewStateMode="Enabled">asp:Label><br />
<asp:Label ID="Label2" runat="server" Text="Two">asp:Label> <br /><br />
<asp:Button ID="Button1" runat="server" Text="PostBack" />
div>
form>
body>
html>
Observe that ViewStateMode on the Page is set to Disabled. The child Label1 control has ViewStateMode set to Enabled, so Label1 ‘ideally’ should save view state. Since the ViewStateMode property is not set on Label2 , so the Label2 control inherits this property value from its parent (Page) and therefore saves no view state.
Note: The default value of ViewStateMode is Enabled for Pageobject. The default value of ViewStateMode is Inherit for controls.
Run the application and hit the button. What do you expect now?
When the page first loads, both the Label controls display the text as set in the codebehind file.
Before PostBack
Postback2-1
However, after hitting the button and causing a postback, Label1 does retain its value as we had expected!
After PostBack
Postback3-1
This is a welcome change as now we can disable ViewState on a parent level and enable it only for those child controls that require it. This improves performance with lesser efforts and gives us more control. This was however just a demo. You will realize maximum advantage of this new feature in a Master-Content page scenario by setting ViewStateMode to Disabled for the Master page and then enable it individually in Content Pages, or individually on controls that require view state.

Wednesday, October 13, 2010

installing MOSS 2007 SharePoint farm

MOSS 2007 can be installed as a Stand-alone Server application or as a Server farm or as a Web Front End. MOSS 2007 installs WSS v3.0 automatically.

If you have only one server, you have no option but to install Stand-alone Type, this will install desktop database engine (SQL Server 2005 Express Edition) along. It will be an independent instance of SQL Server just for the SharePoint application. But this will result in very poor performance. And can only be used by a very small user group or just for learning purpose.

If you have to use it in a relatively larger setup you must install SharePoint farm (atleast one Complete - Install all components Server Type) with database on a different dedicated database server.

In this post I will show the steps for installing MOSS 2007 SharePoint farm:

Prerequisites: Please read this before installing Beta 2

Step 1: Start the installation by clicking the Setup.exe in the x86/x64(for 64 bit machine), and the installation starts with the following screen. Select the Complete Server Type option and click the Install Now button. This will take few minutes and will install the basic components.

Step 2: After the installation of basic components, it will automatically start the SharePoint Products and Technologies Wizard. This Wizard can even be started from the Start-->All Programs-->Microsoft Office Server-->SharePoint Products and Technologies Wizard link also. This will present a Welcome screen, Click the Next > button

Step 3: Next screen will be of Connect to a server farm, if it is the first server in the farm select the option "No, I want to create a new server farm" and if there are already one or more existing server, you can select either of the option "Yes, I want to connect to an existing server farm" or "No, I want to create a new server farm", Click the Next > button

Step 4: In this step specify the Configuration Database Settings. Do remember if your configuration database is hosted on another server, you must specify a domain account (Global Domain Account), Click the Next > button

Step 5: Next step is to Specify the port number for the SharePoint Central Administration Web Application. details of Web Application and the port number can be read from the following screen shot, Click the Next > button

Step 6: Finally it will show the Configuration Successful screen, Click the Finish button

Step 7: This will open up the Central Administration homepage in the browser.

Step 8: In the Administrative Tasks as shown in the above screen-shot, Click on the "Initial Deployment: Assign Services to Servers" link, this will open-up the following page, Click the Action Link "Initial Deployment: Assign Services to Servers"

Step 9: It will open-up the following screen, just make sure you start all highlighted services in the list(or atleast the one shown as Started here) :

Step 10: Now go back to the Home-page as shown in Step 7 and click the "Application Management" tab, this will open-up the following screen, Now since Services on Server are started, "Create or extend Web application" will be displayed. Click the link.:

It will further show the following page, continue with the "Create a new Web Application" link:

Step 11: Now the Create New Web Application Screen will open. Fill the appropriate values. By default it will show some Port number but since I wanted to host the application on 80 port I changed it to 80. Also there please remember the following

Notes:

  1. In the Load Balanced URL section change the name of the server to the IP of the server in the URL, by doing this you will not require do DNS settings, if otherwise you can continue with the server name also,
  2. In the Application Pool section select a configurable security account. I have given my security account ID. In my case both Predefined options(Network Service/Local Service) didn't worked and gave error.

Step 12: You will get the following screen on completion of Step 11. Now Click the "Create a new Windows SharePoint Services site collection." link

Create the site collection and that's it.

You can create multiple Web-Applications(at different Port) and multiple Site-Collections in every Web-Application.

Popular Posts

Recent Posts

Unordered List

Text Widget