Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method overloading in webservices

I have 2 question related to the web services.

  1. How we achieve the method overloading in web services.
  2. How to implement security(authentication) in web services.
like image 230
Vijjendra Avatar asked Apr 16 '11 19:04

Vijjendra


People also ask

Can we overload method in web service?

This is a very common interview question as well: Is it possible to overload a web method in a web service? The answer is yes, you need to use MessageName property for this.

What is method overloading and example?

In Java, two or more methods may have the same name if they differ in parameters (different number of parameters, different types of parameters, or both). These methods are called overloaded methods and this feature is called method overloading. For example: void func() { ... }

What are the methods of overloading?

Overloading Methods Method overloading can be achieved by the following: By changing the number of parameters in a method. By changing the order of parameters in a method. By using different data types for parameters.

Is method overloading possible in WCF?

So clearly by default WCF does not allow you to overload methods in service. However there is one attribute in OperationContract which can allow you to overload method at service end.


2 Answers

How we achieve the method overloading in web services.

If you are using SOAP you can't. Method names must have unique names in the exported WSDL. Depending on the technology you are using there are different ways to specify a method name. For example in WCF you could use the [OperationContract] attribute to specify a name:

[ServiceContract]
public interface IMyService
{
    [OperationContract(Name = "Foo")]
    void Foo();

    [OperationContract(Name = "FooWithId")]
    void Foo(int id);
}

How to implement security(authentication) in web services.

The following guide is a very good start for implementing security in WCF.

like image 168
Darin Dimitrov Avatar answered Nov 09 '22 17:11

Darin Dimitrov


Okay for overloading:

[WebMethod(MessageName = "MaxInt", Description = "Compare two int values 
and return the max value", EnableSession = true)]
public int MaxValue(int a, int b)
{
   return (a > b ? a : b);
}
[WebMethod(MessageName = "MaxFloat", Description = "Compare two float values 
and return the max value", EnableSession = true)]
public float MaxValue(float a, float b)
{
   return (a > b ? a : b);
}

What do you mean precisely by authentication? You can obviously use a validation key to access webservice. The question is confusing. Elaborate please.

like image 43
Naveed Ahmad Avatar answered Nov 09 '22 16:11

Naveed Ahmad