Logo
CAR REVIEW
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
PREGNANCY
 
 
Windows Server

BizTalk 2009 : WCF LOB Adapter SDK (part 3) - Implementing the Connection

6/26/2011 4:35:54 PM

3. Step 3: Implementing the Connection

The runtime connection management is one of the features that makes the WCF LOB Adapters stand apart from their closest competitors: WCF services. Implementing a connection is a mandatory step, even if your LOB system doesn't require a connection, as in the case of our mythical hotel application. Most, if not all, real-life LOB applications do require establishing a connection before granting access to their features.

To enable runtime connection management, you have to implement three classes generated by the WCF LOB Development Wizard, each performing its specific task:

  • HotelAdapterConnectionUri

  • HotelAdapterConnectionFactory

  • HotelAdapterConnection

3.1. Implementing the HotelAdapterConnectionUri Class

The HotelConnectionUri class inherits the abstract ConnectionUri[] class from the Microsoft.ServiceMode.Channels.Common namespace. The ConnectionUri class represents a connection string to a target LOB system. Table 9 shows the methods and properties that you have to implement in the HotelAdapterConnectionUri class.

[] http://msdn.microsoft.com/en-us/library/microsoft.servicemodel.channels.common.connectionuri.aspx

Table 9. The HotelAdapterConnectionUri Cass's Methods and Properties
Method/PropertyDescription
public override Uri UriContains the connection URI.
public HotelAdapterConnectionUriInstantiates the HotelAdapterConnectionUri class.
public override string SampleUriStringGets the sample URI string to present in the Add Adapter Service Reference plug-in and Consume Adapter Service add-in, as shown in Figure 10.

Figure 10. Sample Uri string

Here is the process to follow:

  1. In Visual Studio, open the HotelAdapterConnectionUri.cs file.

  2. Add a new private variable UriBuilder type to the class definition:

    private UriBuilder uriBuilder;

  3. Update the class constructor as follows:

    #region Constructors

    /// <summary>
    /// Initializes a new instance of the ConnectionUri class
    /// </summary>
    public HotelAdapterConnectionUri()
    {
    uriBuilder = new UriBuilder();
    }

    /// <summary>
    /// Initializes a new instance of the ConnectionUri
    /// class with a Uri object
    /// </summary>

    public HotelAdapterConnectionUri(Uri uri)
    : base()
    {
    uriBuilder = new UriBuilder(uri);
    }

    #endregion Constructors

  4. Locate the Uri property, and replace it with the following code:

    public override Uri Uri
    {
    get
    {
    //check if connection string elements are specified
    if (String.IsNullOrEmpty(this.host))
    {
    throw new InvalidUriException(
    "Host name must be specified.");
    }
    if (String.IsNullOrEmpty(this.application))
    {
    throw new InvalidUriException(
    "Application name must be specified.");
    }
    // the connection uri object


    this.uriBuilder.Scheme = HotelAdapter.SCHEME;
    this.uriBuilder.Host = host;
    this.uriBuilder.Path = application;
    this.uriBuilder.Query = "enableAuthentication="
    + enableAuthentication.ToString();

    return uriBuilder.Uri;
    }
    set
    {
    this.host = value.Host;
    //walk through connection string segments and get app name
    //it is in the last segment
    if (value.Segments != null && value.Segments.Length > 1)
    {
    foreach (string segment in value.Segments)
    {
    application = segment;
    }
    }
    this.enableAuthentication = false;
    string[] enableAuthenticationValue =
    GetQueryStringValue(value, "enableAuthentication");
    if (enableAuthenticationValue.Length == 1)
    {
    this.enableAuthentication =
    Boolean.Parse(enableAuthenticationValue[0]);
    }
    }
    }


    Please note the use of the UriBuilder[] class. This class simplifies building and parsing connection strings.

    [] http://msdn.microsoft.com/en-us/library/system.uribuilder_members.aspx

  5. Specify a sample string as shown in the following code snippet. Although this is not a requirement, providing a sample string for adapter consumers is a good practice.

    /// <summary>
    /// Returns the sample connection string
    /// to be presented in UI design-time tools
    /// </summary>
    public override string SampleUriString
    {
    get


    {
    return HotelAdapter.SCHEME +
    "://{host}/{application}?enableAuthentication={true,fals
    e}";
    }
    }


In the next subsection, you will implement the HotelAdapterConnectionFactory class.

3.2. Implementing the HotelAdapterConnectionFactory Class

The HotelAdapterConnectionFactory class implements the IConnectionFactory interface from the Microsoft.ServiceModel.Channels.Common namespace. This interface is located in the Microsoft.ServiceModel.Channels assembly. The WCF LOB Development Wizard provides the implementation for the CreateConnectionIConnectionFactory interface. The purpose of this method is to instantiate the HotelAdapterConnection method, which is the only public method exposed by the class, which represents a single connection to the target LOB system.

Here is the process to follow to implement the HotelAdapterConnectionFactory:

  1. In Visual Studio, open the HotelAdapterConnectionFactory.cs file.

  2. Locate the Private Fields region, and add a new variable:

    private HotelAdapterConnectionUri uri;

  3. Update the HotelAdapterConnectionFactory constructor so that it looks like the following:

    /// <summary>
    /// Initializes a new instance of the
    ///HotelAdapterConnectionFactory class
    /// </summary>
    public HotelAdapterConnectionFactory(ConnectionUri connectionUri
    , ClientCredentials clientCredentials
    , HotelAdapter adapter)
    {
    this.uri = (HotelAdapterConnectionUri)connectionUri;
    this.clientCredentials = clientCredentials;
    this.adapter = adapter;
    }

  4. Locate the Public Properties region, and add the ConnectionUri and ClientCredentials properties, as shown here:

    public ClientCredentials ClientCredentials
    {
    get

    {
    return this.clientCredentials;
    }
    }
    /// <summary>
    /// Returns the connectionuri
    /// </summary>
    public HotelAdapterConnectionUri Uri
    {
    get
    {
    return this.uri;
    }
    }

In the next section, you will implement the HotelAdapterConnection class, the last of the three required to enable the WCF LOB SDK connection management.

3.3. Implementing the HotelAdapterConnection Class

The HotelAdapterConnection class represents a single connection to the target LOB system and implements the IConnection[] interface from the Microsoft.ServiceModel.Channels.Common namespace. This interface is located in the Microsoft.ServiceModel.Channels.dll assembly. Table 10 shows the public methods and properties exposed by the IConnection interface.

[] http://msdn.microsoft.com/en-us/library/microsoft.servicemodel.channels.common.iconnection_members.aspx

Table 10. IConnection Interface Methods and Properties
Method/PropertyDescription
AbortAborts the connection to the external system/application.
BuildHandlerBuilds a new instance of the class that implements the IConnectionHandler interface.
ClearContextClears the context of the connection. This method is called when a connection is returned to the connection pool.
IsValidReturns a value indicating whether the connection is valid.
CloseCloses the connection to the target LOB system.
OpenOpens the connection to the target LOB system.
ConnectionIdProperty. Returns the ID of the connection.

Although HotelAdapter doesn't require a connection to the target system as we mentioned earlier, we nevertheless will show you how to handle user credentials. Most LOB systems require client applications to provide valid credentials before authorizing access to their data and functionality. To set user credentials, you can use the Add Adapter Service Reference plug-in or Consume Adapter Service add-in, as shown in Figure 11.

Figure 11. Security configuration page

When you close the Configure Adapter dialog box and click the Connect button, the WCF LOB SDK runtime component will call the Open method of the HotelAdapterConnection class, which handles the user credentials.

Here is the process you have to follow to implement the IConnect interface and user credentials handling:

  1. In Visual Studio, open the HotelAdapterConnection.cs file.

  2. Comment out all the NotImplemented exceptions. Modify the IsValid method so that it returns true.

  3. In the IConnection Members region, locate the Open method, and replace it with the following code:

    if (this.ConnectionFactory.Uri.EnableAuthentication == true)
    {
    if (this.connectionFactory.ClientCredentials != null &&
    string.IsNullOrEmpty(
    this.connectionFactory.
    ClientCredentials.UserName.UserName))

    {
    throw
    new CredentialsException("Username is expected.");
    }
    }

  4. Build and deploy the project.

Now that you are familiar with the key classes and interfaces that define connection functionality, we will show you how to implement the connection-based metadata handlers.
Other -----------------
- SQL Server 2008 : Configuring the Instance (part 3)
- SQL Server 2008 : Configuring the Instance (part 2) - Specifying the Backup Compression Default & Enabling Login Failure Auditing
- SQL Server 2008 : Configuring the Instance (part 1) - Viewing Configuration Settings & Specifying Maximum and Minimum Server Memory
- Microsoft PowerPoint 2010 : Expanding PowerPoint Functionality - Loading and Unloading Add-ins
- Microsoft PowerPoint 2010 : Expanding PowerPoint Functionality - Viewing and Managing Add-ins
- Microsoft Dynamics CRM 2011 : Sending and Tracking Email Messages in Microsoft Dynamics CRM for Outlook
- Microsoft Dynamics CRM 2011 : Using Microsoft Dynamics CRM for Outlook - Using the Add Contacts Wizard
- Microsoft Dynamics CRM 2011 : Using Microsoft Dynamics CRM for Outlook - Creating and Tracking Contacts
- Windows Server 2008 R2 : Managing Disks and Disk Storage - Understand the Basics (part 2) - Work with Partitions & Use DiskPart
- Windows Server 2008 R2 : Managing Disks and Disk Storage - Understand the Basics (part 1) - Work with Your Storage
- SharePoint 2010 : Securing a SharePoint Farm
- SharePoint 2010 : Introducing SharePoint Security
- SQL Server 2008 : SQL Server Configuration Manager (part 2) - SQL Server Network Configuration
- SQL Server 2008 : SQL Server Configuration Manager (part 2) - SQL Server Network Configuration
- SQL Server 2008 : SQL Server Configuration Manager (part 1) - SQL Server Services
- BizTalk 2009 : WCF LOB Adapter SDK - WCF LOB Adapter Vivisected
- BizTalk 2009 : WCF LOB Adapter SDK - WCF LOB Adapters vs. WCF Services
- BizTalk 2009 : Understanding the WCF LOB Adapter
- SQL Server 2008 High Availability : Database Clustering
- SQL Server 2008 High Availability : Database Mirroring (part 2) - SharePoint and Database Mirroring
 
 
Most view of day
- BizTalk 2006 : Creating More Complex Pipeline Components (part 2) - Schema Selection in VS .NET Designer
- Maintaining Desktop Health : Using Task Scheduler (part 2) - Task Scheduler Security, Task Scheduler User Interface
- Windows Server 2012 Group Policies and Policy Management : GPO Administrative Tasks - Backing Up and Restoring Domain GPOs
- SQL Server 2008 R2 : A Performance Monitoring Approach (part 2) - Monitoring the Processors
- Client Access to Exchange Server 2007 : Using Outlook 2007 Collaboratively (part 2) - Sharing Information with Users Outside the Company
- Microsoft Visio 2010 : Visualizing Your Data - Editing Data Graphics
- SharePoint 2010 : Farm Governance - Administering SharePoint Designer
- Windows Phone 8 : Orientation and the PhoneApplicationPage Class - PhoneApplicationPage Orientation Property
- Windows Server 2008 : Promoting and Demoting a Domain Controller - Demoting a DC with dcpromo, Using dcpromo with an unattend File
- Managing Windows 7 : Managing Windows Arrangements, Changing Search Options
Top 10
- Windows Phone 8 : Scheduled Tasks - Scheduled Task API Limitations
- Windows Phone 8 : Scheduled Tasks - Updating Tiles Using a Scheduled Task Agent
- Windows Phone 8 : Scheduled Tasks - To-Do List Scheduled Task Sample (part 5) - Editing an Existing To-Do Item
- Windows Phone 8 : Scheduled Tasks - To-Do List Scheduled Task Sample (part 4) - Creating the To-Do Item Shell Tile, Saving a To-Do Item
- Windows Phone 8 : Scheduled Tasks - To-Do List Scheduled Task Sample (part 3) - Debugging Scheduled Tasks
- Windows Phone 8 : Scheduled Tasks - To-Do List Scheduled Task Sample (part 2) - TodoService, TodoItemViewModel
- Windows Phone 8 : Scheduled Tasks - To-Do List Scheduled Task Sample (part 1) - TodoItem,TodoDataContext
- Windows Phone 8 : Scheduled Tasks - Using Scheduled Tasks
- Windows Phone 8 : Scheduled Tasks - Background Agent Types
- Windows Phone 8 : Windows Phone Toolkit Animated Page Transitions - Reusing the Transition Attached Properties
 
 
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
2015 Camaro