Problem Statement
Assume that ABC Inc. is one of the leading mobile manufacturing companies in India. Currently, ABC Inc uses an XML-based web service that provides the details of the various mobile models available for sale. The management of ABC Inc wants their Web service to accommodate features such as reliable messaging, transactions, and security, and this web service should be accessible from any application of all platforms.
The developers of ABC Inc. know that they can fulfill all the above requirements by developing the service in WCF.
Prerequisite
Create tblProduct in SqlServer for this Demo.
Solution
Task 1. Creating a WCF Service
To create a WCF service you need to perform the following tasks.
- Open Microsoft Visual Studio.
- Select "File" - "New" - "Website". A dialog box appears, as shown in the following figure.
- Select the WCF Service template under the Visual Studio installed templates section.
- Click on the "OK" Button. The "App_Code/Service.cs" file appears, as shown in the following figure.
- Add the following namespaces to the "App_Code/Service.cs" file.
using System.Data;
using System.Data.SqlClient;
- Remove the following code snippet from the Service class.
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
- Open "App_Code/Iservice.cs" and remove the following highlighted code from the interface.
[ServiceContract]
public interface IService
{
[OperationContract]
string GetData(int value);
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
// TODO: Add your service operations here
}
// Use a data contract as illustrated in the sample below to add composite types to service operations.
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";
[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}
[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}
- Add the following namespace in the "App_Code/Iservice.cs" interface.
using System.Data;
- Type the following code snippet for the "Iservice" interface as shown in the following figure.
- Type the following code for the "Service.cs" file as shown in the following figure.
- Verifying WCF Service: Press F5 to run your service; you will get a browser window as shown in the following figure.
Task 2. Creating ASP.Net client application
- Open Visual Studio then click on "File" - "New" - "Web Site" - "ASP.Net website".
- Drag and drop a TextBox, Button, and GrideView as shown in the following figure.
- Switch to the Solution Explorer. Right-click on the root directory (the solution) and click on "Add Service Reference". Provide the WSDL file link in the Address TextBox, as shown in the following figure.
- Click the "OK" Button.
- For the Click event of the Show Button write the following code.
int Id = Convert.ToInt32(TextBox1.Text);
ServiceReference1.ServiceClient proxy = new ServiceReference1.ServiceClient();
GridView1.DataSource = proxy.QueryProduct(Id).Tables[0];
GridView1.DataBind();
- Press F5 to run your ASP.Net application. Enter a product ID in the TextBox and click on the Show Button. If everything goes well you will get the following output as shown in the following figure.