-
18 Jun 2009 12:38 AM #21
Sorry, I didn't make myself clear. What I means is...
I try to add a test class to the "ExtDirectSample" so that it can be seem by the client .
Here is how I do it:
I created a Test.ashx as below:
I used different ProviderName and Namespace in class compare to the same items of Sample class in Sample.ashxCode:namespace ExtDirectSample { [DirectAction] public class test : DirectHandler { public override string ProviderName { get { return "Ext.app.REMOTING_API_1"; } } public override string Namespace { get { return "MyApp_1"; } } [DirectMethod] public string SayHello() { return "Hello!"; } } }
Then, I add following code to default.aspx.
After that client will return results.Code:<script type="text/javascript" src="test.ashx"></script>
How to make the client return test class using the same ProviderName and Namespace?Code:Ext.app.REMOTING_API_1 = {"type":"remoting","url":"/control/ExtDirectSample2/ExtDirectSample/test.ashx" ,"namespace":"MyApp_1","actions":{"test":[{"name":"SayHello","len":0}]}};
thx.
-
19 Jun 2009 12:59 PM #22
Thank you Evant and all for the great work.
In my application, I replaced shibubh's Direct .net implementation by Router 0.4.
Since I made the change, my custom type can't be serialized.
Do you know why?
Error message: Unsupported type: TestExtJS.ServiceResponse. Use the JsonSerializer class to get the object's JSON representation.
Thrown from ResultConverter.cs
Custom class returned by DirectMethod:
public class ServiceResponse
{
public bool Success { get; set; }
public string Ex { get; set; }
public int Total { get; set; }
public object Data { get; set; }
}
I'm using .net 3.5
Thanks,
Luc
-
19 Jun 2009 6:46 PM #23
@GodKnow: I see what you're saying now. Let me have a think
Evan Trimboli
Sencha Developer
Twitter - @evantrimboli
Don't be afraid of the source code!
-
19 Jun 2009 8:27 PM #24
Ok, I've updated the main post. Basically I've added a few virtual methods that let you decide how to configure a handler, as opposed to just making them a one to one mapping between classes. See Group.ashx for an example of doing this.
Evan Trimboli
Sencha Developer
Twitter - @evantrimboli
Don't be afraid of the source code!
-
20 Jun 2009 7:00 AM #25
-
22 Jun 2009 7:05 AM #26
Do I need to create a custom json serializer for custom classes returned from a direct action?
-
22 Jun 2009 7:30 AM #27
Not really sure, I wouldn't think so, though I'm not really familiar with the internals of the JSON framework.
Evan Trimboli
Sencha Developer
Twitter - @evantrimboli
Don't be afraid of the source code!
-
25 Jun 2009 12:55 PM #28
I found out that the serializer was failing because the class ResultConverter was unable to convert my custom types.
I disabled it and now it works fine. What was the purpose of this converter? It was not present in version 0.1.
I also added a JsonConverter array to DirectHandler and modified DirectProcessor to call JsonConvert with the array. It enables the web application to add json converters by direct handler.
Do you intend to put the code online? I would contribute my changes.
Thanks,
Luc
-
25 Jun 2009 2:52 PM #29
The issue was that it wouldn't automatically serialize JObject or JArray types when returned from a method. If you have an alternate solution that makes these play well together that would be great.
Evan Trimboli
Sencha Developer
Twitter - @evantrimboli
Don't be afraid of the source code!
-
26 Jun 2009 6:17 AM #30
Hello evant,
Here is how I could make it works.
1) I renamed ResultConverter to JContainerJsonConverter, made the class public, and changed the CanConvert method to:
By doing this, the converter won't get in the way of other types it can't convert.Code:return objectType.IsSubclassOf(typeof(JContainer));
2) We have to remove the attribute [JsonConverter(typeof(ResultConverter))] from the Result property in DirectResponse class.
3) Add JsonConverters property to DirectHandler:
4) Add JsonConverters parameter to DirectProcessor.Execute in DirectHandler file, line 42:Code:/// <summary> /// Retrieves the list of JsonConverter to apply to the json serializer /// </summary> public virtual Newtonsoft.Json.JsonConverter[] JsonConverters { get { return new Newtonsoft.Json.JsonConverter[] { new JContainerJsonConverter() }; } }
5) In DirectProcessor class, apply modifications to Execute method:Code:data = DirectProcessor.Execute(provider, context.Request, this.JsonConverters);
Now, in the DirectHandler class in my application, I can add the override property JsonConverters. Mine looks like this:Code:public static string Execute(DirectProvider provider, HttpRequest httpRequest, JsonConverter[] jsonConverters) { List<DirectResponse> responses = new List<DirectResponse>(); if (!String.IsNullOrEmpty(httpRequest[DirectRequest.RequestFormAction])) { DirectRequest request = new DirectRequest(); request.Action = httpRequest[DirectRequest.RequestFormAction] ?? string.Empty; request.Method = httpRequest[DirectRequest.RequestFormMethod] ?? string.Empty; request.Type = httpRequest[DirectRequest.RequestFormType] ?? string.Empty; request.IsUpload = Convert.ToBoolean(httpRequest[DirectRequest.RequestFormUpload]); request.TransactionId = Convert.ToInt32(httpRequest[DirectRequest.RequestFormTransactionId]); request.Data = new object[] { httpRequest }; responses.Add(DirectProcessor.ProcessRequest(provider, request)); } else { UTF8Encoding encoding = new UTF8Encoding(); string json = encoding.GetString(httpRequest.BinaryRead(httpRequest.TotalBytes)); List<DirectRequest> requests = JsonConvert.DeserializeObject<List<DirectRequest>>(json); if (requests.Count > 0) { foreach (DirectRequest request in requests) { responses.Add(DirectProcessor.ProcessRequest(provider, request)); } } else { responses.Add(DirectProcessor.ProcessRequest(provider, JsonConvert.DeserializeObject<DirectRequest>(json))); } } if (responses.Count > 1) { return JsonConvert.SerializeObject(responses, jsonConverters); } else { DirectResponse response = responses[0]; if (response.IsUpload) { return String.Format("<html><body><textarea>{0}</textarea></body></html>", JsonConvert.SerializeObject(response).Replace(""", "\\"")); } else { return JsonConvert.SerializeObject(response, jsonConverters); } } }
Where JContainerJsonConverter is the converter defined in the Ext.Direct project; and SearchResultJsonConverter is a custom json converter defined in my application. If you don't override the JsonConverters property, it will serialize the same way as before, using the JContainer converter.Code:public override JsonConverter[] JsonConverters { get { return new JsonConverter[] { new Ext.Direct.JContainerJsonConverter(), new SearchResultJsonConverter() }; } }
I hope that helps. What do you think?


Reply With Quote
