Providing an IFCE Business Solution in Visual Basic .NET


Providing an IFCE Business Solution in
Visual Basic .NET

Web services can be called from any front-end application. This means both Windows forms and web forms can invoke the same services. In fact, .NET web services can be called by non-Microsoft applications. As we have observed in previous chapters, a developer building a J2EE can call a web service built in Microsoft .NET because web services are universally accessible. Application interoperability is the key.

Currency conversion is a service frequently needed by corporations participating in a financial business. A consumer can walk into a local bank branch or a business such as the Automobile Association to buy or sell foreign currency. IFCE provides such services. Let’s build a web service in Visual Studio .NET to provide a currency conversion service.

Open Visual Studio .NET to create a project of the type ASP.NET web service. Name the project CurrencyConverter and ensure that the server selected for hosting the web service has a web server running IIS 4.0 or higher and containing a copy of the .NET Framework. Once the project is loaded, a blank designer appears. Use Solution Explorer to view how Visual Studio creates the project and presents a hierarchical tree listing the following items:

  • References to System namespaces as follows: System, System.Data, System.Web, System.Web.Services, and System.XML.

  • The AssemblyInfo.vb namespace, which includes information about the assembly, such as file name and path.

  • The Global.asax config file, which specifies the System.Web.HttpApplication namespace. It maps values in the application configuration file <appSettings>.

  • Service1.asmx, named by Visual Studio with the .asmx extension. This extension specifies a web service.

  • The Web.config file, which consists of tags and attributes.

The following config file has been generated for the CurrencyConverter Web Service:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<!-- DYNAMIC DEBUG COMPILATION
Set compilation debug="true" to insert debugging symbols (.pdb information)
into the compiled page. Because this creates a larger file that executes
more slowly, you should set this value to true only when debugging and to
false at all other times. For more information, refer to the documentation
about debugging ASP.NET files.
-->
<compilation defaultLanguage="vb" debug="true" />

<!-- CUSTOM ERROR MESSAGES
Set customErrors mode="On" or "RemoteOnly"
to enable custom error messages, "Off"
to disable.
Add <error> tags for_u101 each of the errors you want to handle.

"On" Always display custom (friendly) messages.
"Off" Always display_u100 detailed ASP.NET error information.
"RemoteOnly" Display_u99 custom (friendly)
messages only to users not running on the local Web server.
This setting is recommended for security purposes, so
that you do not display application detail information to remote clients.
-->
<customErrors mode="RemoteOnly" />

<!-- AUTHENTICATION
This section sets the authentication policies
of the application. Possible modes are "Windows",
"Forms", "Passport" and "None"
"None" No authentication is performed.
"Windows" IIS performs authentication
(Basic, Digest, or Integrated Windows) according to
its settings for the application. Anonymous access must be disabled in IIS.
"Forms" You provide a custom form (Web page)
for users to enter their credentials, and then
you authenticate them in your application.
A user credential token is stored in a cookie.
"Passport" Authentication is performed
via a centralized authentication service provided
by Microsoft that offers a single
logon and core profile services for member sites.
-->

<authentication mode="Windows" />
<!-- AUTHORIZATION
This section sets the authorization policies
of the application. You can allow or deny access
to application resources by user or role.
Wildcards: "*" means everyone, "?" means anonymous
(unauthenticated) users.
-->
<authorization>
<allow users="*" /> <!-- Allow all users -->

<!-- <allow users="[comma separated list of users]"
roles="[comma separated list of roles]"/>
<deny users="[comma separated list of users]"
roles="[comma separated list of roles]"/>
-->
</authorization>

<!-- APPLICATION-LEVEL TRACE LOGGING
Application-level tracing enables trace log
output for every page within an application.
Set trace enabled="true" to enable application
trace logging. If pageOutput="true", the
trace information will be displayed at the bottom of each page.
Otherwise, you can view the
application trace log by browsing the "trace.axd"
page from your web application root.
-->
<trace enabled="false" requestLimit="10"
pageOutput="false" traceMode="SortByTime"
localOnly="true" />
<!-- SESSION STATE SETTINGS
By default ASP.NET uses cookies to identify
which requests belong to a particular session.
If cookies are not available, a session
can be tracked by adding a session identifier to the URL.
To disable cookies, set sessionState cookieless="true".
-->
<sessionState
mode="InProc"
stateConnectionString="tcpip=127.0.0.1:42424"
sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes"
cookieless="false"
timeout="20"
/>

<!-- GLOBALIZATION
This section sets the globalization settings of the application.
-->
<globalization requestEncoding="utf-8"
responseEncoding="utf-8" />

</system.web>
</configuration>

The structure of the Web.config file is XML based with the following format:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<elementName1>
<childElementName1
attributeName1=value
attributeName2=value
attributeNameN=value />
</elementName1>
<elementName2>
<childElementName1
attributeName1=value
attributeName2=value
attributeNameN=value />
</elementName2>
</system.web>
</configuration>

Each Web.config file should contain the standard XML declaration. The file also contains opening and closing <configuration> tags. Nested within the configuration tags are the <system.web> opening and closing tags. Following these tags are the attribute tags associated with a specific value. Refer to them as elements.

Returning to the development of the web service, right-click on the Service1.asmx file in the Solution Explorer and select Rename. Then name the file CurrencyConvert.asmx. Select “click here to switch to code view” to open the code-behind file named CurrencyConvert.asmx.vb. The following code demonstrates how this page looks:

Imports System.Web.Services

<System.Web.Services.WebService (Namespace := "http://tempuri.org/
CurrencyConverter/Service1")> _
Public Class Service1
Inherits System.Web.Services.WebService
#Region " Web Services Designer Generated Code "
Public Sub New()
MyBase.New()
'This call is required by the Web Services Designer.
InitializeComponent()
'Add your own initialization code
after the InitializeComponent() call
End Sub
'Required by the Web Services Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Web Services Designer
'It can be modified using the Web Services Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()>
Private Sub InitializeComponent()
components = New System.ComponentModel.Container()
End Sub

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
'CODEGEN: This procedure is required by the Web Services Designer
'Do not modify it using the code editor.
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
#End Region

' WEB SERVICE EXAMPLE
' The HelloWorld() example service returns the string Hello World.
' To build, uncomment the following lines,
then save and build the project.
' To test this web service, ensure that the .asmx file is the start page
' and press F5.
'
'<WebMethod()> _
'Public Function HelloWorld() As String
' Return "Hello World"
'End Function

End Class

Note how the Web Service example is commented out. The function name is HelloWorld(). Also observe how WebMethod has a less-than symbol placed before it. This tells the compiler to treat this function as a web service and marks it as callable on the Web.

Add the following code to the file:

<WebMethod()> Public Function
ConvertCurrency
(ByVal dAmount As Decimal, ByVal sFrom As String,
ByVal sTo As String) As Decimal
Select Case sFrom
Case "Euro"
Return CDec(dAmount * 1.08)
Case "Mark"
Return CDec(dAmount * 1.64)
Case "Sterling"
Return CDec(dAmount * 1.65)
Case "Lire"
Return CDec(dAmount * 1.998)
Case "Francs"
Return CDec(dAmount * 1.4435)
End Select
End Function

The code creates the method named CurrencyConvert that accepts three parameters:

  • Amount of currency to convert

  • Type of currency

  • Type of currency to convert to

The Select Case statement ascertains whether the user is converting from the euro or Deutsche mark and some other currency. In each case, the currency is converted into American currency.

Select the Build menu and choose Build CurrencyConverter. This step creates the web service on the web server.

The next step is to test the web service. Right-click on CurrencyConvert.asmx in Solution Explorer and select View in Browser. A default page is created by .NET that allows the user to submit the amount and type of currency to be converted. The CurrencyConvert Web service is now ready to be invoked by a Web Service client. Click on the CurrencyConverter link that appears on the operations page.

Here is the default page:

click to expand

The results are displayed in Internet Explorer:

<?xml version="1.0" encoding="utf-8" ?>
<decimal xmlns="http://tempuri.org/CurrencyConverter/Service1">135</decimal>

The SOAP headers displayed here are automatically generated by Visual Studio and are self-explanatory.

The following is a sample SOAP request and response.
The placeholders shown need
to be replaced with actual values.
POST /CurrencyConverter/CurrencyConvert.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/CurrencyConverter/Service1/ConvertCurrency"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd=
"http://www.w3.org/2001/XMLSchema"
xmlns:soap=
"http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ConvertCurrency xmlns="http://tempuri.org/CurrencyConverter/Service1">
<dAmount>decimal</dAmount>
<sFrom>string</sFrom>
<sTo>string</sTo>
</ConvertCurrency>
</soap:Body>
</soap:Envelope>
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap=
"http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ConvertCurrencyResponse xmlns="http://tempuri.org/CurrencyConverter/Service1">
<ConvertCurrencyResult>decimal</ConvertCurrencyResult>
</ConvertCurrencyResponse>
</soap:Body>
</soap:Envelope>
HTTP POST
The following is a sample HTTP POST
request and response.
The placeholders shown
need to be replaced with actual values.
POST /CurrencyConverter/CurrencyConvert.asmx/ConvertCurrency HTTP/1.1
Host: localhost
Content-Type: application/x-www-form-urlencoded
Content-Length: length

dAmount=string&sFrom=string&sTo=string
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<decimal xmlns="http://tempuri.org/
CurrencyConverter/Service1">decimal</decimal>

The web service is protocol agnostic and provides interoperability 24/7 to any client residing on any platform or operating system.




.NET & J2EE Interoperability
Microsoft .NET and J2EE Interoperability Toolkit (Pro-Developer)
ISBN: 0735619220
EAN: 2147483647
Year: 2004
Pages: 101
Authors: Simon Guest

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net