Consuming a WCF Service in an ASP.NET 3.5 Client Application
1. Create the WCF service using projects to represent the layering of the service:
Figure 1: The WCF Service Project and the IIS Host Project is the Published Service to a Windows IIS Server
Notice that the test application is WebApplication1 with the startup page AJAX.aspx. The actual service is in the WCF Library project called Service with the IAppDataServiceContract interface. The service business logic is in the BLL project, and the DAL is the data access layer that calls the Connection object (in the Connection project) to get the encrypted SQL Server connection string to access the database and pass parameters to the stored procedure. The DAL then populates the AppDataContract object from the AppDataServiceContracts project and passess the contract object back to the BLL. The BLL passes the object back to the calling application page code-behind page to populate a form.
2. Before all of the above can happen, however, the WebApplication1 must have a service reference to the IISHost service. In order to reference the IISHost Service, you must right click on the IISHost project and publish the WCF service:
3. Next, create the consuming client application:
4. Add a service reference to the client application project:
The added service reference is shown below:
5. Instantiate the service and contract references in the ASP.NET form code-behind file:
private CAMAppDataService.AppDataServiceContractClient svc = new CAMWebAppSample.CAMAppDataService.AppDataServiceContractClient();
private CAMAppDataService.AppDataContract appData = new CAMWebAppSample.CAMAppDataService.AppDataContract();
6. Create the button click event, call the service method, and populate the form:
protected void btnGetAppData_Click(object sender, EventArgs e)
{
appData = svc.GetAppDataByApplicationID(Convert.ToInt32(this.txtAppID.Text.Trim()));
this.txtApplicationID.Text = appData.ApplicationID.ToString();
this.txtAppName.Text = appData.ApplicationName.Trim();
this.txtAppSyncConnString.Text = appData.SyncConnectionString.Trim();
this.txtAppAsyncConnString.Text = appData.SyncConnectionString.Trim();
}