2

I had a problem (which is now fixed) but I've no idea why...

I have an Ajax AutoCompleteExtender with a WebService method to populate like

<WebMethod()> _
Public Shared Function populateACE(prefixText As String) As List(Of String)

However this didn't work - I put a breakpoint in, and it didn't even get hit. However...

<WebMethod()> _
Public Function populateACE(prefixText As String) As List(Of String)

..does work (the only difference being not Shared).

Fair enough, but why? If you have an instance of a class then you can access Shared methods of it; if you don't have an instance of a class then you can access Shared methods of it. So what is going on behind the scenes?

El Ronnoco
  • 11,753
  • 5
  • 38
  • 65

1 Answers1

1

If you're calling a page method then it must be Shared/static. But when calling methods attached to asmx services, accordining to John Saunders in this question, Why are Static Methods not Usable as Web Service Operations in ASMX Web Services?, web methods can't be Shared/static by design.

I'd have to guess that both are design limitations in the pipelines that retrieve pages vs. web methods.

To quote the relevant part from John Saunders' answer..

The answer is: because you can't.

It's not designed that way. The design is that an instance of the web service class will be created, and then an instance method will be called.

..but still worth having a look at the full answer.

Community
  • 1
  • 1
James McLachlan
  • 1,368
  • 13
  • 27