September 30, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Building Distributed Apps? Use XML Web Services, Not Remoting (Mostly)

  • December 15, 2004
  • By Paul Kimmel
  • Send Email »
  • More Articles »

Listing 2: All of this complicated-looking code is generated by SPROXY, which is invoked by the WSDL utility.

'------------------------------------------------------------------
' <autogenerated>
'     This code was generated by a tool.
'     Runtime Version: 1.1.4322.573
'
'     Changes to this file may cause incorrect behavior and will
'     be lost if the code is regenerated.
' </autogenerated>
'------------------------------------------------------------------

Option Strict Off
Option Explicit On

Imports System
Imports System.ComponentModel
Imports System.Diagnostics
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Xml.Serialization

'
'This source code was auto-generated by Microsoft.VSDesigner,
'Version 1.1.4322.573.
'
Namespace localhost2
    
    '<remarks/>
    <System.Diagnostics.DebuggerStepThroughAttribute(),  _
     System.ComponentModel.DesignerCategoryAttribute("code"),  _
     System.Web.Services.WebServiceBindingAttribute(  _
          Name:="Service1Soap", [Namespace]:=  _
          "http://tempuri.org/WebService1/Service1")> _
    Public Class Service1
        Inherits System.Web.Services.Protocols.SoapHttpClientProtocol
        
        '<remarks/>
        Public Sub New()
            MyBase.New
            Me.Url = "http://localhost/WebService1/Service1.asmx"
        End Sub
        
        '<remarks/>
        <System.Web.Services.Protocols.SoapDocumentMethodAttribute(
             "http://tempuri.org/WebService1/Service1/HelloWorld",
             RequestNamespace:="http://tempuri.org/WebService1/Service1",
             ResponseNamespace:="http://tempuri.org/WebService1/Service1",
             Use:=System.Web.Services.Description.SoapBindingUse.Literal,
             ParameterStyle:=  _
                  System.Web.Services.Protocols.  _
                  SoapParameterStyle.Wrapped)>  _
        Public Function HelloWorld() As String
            Dim results() As Object = Me.Invoke("HelloWorld", _
                                                New Object(-1) {})
            Return CType(results(0),String)
        End Function
        
        '<remarks/>
        Public Function BeginHelloWorld(ByVal callback _
               As System.AsyncCallback, ByVal asyncState _
               As Object) As System.IAsyncResult
            Return Me.BeginInvoke("HelloWorld", New Object(-1) {}, _
                                  callback, asyncState)
        End Function
        
        '<remarks/>
        Public Function EndHelloWorld(ByVal asyncResult _
               As System.IAsyncResult) As String
            Dim results() As Object = Me.EndInvoke(asyncResult)
            Return CType(results(0),String)
        End Function
    End Class
End Namespace

The XML Web services technology in .NET manages all of this code for you. All you have to do is treat the Web service like a black box and interact with its external interface. Listing 3 shows how easy it is to call the HelloWorld Web method.

Listing 3: Call a Web service's methods like any other method, by creating an instance of the class and invoking the method.

Module Module1

    Sub Main()
        Dim Service As localhost2.Service1 = New localhost2.Service1
        Console.WriteLine(Service.HelloWorld())
        Console.WriteLine("press enter")
        Console.ReadLine()
    End Sub

End Module

In the code, you declare an instance of the Web service, including the namespace. By default, the Web service is named Service1. Like any other object, I need to create an instance of the class and then simply invoke the class's behaviors.

Advanced Web Service Information

To do even more advanced things with Web services, you need to know more. The example invoked the Web service synchronously. This means the next line of code doesn't execute until the Web method returns. If you have a long-running process, you may want to invoke the Web method asynchronously. Asynchronous invocation permits your code to go and do other things without waiting for the Web service to return.

To use asynchronous Web services, you need to be comfortable with creating and using delegates. You also need to remember that the asynchronous Web method returned comes back on a separate thread from the invoking thread. You need to be comfortable with the concept of marshalling data across threads by using the Invoke method and more delegates.

Also, you can pass and return composite types across Web services, but by default the client application works with a flattened, properties-only version of the composite type, except in the case of ADO.NET DataSets. You also can typecast a flattened Web service-defined proxy class to a class with methods, but this means deploying your business classes to consumers or consumers creating their own business classes. Deploying business classes is not recommended.

Finally, you could use Dotfuscator to obfuscate—scramble the MSIL into gibberish—to prevent consumers from using Anakrino or Reflector to decompile your business classes, but again, you may not always be able to get your business classes deployed to consumer machines. Hence, it is better to design applications that use Web services in such a manner as to not need to deploy business classes.

XML Web Services Will Do

XML Web services in .NET are built on top of .NET Remoting. For all intents and purposes, a Web service is marshal-by-value .NET Remoting. The rumor mill also suggests that .NET Remoting may be completely concealed by XML Web services in the future, making advanced features of Remoting like event sinks available using Web services.

I hope you are more comfortable with XML Web services. Now, when you have to choose between Remoting and Web services, you are prepared to make a decision. Most of the time when you create distributed applications, XML Web services will do.

Biography

Paul Kimmel is the VB Today columnist for www.codeguru.com and has written several books on object-oriented programming and .NET. Check out his book Visual Basic .NET Power Coding from Addison-Wesley, and his upcoming book UML DeMystified from McGraw-Hill/Osborne (Spring 2005). Paul is also the founder and chief architect for Software Conceptions, Inc, founded 1990. He is available to help design and build software worldwide. You may contact him for consulting opportunities or technology questions at pkimmel@softconcepts.com.

If you are interested in joining or sponsoring a .NET Users Group, check out www.glugnet.org.

Copyright © 2004 by Paul Kimmel. All Rights Reserved.





Page 3 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel