.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 not only Web Applications, but Services and Windows applications as well.

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();
Response.Write("3 + 5 = " + objCalc.Add(3,5));

Invoking COM Components

There are two ways to invoke COM components in an ASP.NET application. One method is the classic ASP method of calling Server.CreateObject. As mentioned in the section on Intrinsic Objects, Server is an intrinsic object in ASP.NET. Instantiating COM components in this fashion is late-bound.

object myObj = Server.CreateObject("DotnetCoders.VBComponent");

Early binding can also be accomplished, and is recommended for performance reasons. Using Visual Studio.NET, you first have to add a reference to the COM Component by selecing Project->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();

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)]
public static 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);
}
}