|
Building Web Services Using Microsoft.net, by Jonathan Cortez
WSJ Vol 01 Issue 01 - pg.49
Listing 1 Class contained in the same .asmx file
<% @WebService Language="C#" Class="FooBar" %>
using System.Web.Services;
public class FooBar
{
[ WebMethod ]
public string GetMessage()
{
return "This class is contained in the same .asmx file.";
}
}
Listing 2 Class contained in a separate assembly
In FooBar.asmx:
<% @WebService Language="C#" Class="FooBar,MyAssembly" %>
In MyAssembly.cs:
using System.Web.Services;
public class FooBar
{
[ WebMethod ]
public string GetMessage()
{
return "This class is contained in a separate assembly.";
}
}
Listing 3 Overriding the default XML namespace
<% @WebService Language="C#" Class="FooBar" %>
using System.Web.Services;
[ WebService(Namespace="http://www.mycompanyname.com/") ]
public class FooBar
{
[ WebMethod ]
public string Hello()
{
return "Hello Web Services.";
}
}
Listing 4 Defining Web service methods
<% @WebService Language="C#" Class="FooBar" %>
using System.Web.Services;
public class FooBar
{
[ WebMethod(Description="Returns a greeting to the caller.") ]
public string Hello()
{
return "Hello Web Services.";
}
public string DoSomeWork()
{
return "I did some work."
}
}
Listing 5 Calculator Web service (Calculator.asmx)
<%@ WebService Language="C#" Class="Calculator" %>
using System.Web.Services;
[ WebService(Namespace="http://www.us.cgey.com/webservices/calculator") ]
public class Calculator
{
[ WebMethod(Description="Returns the sum of two numbers.") ]
public long Add(long lNum1, long lNum2)
{
return lNum1 + lNum2;
}
[ WebMethod(Description="Returns the difference of two numbers.") ]
public long Subtract(long lNum1, long lNum2)
{
return lNum1 - lNum2;
}
[ WebMethod(Description="Returns the product of two numbers.") ]
public long Multiply(long lNum1, long lNum2)
{
return lNum1 * lNum2;
}
[ WebMethod(Description="Returns the quotient of two numbers.") ]
public long Divide(long lNum1, long lNum2)
{
return lNum1 / lNum2;
}
}
Listing 6 Calculator Web service client (CalculatorClient.cs)
using System;
public class CalculatorClient
{
public static void Main()
{
// Create the proxy
Calculator oCalcProxy = new Calculator();
// Invoke the Multiply method and display results on the screen
Console.WriteLine("4 X 4 = " + oCalcProxy.Multiply(4, 4));
}
}
|