1. #21
    Sencha User
    Join Date
    Jul 2007
    Posts
    8
    Vote Rating
    0
    GodKnow is on a distinguished road

      0  

    Default


    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:
    Code:
    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!";
            }
    
        }
    }
    I used different ProviderName and Namespace in class compare to the same items of Sample class in Sample.ashx
    Then, I add following code to default.aspx.
    Code:
    <script type="text/javascript" src="test.ashx"></script>
    After that client will return results.

    Code:
    Ext.app.REMOTING_API_1 = {"type":"remoting","url":"/control/ExtDirectSample2/ExtDirectSample/test.ashx"
    ,"namespace":"MyApp_1","actions":{"test":[{"name":"SayHello","len":0}]}};
    How to make the client return test class using the same ProviderName and Namespace?

    thx.

  2. #22
    Ext User
    Join Date
    Jun 2009
    Posts
    5
    Vote Rating
    0
    lucg is on a distinguished road

      0  

    Default


    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

  3. #23
    Sencha - Ext JS Dev Team evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    17,065
    Vote Rating
    659
    evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute

      0  

    Default


    @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!

  4. #24
    Sencha - Ext JS Dev Team evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    17,065
    Vote Rating
    659
    evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute

      0  

    Default


    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!

  5. #25
    Sencha User
    Join Date
    Jul 2007
    Posts
    8
    Vote Rating
    0
    GodKnow is on a distinguished road

      0  

    Thumbs up


    Quote Originally Posted by evant View Post
    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.

    This is great! Thanks so much, Evan.

  6. #26
    Ext User
    Join Date
    Jun 2009
    Posts
    5
    Vote Rating
    0
    lucg is on a distinguished road

      0  

    Default


    Do I need to create a custom json serializer for custom classes returned from a direct action?

  7. #27
    Sencha - Ext JS Dev Team evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    17,065
    Vote Rating
    659
    evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute

      0  

    Default


    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!

  8. #28
    Ext User
    Join Date
    Jun 2009
    Posts
    5
    Vote Rating
    0
    lucg is on a distinguished road

      0  

    Default


    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

  9. #29
    Sencha - Ext JS Dev Team evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    17,065
    Vote Rating
    659
    evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute

      0  

    Default


    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!

  10. #30
    Ext User
    Join Date
    Jun 2009
    Posts
    5
    Vote Rating
    0
    lucg is on a distinguished road

      0  

    Default


    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:

    Code:
    return objectType.IsSubclassOf(typeof(JContainer));
    By doing this, the converter won't get in the way of other types it can't convert.

    2) We have to remove the attribute [JsonConverter(typeof(ResultConverter))] from the Result property in DirectResponse class.

    3) Add JsonConverters property to DirectHandler:
    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()
                    };
                }
            }
    4) Add JsonConverters parameter to DirectProcessor.Execute in DirectHandler file, line 42:
    Code:
    data = DirectProcessor.Execute(provider, context.Request, this.JsonConverters);
    5) In DirectProcessor class, apply modifications to Execute method:
    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("&quot;", "\\&quot;"));
                    }
                    else
                    {
                        return JsonConvert.SerializeObject(response, jsonConverters);
                    }
    
                }
            }
    Now, in the DirectHandler class in my application, I can add the override property JsonConverters. Mine looks like this:
    Code:
    public override JsonConverter[] JsonConverters
            {
                get
                {
                    return new JsonConverter[] {
                        new Ext.Direct.JContainerJsonConverter(),
                        new SearchResultJsonConverter()
                    };
                }
            }
    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.

    I hope that helps. What do you think?