Ajax.net 3.5 JSON and WCF Serialization

  • Thread starter Stilgar[bbs.iscabbs.com]
  • Start date
S

Stilgar[bbs.iscabbs.com]

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":"(e-mail address removed)","UserName":"LeeroyJenkins"}}

What am I overlooking?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,794
Messages
2,569,641
Members
45,355
Latest member
SJLChristi

Latest Threads

Top