Ajax.net 3.5 JSON and WCF Serialization

Discussion in 'ASP .Net Web Services' started by Stilgar[bbs.iscabbs.com], Feb 12, 2010.

  1. I'm trying some WCF ajax.net 3.5 out for the first time, and I'm
    having some difficulties. I have a WCF Service which I have connected
    to in my aspx page using <asp:ScriptManager>. I have a custom object
    which has been decorated as a [DataContract]. I'm calling the object
    from javascript successfully, I can see the JSON response in FireBug,
    but Sys.Serialization.JavaScriptSerializer.deserialize() chokes on
    it. I'm sure I'm making a noob mistake, but I used up all my patience
    getting the Web.Config values right for running the WCF service :)

    Here's a mockup of what I'm doing (hopefully wrapping wont make it
    unreadable):

    // The custom object

    namespace my.namespacehere
    {
    [DataContract]
    public class UserAccountData : IComparible
    {
    [DataMember]
    public Guid AccountGuid { get; set; }

    [DataMember]
    public string Email { get; set; }

    [DataMember]
    public string UserName { get; set; }

    // Ctor and IComparible stuff omitted
    }
    }

    // The WCF Service .svc.cs code

    namespace my.namespacehere
    {
    [ServiceContract( Namespace = "AdminServices" )]
    [AspNetCompatibilityRequirements( RequirementsMode =
    AspNetCompatibilityRequirementsMode.Allowed)]
    public class LookupServices
    {
    [OperationContract]
    public UserAccountData GetUserAccountData( Guid accountGuid )
    {
    // The code for the static lookup isn't included, but you
    get the jist of it
    return UserAccount.GetUserAccountData( accountGuid );
    }
    }
    }

    // Okay, here's the javascript code in the .aspx

    <script type="text/javascript" language="javascript">
    function getIt( userGuidReadFromInput )
    {

    AdminServices.LookupServices.GetUserAccountData( userGuidReadFromInput,

    onSuccess, onFailure, null);
    }

    function onSuccess( result )
    {
    try
    {
    var parsed =
    Sys.Serialization.JavaScriptSerializer.deserialize( result );
    }
    catch (e)
    {
    // I _ALWAYS_ hit this with error:
    // Cannot convertSys.ArgumentTypeException:
    Sys.ArgumentTypeException:
    // Object of type 'Object' cannot be converted to type
    'String'. Parameter name: data
    alert("exception! " + e);
    return;
    }
    // If I got this far, I'd expect parsed.AccountGuid,
    parsed.UserName and parsed.Email
    // to contain my data
    }
    </script>


    I can see the JSON being returned from the .svc in Firebug and it
    looks like this:

    {"d":
    {"__type":"UserAccountData:#my.namespacehere","AccountGuid":"ebba480e-8b85-4277-
    afda-
    f57f144af0a9","Email":"","UserName":"LeeroyJenkins"}}

    What am I overlooking?
    Stilgar[bbs.iscabbs.com], Feb 12, 2010
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Simon
    Replies:
    0
    Views:
    1,010
    Simon
    Oct 13, 2009
  2. Cindy Lee
    Replies:
    1
    Views:
    2,109
    Mr. Arnold
    Mar 19, 2010
  3. Florian Frank
    Replies:
    0
    Views:
    240
    Florian Frank
    Jun 30, 2009
  4. sajuptpm
    Replies:
    2
    Views:
    337
    sajuptpm
    Dec 28, 2012
  5. Acácio Centeno
    Replies:
    1
    Views:
    254
    dieter
    Feb 15, 2013
Loading...

Share This Page