Web services serialization?

Last post 07-24-2007 2:28 PM by Ry@n. 6 replies.

Sort Posts:

  • Web services serialization?

    07-23-2007, 12:33 PM
    • Loading...
    • Ry@n
    • Joined on 07-23-2007, 11:59 AM
    • Posts 8

     Hello,

    I am trying to create a simple ASP.NET web service that can return a string as its output (such as "Hello world!") without it being wrapped in XML (such as "<string>Hello world!</string>").  This seems like a very simple task, but I have been searching for a solution for hours with no success.  The reason I want to do this is so that I can write a web service which can be referenced in the SRC attribute of an HTML SCRIPT tag, similar to how Yahoo's JSON APIs work.

    Does anyone know how to accomplish this?  The code I am using is:

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello world!";
        }

    Thanks!
    Ryan

    Filed under: , ,
  • Re: Web services serialization?

    07-23-2007, 3:47 PM
    • Loading...
    • Vinija
    • Joined on 05-11-2007, 9:45 AM
    • Posts 76

    If you want to have your web method working in JSON mode
    you have to apply [ScriptService] attribute to your web service class.
     

    Ran.
    http://blogs.microsoft.co.il/blogs/ranw
    Filed under: , ,
  • Re: Web services serialization?

    07-23-2007, 3:59 PM
    • Loading...
    • Ry@n
    • Joined on 07-23-2007, 11:59 AM
    • Posts 8

    Vinija, thanks for your suggestion (I should have included more code in my example).  I am in fact using the [ScriptService] attribute in my web service class, but it has no effect -- when I browse to http://localhost:49526/trunk/WebServiceTest.asmx/HelloWorld, I still see the following result:

    <?xml version="1.0" encoding="utf-8" ?>
      <string xmlns="http://tempuri.org/">Hello world!</string>
  • Re: Web services serialization?

    07-23-2007, 8:28 PM
    Answer
    • Loading...
    • Steve Marx
    • Joined on 05-26-2006, 8:35 PM
    • Microsoft
    • Posts 643

    I'm not familiar with the Yahoo API, but to be able to use this with a <script> tag, you'll need to use something called "JSON-P" or "JSON with Padding".  Basically that returns a string that looks like: "callback('Hello world!');"  It's the JSON-encoded return value of your web service wrapped in parentheses and prepended with a callback name.  That way you can implement a function on the page called "callback" and get passed the result of the web service call.

    ASP.NET AJAX can be used to use an HTTP GET (see the ScriptMethod attribute for configuring HTTP GET versus POST) to retrieve a JSON-encoded string, but it doesn't currently wrap it for you with a callback method.

    I think the simplest thing for you to do is to build your own handler, and just do something like:

    Response.Write(string.Format("callback({0});", new JavaScriptSerializer().Serialize("Hello, World!")));

    Steve Marx | ASP.NET AJAX Evangelist | Microsoft Corporation
  • Re: Web services serialization?

    07-23-2007, 8:31 PM
    • Loading...
    • Steve Marx
    • Joined on 05-26-2006, 8:35 PM
    • Microsoft
    • Posts 643

    I just realized that "build your own handler" sounds scary... I just mean a .ashx.  I'm basically suggesting that instead of a web service.

    Steve Marx | ASP.NET AJAX Evangelist | Microsoft Corporation
  • Re: Web services serialization?

    07-23-2007, 11:38 PM
    • Loading...
    • Vinija
    • Joined on 05-11-2007, 9:45 AM
    • Posts 76

    Well, there is nothing wrong with seeing XML tags on the response however you can also do
    the following.

     
    1. In your page's script manager add reference to your web service

    (This should be included inside your scriptmanager tag :  <asp:ServiceReference Path="~\YourService.asmx" /> )

    2. Call yor web method from your page's client side <YourService>.<YourMethodName>(<yourparameter>,onSuccess, onError)
       while onSuccess and onError are two callbacs invoked on seccess / error respectively.

    3. At your onSeccuss function you can do the following:
       

    function onSuccess(result)

    {

        alert(result); 

    {
     

    and the returned string will apear. 

    Ran.
    http://blogs.microsoft.co.il/blogs/ranw
  • Re: Web services serialization?

    07-24-2007, 2:28 PM
    • Loading...
    • Ry@n
    • Joined on 07-23-2007, 11:59 AM
    • Posts 8

    Thanks Steve, using a generic handler did the trick -- it seems to be just what I was looking for.

Page 1 of 1 (7 items)
Microsoft Communities
Page view counter