views:

115

answers:

3

I read an article some time back that explaind how I could add a webservice function to my aspx file for my ajax callbacks to call. Now I can't find this article or any other documentation on this.

Anyone using this and can you explain how to do this?

Thanks

Endre

+2  A: 

You can call pretty much any method that has the attribute [WebMethod]

Perhaps this was the article you read?

David Hedlund
It was not this article, but thanks for the link. I liked the aproche there better then the one I read.
Endre
A: 

I'd bear in mind that it's often best to keep your web services seperate from the pages that call them.

To create a simple web service in an aspx file, you'd use something like this:

<%@ WebService Language="C#" Class="MyWebService" %>

using System;
using System.Web;
using System.Web.Services;

[WebService(Namespace = "http://www.example.com/webservices/MyWebService, Description = "My Web Service")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class MyWebService : WebService
{

    [WebMethod(Description = "Add two numbers and return the result.")]
public int AddNumbers(int first, int second) {
        return first + second;
}

}

If you are looking for solid cross platform dynamic JavaScript component which can talk to your web service I'd check out http://www.guru4.net/articoli/javascript-soap-client/en/ (I use this and highly recommend it).

Alternatively, you could use something like jQuery to access the REST interface, or parse the XML from the SOAP response yourself.

Iain Collins
-1: webservice+ajax demands `[ScriptService]` and `[ScriptMethod]`
Rubens Farias
This creates a full webservice, and not quite what I was looking for.
Endre
Rubens Farias: No it does not.
Iain Collins
"To invoke a Web service method from ECMAScript (JavaScript), you must apply the ScriptServiceAttribute": http://msdn.microsoft.com/en-us/library/system.web.script.services.scriptserviceattribute.aspx
Rubens Farias
Rubens Farias: ScriptServiceAttribute specific to ASP.NET AJAX and is not at all required to access a web service from JavaScript.
Iain Collins
Endre: Fair enough. I would give it some serious consideration if you are likely use this approach much, as from an architectural point of view it can make things much easier to centralise + separate your web services if you rely on them heavily (e.g. such as if you want access the same data from one that just that page), but it's valid to say that is overkill if it's just being used sparingly.
Iain Collins
I agree with you that any serious webservices should not be made the way I asked about. But in this project I need a webservice that's just a proxy for another webservice, and it's only in this one place I need it. I se now that my question was completely clear, but I wanted this webservice to reside in a aspx page that also contains normal web form stuff. Because of this I didn't think your solution would work for me.
Endre
+5  A: 

I believe you need to mark the method as a [WebMethod]

http://geekswithblogs.net/frankw/archive/2008/03/13/asp.net-ajax-callbacks-to-web-methods-in-aspx-pages.aspx

From the above article:

  1. The method MUST be static
  2. The method needs to be decorated with [WebMethod()]
  3. The method needs to be decorated with [ScriptMethod()] if you want to make a ASP.NET AJAX callback to it

public partial class Products : System.Web.UI.Page

{ [System.Web.Services.WebMethod()] [System.Web.Script.Services.ScriptMethod()]

public static List GetProducts(int cateogryID) {

// Put your logic here to get the Product list }

hearn
That was the article I read, thanks.
Endre
no problem! a pleasure ;-)
hearn