.netCoders Contact Us
Search:

Invoking Services

For the exam, you should be familiar with invoking Web Services, COM components, and native operating system APIs. Here we'll cover the basics of calling each of the services mentioned in the exam requirements. The good news is that the source for invoking these objects is applicable to all three exams: Web Windows, and Service applications.

Invoking Web Services

To instantiate and invoke a web service, follow these steps:
  1. Add a Web Reference - Select Project->Add Reference, and enter in the path to the WSDL for the web service
  2. Instantiate the .NET proxy object
  3. Call methods on the proxy object

When you add a Web Reference, Visual Studio will use the Web Services Description Language Utility (wsdl.exe) utility to create a proxy object for the web service.

You can see this reference in Solution Explorer under the Web References tab. Then, you create an instance of the proxy object like any other .NET object. For example, after adding the Web Reference, the following code calls the Add method on the Calculator service.

com.dotnetcoders.www.Calc1 objCalc = new com.dotnetcoders.www.Calc1();
MessageBox.Show("3 + 5 = " + objCalc.Add(3,5));
You can also call the Web Services Description Language (wsdl.exe) utility from the command line, passing it the location of the WSDL, and the utility will generate a proxy object for you.

When calling Web Services, you also have the option of calling web methods asynchronously. The methods that make this possible are generated automatically by the wsdl.exe utility in the proxy object. The naming convention for asynchronous web methods is Begin[MethodName] and End[MethodName]. For example, the following screenshot shows the methods of the Amazon.com Web Service. Notice that there is an AuthorRequest method for synchronous calls, and a BeginAuthorRequest method for asynchronous calls.

In order to call the web method asynchronously, supply a callback delegate to the Begin[MethodName] method. When the web service returns it's response, a thread will invoke your callback function.

In the event that the author of the web service changes their service after you have already added a reference to your project, you can just right-click the service in the Solution Explorer, and select Update Reference. This will re-generate the proxy object with the updated methods.

Invoking COM Components

From a .NET application, you can make use of unmanaged code such as COM components. Using Visual Studio.NET, you first have to add a reference to the COM Component by selecingProject->Add Reference and selecting the COM tab. Behind the scenes, Visual Studio.NET is using the command line type library import utility (tlbimp.exe) to create a managed wrapper around the component. If you expand the References of the web application in Solution Explorer, you will see the name of the reference for the COM component. Then, to use your component, you just instantiate it like any other .NET component.
object myObj = new DotnetCoders.VBComponent();
You should be familiar with the command-line utilities involved with COM Interoperability.

Assembly Registration Utility (RegAsm.exe)
The Assembly Registration Utility is used to register a .NET assembly as a COM callable component. This involves generating a GUID for the component and writing the appropriate registry entries.

TlbImp.exe
The Type Library to Assembly Converter (tlbimp.exe) utility creates an assembly from a COM component, allowing managed code to call the COM component.

TlbExp.exe
The Assembly to Type Library Converter (tlbexp.exe) utility generates a COM type library from a .NET assembly, allowing .NET components to be called by COM components. Note that this utility only generates a type library; it does not register the component. To both generate a type library, and register the component, use the RegAsm.exe utility.

Invoking Native Functions

To invoke API functions, you need to know the name of the DLL and the signature of the method. Then, you import the System.Runtime.InteropServices namespace, and use the DllImport attribute on the method declaration. The .NET Framework handles the marshaling of data types between managed and unmanaged code. The following example from the .NET documentation shows how to reference the WIN32 API MessageBox method.
//Import Appropriate Namespace
using System.Runtime.InteropServices;

//Setup Class With Method Declaration
public class Win32
{
[DllImport("user32.dll)]
publicstatic extern int MessageBox(int hWnd, String text, String caption, uint type);
}

public class HelloWorld
{
//Call Method
public static void Main()
{
     Win32.MessageBox(0, "Hello World", "Platform Invoke Sample", 0);
}
}