views:

405

answers:

4

I was asked a question in interview

can there be 2 webmethods with same name inside a web service in c#.

With function overloading it is possible but interviewer wanted an answer without using function overloading.

My answer was No. IS it correct.Please comment.

+2  A: 

No, you can't have two methods with the same name and same signature. That doesn't make much sense.

BobbyShaftoe
He did not say same signature. That wont even compile!
leppie
@leppie, I'm not sure this makes any sense to me whatsoever. If you have two functions with the same name and aren't doing function overloading then you have the same signature. Now, there's something else interesting about this. Having such a thing will not compile and thus will not work. So, this doesn't make much sense to me either.
BobbyShaftoe
+1  A: 

I guess you can if you expose them as different external names (if that is possible).

leppie
exactly - same in WCF - you need to give them distinct externally visible names
marc_s
+1  A: 

Actually, it is possible with a little bit of work. The key is changing the message name:

(taken from the link below)

[WebMethod(MessageName="Add_TwoNumbers")]

http://scottwhite.blogspot.com/2005/09/overloading-web-service-methods-in-net.html

Kevin
+1  A: 

It can be done as you can read here: http://www.codeproject.com/KB/webservices/OverloadingInWebService.aspx

The reason why you need to do this "special" stuff is just because WSDL does't support the same method names...

Edit: I see the link forgot about the WebServiceBinding:

[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.None)]
public class TestService : System.Web.Services.WebService  {

}
Zenuka
But this is about overloading.
BobbyShaftoe