Accessing and Invoking Components

You can access any .NET or COM library on your system without much difficulty. These components might represent other development projects you have created, legacy COM components, or business logic that you need to incorporate into your application. The generalized scheme for accessing .NET or COM components is to create a reference to the type library, Import (use) the type library in your application, and then declare and instantiate the class you want to use.

You can obtain a list of available type libraries in the Add Reference dialog box. To display the Add Reference dialog box, right-click References in the Solution Explorer under your project, and choose Add Reference from the shortcut menu.

The Add Reference dialog box has three tabs. Choosing the .NET tab displays available .NET assemblies. Choosing the COM tab displays the available COM type libraries, and choosing the Projects tab displays available projects. If the item you want to add a reference to is not listed, you can browse to the file location by clicking the Browse button. Once you have located the reference you would like to add, you can select it with the Select button. The reference is added to your project in the Solution Explorer.

Once you have added a reference to an assembly or COM type library, you can use the types contained in that reference in your application by using the fully qualified name of the type. If you want to obviate the need to use the fully qualified name, you can use the Imports (Visual Basic .NET) or using (Visual C#) keyword to make members accessible. Chapter 1 describes how to use the Imports and using statements.

Once a reference has been established, you can declare and instantiate the types contained in the type library as you would any other type.

To access a .NET assembly or COM type library

In Solution Explorer, right-click the References node under your project, and choose Add Reference.

The Add Reference dialog box opens.

Select the reference you want to add. COM type libraries are listed under the COM tab, .NET assemblies are listed under the .NET tab, and projects can be found under the Projects tab.

Use the Imports (Visual Basic .NET) or using (Visual C#) keyword to import the reference into your application.

Declare and instantiate the types exposed by your reference as normal.

Instantiating ActiveX Controls

ActiveX is an implementation of COM. As such, you can add a reference to an ActiveX control type library in much the same way as you would add a reference to a COM library. You can then add the ActiveX control to the Toolbox and add it to your applications at design time.

To instantiate an ActiveX control

Add the ActiveX control to the Toolbox by customizing the Toolbox.

Drag an instance of the control from the Toolbox to the designer to add it to your project at design time. Alternatively, you can declare and instantiate the control in code and add it to your application dynamically at run time.

Accessing a Web Service

An integral feature of the .NET Framework is the Web Service. A Web Service is a type of class that is hosted on the Internet. You can declare an instance of the Web Service in your application, and then call its methods either synchronously or asynchronously.

You can create a reference to a Web Service in the Solution Explorer. You can display the Add Web Reference window by right-clicking References under your project and choosing Add Web Reference. This displays a screen that allows you to navigate to the URL of a Web reference or to search online if you do not know the URL.

If you do not know the address of the Web Service you would like to use, the window displays directories in which to search. The Universal Description Discovery Integration (UDDI) directory allows you to search the Web for companies that provide Web Services. When a company is selected, the Web Services it offers will be displayed. Choosing a Web Service will display the XML contract in the left page of the window and the name of the Web Service in the right window.

You can also view a more reader friendly description of the Web Service by removing "?WSDL" from the address displayed in the status bar and reloading the page

Synchronous method calls are made just like regular methods calls. However, because they are accessing resources on the Internet, response time can vary, and application execution will pause until a synchronous call to a Web method is completed.

If you do not want to pause program execution while you wait for a Web Service call to complete, you can make an asynchronous method call. An asynchronous call starts the call to the Web Service on a separate thread, allowing program execution to continue while the Web Service is processing the request. For every method found on a Web Service, there are two additional methods for use asynchronously.

The names of these methods are the name of the Web method prepended with Begin and End. Thus, if a Web Service exposed a method called MyMethod, the asynchronous methods for MyMethod would be BeginMyMethod and EndMyMethod.

You begin an asynchronous method call with the Begin method. In addition to requiring the same parameters as a synchronous method call, a Begin method call requires an AsyncCallback delegate that specifies the method for the Web method to call back on and an Object that allows applications to specify custom state information.

Every Begin method has a return type of IAsyncResult. This interface is used by the corresponding End method to retrieve the data returned by the asynchronous call. The callback method specified by the AsyncCallback delegate must have a signature that takes an IAsyncResult as a parameter.

In the callback method, you can call the End method to retrieve the data. The following example demonstrates how to make an asynchronous call to a method called MyMethod.

To create a reference to a Web Service

In Solution Explorer, right-click References and choose Add Web Reference.

Locate the reference on the Web using either UDDI search or by typing in the address of the Web Service. Click the Add Reference button to add the reference to your project.

After the reference is added, you can instantiate the Web Service just as you would any other class.

To call a method on a Web Service synchronously

Instantiate the Web Service.

Call the method as you would call a method of any other class.

To call a method on a Web Service asynchronously

Create a callback method that takes an IAsyncResult as a parameter.

Call the Begin method of the Web method to invoke the asynchronous call. Supply a delegate to the callback method as a parameter to this method.

Either in the callback method or in another method, call the End method of the Web method to retrieve the data, specifying the IAsyncResult returned by the Begin method as the parameter.

Accessing the Windows API

Accessing native functions of the operating system has become less important with the introduction of the .NET Framework. Most of the important functions exposed by the Windows API are wrapped in .NET classes, which allow you to access their functionality in a type-safe, robust manner.

Nevertheless, you might occasionally want to make a call to an unmanaged function. You can use the Declare keyword in Visual Basic .NET or the DllImportAttribute attribute in Visual C# to import a native function. Once you have imported the function, you can use it like any other function in your application.

To declare an external function in Visual Basic .NET

Use the Declare keyword to specify an external function. The Lib keyword indicates the DLL in which the function is found, and the name and signature must match the name and signature of the external function exactly.

To declare an external function in Visual C#

Use the DllImportAttribute attribute found in the System.Runtime.InteropServices namespace to specify the DLL that contains your function. You must mark your function with both the static and the extern keywords, and the name and signature must match the name and signature of the external function exactly.

related post

DAY 11 OOPS INTRODUCTION

DAY 12 POLYMORPHISM

DAY 13 INHERITANCE AND POLYMORPHISM

DAY 14 EBUGGING TOOLS IN DOT NET

DAY 15 DEBUG AND TRACE IN CLASSES

DAY 16 UNIT TEST PLAN

DAY 17 EXCEPTIONS IN VISUAL STUDIO

DAY 19 ADO.NET INTRODUCTION

DAY 20 DATA ACCESSING IN DOT NET

DAY 21 DATA BASE OBJECTS


No comments:

Post a Comment