Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
21
21
• Learn how to consume (instantiate and invoke) a web service
• Be able to instantiate and invoke a COM or COM+ component
• Understand how to call native functions using platform invoke
Those of you who have done enterprise programming, whereby you create remote
objects and invoke their methods from another remote object, will find this chapter ex-
citing. Those of you who have never done any remote-method invocations will also find
this chapter exciting. The ability to build objects on a “server” and invoke their methods
from a “client” is the true nature of enterprise programming. Remember that a “server”
can be an application running on any machine that provides services to a “client,”
which is simply an application that requests services of the server.
Whether you call it DCOM (Distributed Component Object Model), RMI (Remote
Method Invocation), EJB (Enterprise JavaBeans), or CORBA (Common Object Request
Broker Architecture), it all has the same objective. You want to instantiate an object that
resides elsewhere on a network. Once that object is created, you want to send it mes-
sages and ask it to invoke its methods and return the results to you. In the world of
Microsoft Visual C# .NET and the .NET Framework, we call this Web Services.
In this chapter, we will explore Web Services from the client side (accessing them
from our Windows application). The subject of creating Web Services is the topic of Part
V of this book and the 70-320 examination, and you will need to understand how to
instantiate and invoke a web service. We will also examine connecting to legacy services
such as COM and COM+ in this chapter.
Web Services
When we discuss Web Services, we have to think in terms of objects outside our local
computer. Think of a program that returns the number of students enrolled in a particu-
lar college program. How many of our Windows programs (that make up our college
applications) could take advantage of a routine like that? Probably most of the adminis-
trative-type programs could use it at one time or another. In that case, it’s foolish to
think that we would install the service into each and every assembly. Code reusability
sometimes implies that the code resides on a server machine somewhere in the world in
P:\010Comp\All-in-1\443-6\ch21.vp
Friday, August 23, 2002 5:04:18 PM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
21
1. Open Visual Studio .NET and start a new project (File | New Project).
2. Select the ASP.NET Web Service template from the Visual C# Projects. Notice in
Figure 21-1 that you don’t need to name the service. You simply create the URL
to the service—http://localhost/HelloWebService. Remember that the service
is hosted by a web server (in our case, Microsoft IIS).
P:\010Comp\All-in-1\443-6\ch21.vp
Friday, August 23, 2002 5:04:18 PM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
21
3. The web service will be added to the web server (see the following illustration).
Any error connecting to the web server will occur at this point.
4. By default, Visual Studio .NET will create a file called Servicex.asmx (where
PART IV
x is the number of services in the application). You can rename the service by
right-clicking on the service and selecting Rename. You may want to rename
P:\010Comp\All-in-1\443-6\ch21.vp
Friday, August 23, 2002 5:04:19 PM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
21
TIP The .vsdisco file is a discovery file. Discovery services are used to
locate a web service, and they will be discussed in Part V of this book. The
.vsdisco file is part of that discovery process.
5. Click on the “click here to switch to code view” link (as seen in Figure 21-2).
This will take you to the code window for the HelloWebService.asmx.cs
class file (see Figure 21-3).
6. As you can see in Figure 21-3, we have made two changes to the default code
module. We have changed the name of the class file and the constructor to
HelloWebService, and we have also created the simplest of Web Services by
removing the comments from the method HelloWorld(). You can make the
same changes and see the results.
P:\010Comp\All-in-1\443-6\ch21.vp
Friday, August 23, 2002 5:04:20 PM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
21
PART IV
Figure 21-3 The code window
7. Build the web service. Select Build from the Build menu.
Several things happen in the background when you build the web service. The first
thing that happens is that the virtual root folder in IIS is created (see the selected folder
in Figure 21-4). The second thing that happens is that the helper files that you see in
Figure 21-4 are created. We will explore the different kinds of files when we reach Part V,
which discusses XML web services. For now, notice that NewHelloService.asmx is
the actual web service file.
You can now test your web service by pressing F5 in Visual Studio .NET. The resulting
display is shown in Figure 21-5. The service is presented to you in Internet Explorer, and
you can click on the exposed method name, HelloWorld.
P:\010Comp\All-in-1\443-6\ch21.vp
Friday, August 23, 2002 5:04:20 PM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
21
When you click on the HelloWorld hyperlink, you will see the screen shown at
the top of Figure 21-6. To test the method, click Invoke. The resulting output is shown
at the bottom of Figure 21-6.
P:\010Comp\All-in-1\443-6\ch21.vp
Friday, August 23, 2002 5:04:21 PM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
21
PART IV
Figure 21-6 The XML output of the web service test
1. Add a new project to the solution. Right-click on the solution name and select
Add | New Project.
2. Select a Windows application.
P:\010Comp\All-in-1\443-6\ch21.vp
Friday, August 23, 2002 5:04:21 PM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
21
5. Add the reference to the web service. To do this, right-click on the Windows
application (in our example, ConsumeHelloWebService) and select Add
Web Reference. The Add Web Reference dialog box will open (see Figure 21-7)
and allow you to select services from different web locations. In Figure 21-7,
you can see that you can request services from a UDDI (Universal Description,
Discovery, and Integration) directory or from a Test Microsoft UDDI directory.
If you are interested in learning more about UDDI, refer to Chapter 25.
EXAM TIP The web service doesn’t have to be created using Visual C#
.NET. That’s the wonderful part of this process. You don’t have to worry
about the language that created the web service—you don’t even have to
worry about the platform of the web server.
6. In this example, you want to select a service from the local web server. In
the Add Web Reference dialog box’s Address box, type http://localhost/
HelloWebService/HelloWebService.asmx?WSDL and press ENTER.
7. You will see the XML shown in Figure 21-8. You will also have the option
of adding the reference to the project. Click on Add Reference.
P:\010Comp\All-in-1\443-6\ch21.vp
Friday, August 23, 2002 5:04:21 PM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
21
You can now run the project by pressing F5. The output is shown in the following il-
lustration. When the Consume button is clicked, an instance of the web proxy is
instantiated and used to run the HelloWorld() method. Except for the fact that we
PART IV
P:\010Comp\All-in-1\443-6\ch21.vp
Friday, August 23, 2002 5:04:22 PM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
21
P:\010Comp\All-in-1\443-6\ch21.vp
Friday, August 23, 2002 5:04:22 PM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
21
PART IV
How Web Services Work
When the web reference is added to the application, a proxy object is created from the
WSDL file. It is the proxy object that runs on the application’s behalf and makes the re-
quest of the remote object. In reality, Visual Studio .NET has run WSDL.exe on the
WSDL file to create this proxy object. You can do the same thing from a command
prompt by using the following line:
EXAM TIP The WSDL.exe program creates a proxy class that exposes the
methods of the web service.
P:\010Comp\All-in-1\443-6\ch21.vp
Friday, August 23, 2002 5:04:22 PM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
21
• wsdl calls wsdl.exe, which is the tool that generates the code for web service
clients from the WSDL file.
• /l:cs specifies the language to be used for the class file. In this case, cs is the
default language, C#. You could specify VB for Visual Basic or JS for JScript.
• /o:HelloWebServiceProxy.cs specifies the output file for the generated
proxy class.
• http://localhost/HelloWebService/HelloWebService.asmx?
WSDL is the path to the WSDL file, which contains the exposed methods of the
web service.
• /n:HelloService specifies the namespace for the generated proxy class.
The beauty of this system is its simplicity and the convenience for the developer. If
you had to code method calls to a remote object, you would have to worry about how
the call is marshaled to the remote object. Marshalling is the process of sending argu-
ments to the remote object and receiving the results from the method call. The proxy ob-
ject does all this for you. You simply add the reference or run the WSDL tool, and all this
functionality is created for you.
Here’s what happens:
1. The proxy object is created from the web service’s proxy class:
localhost.HelloWebService myService =
new localhost.HelloWebService();
EXAM TIP The proxy class must be in the search path for the application.
Place it in the \bin directory and you will have no problems.
2. The client application makes a call to a method that is exposed by the proxy
object. In our example, that call looked like this:
myService.HelloWorld()
3. The proxy object then creates a request packet (using XML) that contains any
parameters for the method call.
4. The request packet is sent to the web service.
5. The web service receives the packet, extracts the parameters, and executes
the method.
6. The web service then creates a packet with the results of the method call and
sends it back to the proxy.
7. The proxy receives the packet and extracts the returned data.
8. The client application then uses the return data.
P:\010Comp\All-in-1\443-6\ch21.vp
Friday, August 23, 2002 5:04:23 PM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
21
namespace ConsumeHelloWebService.localhost
{
using System.Diagnostics;
using System.Xml.Serialization;
using System;
using System.Web.Services.Protocols;
using System.ComponentModel;
using System.Web.Services;
/// <remarks/>
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Web.Services.WebServiceBindingAttribute(Name="HelloWebServiceSoap",
Namespace="http://tempuri.org/")]
public class HelloWebService :
System.Web.Services.Protocols.SoapHttpClientProtocol
{
public HelloWebService() {
this.Url = "http://localhost/HelloWebService/HelloWebService.asmx";
PART IV
}
[System.Web.Services.Protocols.SoapDocumentMethodAttribute
("http://tempuri.org/HelloWorld",
RequestNamespace="http://tempuri.org/",
ResponseNamespace="http://tempuri.org/",
Use=System.Web.Services.Description.SoapBindingUse.Literal,
ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
public string HelloWorld() {
object[] results = this.Invoke("HelloWorld", new object[0]);
return ((string)(results[0]));
}
public System.IAsyncResult BeginHelloWorld
(System.AsyncCallback callback, object asyncState) {
return this.BeginInvoke("HelloWorld", new object[0], callback, asyncState);
}
public string EndHelloWorld(System.IAsyncResult asyncResult) {
object[] results = this.EndInvoke(asyncResult);
return ((string)(results[0]));
}
}
}
EXAM TIP After the proxy class is created by using the wsdl.exe tool,
you can compile the proxy using the C# compiler (csc.exe). When you
compile the proxy class into your assembly, you accomplish everything that
Visual Studio .NET does for you (as we saw in the previous section).
P:\010Comp\All-in-1\443-6\ch21.vp
Friday, August 23, 2002 5:04:23 PM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
21
P:\010Comp\All-in-1\443-6\ch21.vp
Friday, August 23, 2002 5:04:23 PM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
21
PART IV
component from the Microsoft libraries or of clicking the Browse button to search for
a component using the Select Component dialog box (see Figure 21-12).
For our example, we will choose a Microsoft component library, msador15.dll.
Scroll down the list of available COM components until you find Microsoft ActiveX
Data Objects Recordset 2.7 Library, and double-click on it to add it to the selected com-
ponents (see Figure 21-13).
COM .NET
COM is based on a binary standard whereby .NET is based on a type standard—the Common
COM rules dictate the internal layout. Type System (CTS).
COM components are stored in a type .NET components have metadata embedded inside
library. the assembly.
COM uses GUIDs (globally unique .NET uses strong names.
identifiers) for identification.
Object lifetime in COM is managed by A .NET component’s lifetime is managed by the
reference counting (how many times has CLR through garbage collection.
the object been used).
Table 21-1 Some Differences Between COM and .NET
P:\010Comp\All-in-1\443-6\ch21.vp
Friday, August 23, 2002 5:04:23 PM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
21
Figure 21-11
Adding COM
references
P:\010Comp\All-in-1\443-6\ch21.vp
Friday, August 23, 2002 5:04:24 PM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
21
PART IV
Figure 21-12 Browsing for a COM component
Once the component has been added to the project, you will see it in the References list
in the Solution Explorer. Open the Object Browser by selecting View | Other Windows |
Object Browser. Figure 21-14 shows the object listing for the ADOR library—
msador15.dll.
You can now use the COM object by specifying its library and class within:
P:\010Comp\All-in-1\443-6\ch21.vp
Friday, August 23, 2002 5:04:24 PM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
21
Figure 21-13 The selected components are listed at the bottom of the Add Reference dialog box
Although Visual Studio .NET does the preceding things for you, you may be asked on
the Microsoft exam to prove your expertise with the command tool that allows you to
manually generate the .NET proxy. The tool is called TtlbImp.exe, and it is the Type
Library Importer. The importer will convert the definitions in the COM library to equiv-
alent definitions in CLR. The tool will create an assembly that can be looked at by using
Ildasm.exe, which is the MSIL disassembler utility.
The syntax for using the Type Library Importer tool is as follows:
Table 21-2 shows some of the syntax options available for the tlbimp.exe tool.
EXAM TIP Use the following list to memorize important COM details.
P:\010Comp\All-in-1\443-6\ch21.vp
Friday, August 23, 2002 5:04:24 PM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
21
PART IV
Figure 21-14 Object Browser
Option Description
/asmversion:versionnumber Identifies the version number of the assembly.
/help or /? Returns the command syntax and options.
/out:filename Identifies the name of the output assembly.
/namespace:namespace Identifies the namespace in which to produce the
assembly.
/primary Produces a primary interop assembly. There is a
description of interop following this table.
/publickey:filename Identifies the file containing the public key to use in
signing the assembly.
/verbose Displays additional information about the library.
/unsafe Creates interfaces without runtime security checks.
/reference:filename Identifies the name of the assembly to use in resolving
references.
/silent Suppresses output display.
Table 21-2 Options for the TlbImp.exe tool
P:\010Comp\All-in-1\443-6\ch21.vp
Friday, August 23, 2002 5:04:25 PM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
21
• The TlbImp.exe tool is used to generate the proxy for a COM component.
• COM components still have to be registered in the Registry. Use
regsvr32.exe to do this.
• COM components used by .NET applications must be registered in the Registry.
• COM interop is a two-way service that creates a bridge between COM and the
.NET framework. This is the way that COM components can “talk” to .NET
components and vice versa.
Platform Invoke
Platform invoke allows us to get access to functionality that is in libraries or applications
that use unmanaged code.
EXAM TIP Unmanaged code is code that runs outside of the .NET
environment. This means that .NET cannot run garbage collection on it
or check the security of the code—it executes in the CLR with minimal
services (security, garbage collection, and so on).
An example of the type of calls that may invoke unmanaged code is a call to a Win32
API, which contains many functional pieces of code that you may choose to use instead
of creating your own code. Within the Win32 API, you will find user32.dll,
gdi32.dll, and kernel32.dll, which will allow you access to some of the Win-
dows platform functionality.
The CLR provides for platform invoke (pinvoke), which allows managed code (our
.NET code) to call unmanaged native platform code.
P:\010Comp\All-in-1\443-6\ch21.vp
Friday, August 23, 2002 5:04:25 PM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
21
If you place the preceding method call inside the click event of a button, you will get
the output shown in the following illustration when the user clicks the button.
PART IV
Summary
This chapter has demonstrated the techniques required when working with compo-
nents. Components can take the shape of a .NET assembly, a COM or COM+ compo-
nent, or an unmanaged code library, such as the Win32 API. Knowing how to consume
these services is the component focus of the Microsoft Windows exam. You may not
need to know how to create a component, but rather how to use it.
Keep in mind the various types of components:
Review the chapter for the utilities that allow these components to be integrated
seamlessly into a .NET Framework application. In the next chapter, we will look at
deploying the final application for use by the client or end user.
P:\010Comp\All-in-1\443-6\ch21.vp
Friday, August 23, 2002 5:04:26 PM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
21
P:\010Comp\All-in-1\443-6\ch21.vp
Friday, August 23, 2002 5:04:26 PM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
21
PART IV
objectresults=this.Invoke("CallMeService",newobject[0]);
return((string)(results[0]));
}
C. publicintCallMeService()
{
object[]results=this.Invoke("CallMeService",newobject[0]);
return((string)(results[0]));
}
D. publicintCallMeService()
{
object[]results=this.Invoke("CallMe",newobject[0]);
return((string)(results[0]));
}
8. What must be done to be ready to consume a web service?
A. Build a proxy library using wsdl.exe.
B. Build a proxy library using csc.exe.
C. Build a proxy library using TblImp.exe.
D. Build a proxy library using pl.exe.
9. You need to call the function, CallMe(), located in the user32.dll library.
The signature of the function is as follows:
string CallMe (string Name, string Address, string Phone
Which code segment will make the function available to your application?
P:\010Comp\All-in-1\443-6\ch21.vp
Friday, August 23, 2002 5:04:26 PM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
21
P:\010Comp\All-in-1\443-6\ch21.vp
Friday, August 23, 2002 5:04:26 PM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
21
PART IV
D. [System.Web.Services.WebMethod]
Test Answers
1. C.
2. A. The method must be declared as public.
3. D.
4. B.
5. D.
6. B.
7. D. The method name must be CallMe and the result must be an array.
8. A.
9. C.
10. A.
11. B.
12. D.
13. C.
14. C.
15. D.
P:\010Comp\All-in-1\443-6\ch21.vp
Friday, August 23, 2002 5:04:26 PM