Skip to content

Instantly share code, notes, and snippets.

@tufanbarisyildirim
Last active December 24, 2015 07:28

Revisions

  1. tufanbarisyildirim revised this gist Sep 30, 2013. 3 changed files with 20 additions and 36 deletions.
    19 changes: 0 additions & 19 deletions Extensions.cs
    Original file line number Diff line number Diff line change
    @@ -1,19 +0,0 @@
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web.Script;
    using System.Web.Script.Serialization;
    using System.Web.Script.Services;

    namespace ServiceTest
    {
    public static class Extensions
    {
    public static string JsonSerialize(this object output)
    {
    JavaScriptSerializer js = new JavaScriptSerializer();
    return js.Serialize(output);
    }
    }
    }
    17 changes: 0 additions & 17 deletions Result.cs
    Original file line number Diff line number Diff line change
    @@ -1,17 +0,0 @@
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    namespace ServiceTest
    {
    [Serializable]
    public class Result
    {
    public bool Success { get; set; }


    private string _message = "";
    public string Message { get { return _message; } set { _message = value; } }
    }
    }
    20 changes: 20 additions & 0 deletions ServiceJsonAdapter.cs
    Original file line number Diff line number Diff line change
    @@ -91,4 +91,24 @@ public void ProcessRequest(HttpContext context)

    #endregion
    }

    public static class Extensions
    {
    public static string JsonSerialize(this object output)
    {
    JavaScriptSerializer js = new JavaScriptSerializer();
    return js.Serialize(output);
    }
    }

    [Serializable]
    public class Result
    {
    public bool Success { get; set; }


    private string _message = "";
    public string Message { get { return _message; } set { _message = value; } }
    }

    }
  2. tufanbarisyildirim created this gist Sep 30, 2013.
    19 changes: 19 additions & 0 deletions Extensions.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web.Script;
    using System.Web.Script.Serialization;
    using System.Web.Script.Services;

    namespace ServiceTest
    {
    public static class Extensions
    {
    public static string JsonSerialize(this object output)
    {
    JavaScriptSerializer js = new JavaScriptSerializer();
    return js.Serialize(output);
    }
    }
    }
    17 changes: 17 additions & 0 deletions Result.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    namespace ServiceTest
    {
    [Serializable]
    public class Result
    {
    public bool Success { get; set; }


    private string _message = "";
    public string Message { get { return _message; } set { _message = value; } }
    }
    }
    94 changes: 94 additions & 0 deletions ServiceJsonAdapter.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,94 @@
    using System;
    using System.Web;
    using System.Reflection;
    using System.Linq;
    using System.Collections.Specialized;
    using System.Collections.Generic;

    namespace ServiceTest
    {
    public class ServiceJsonAdapter : IHttpHandler
    {

    HttpRequest Request
    {
    get
    {
    return HttpContext.Current.Request;
    }
    }

    HttpResponse Response
    {
    get
    {

    return HttpContext.Current.Response;
    }
    }

    /// <summary>
    /// You will need to configure this handler in the web.config file of your
    /// web and register it with IIS before being able to use it. For more information
    /// see the following link: http://go.microsoft.com/?linkid=8101007
    /// </summary>
    #region IHttpHandler Members

    public bool IsReusable
    {
    // Return false in case your Managed Handler cannot be reused for another request.
    // Usually this would be false in case you have some state information preserved per request.
    get { return true; }
    }

    public void ProcessRequest(HttpContext context)
    {
    // This is your real webservice instance. Example : MobileWebService.asmx
    MobileWebService MobService = new MobileWebService();
    Type ServiceType = typeof(MobileWebService);

    NameValueCollection Collection;

    if (Request.HttpMethod == "POST")
    Collection = Request.Form;
    else
    Collection = Request.QueryString;

    String WebMethodName = Request["WebMethod"];

    try
    {
    MethodInfo FoundMethod = ServiceType.GetMethod(WebMethodName);

    if (FoundMethod == null)
    throw new Exception("Metod Bulunamadı : " + WebMethodName);

    //string[] parameters = Collection.AllKeys.Where((x) => x != "WebMethod").SelectMany(Collection.GetValues, (k, v) => k).ToArray();

    ParameterInfo[] RealParams = FoundMethod.GetParameters();
    List<string> parameters = new List<string>();

    foreach (ParameterInfo param in RealParams)
    {
    if (Collection.AllKeys.Contains(param.Name))
    parameters.Add(Collection[param.Name]);
    else
    parameters.Add(String.Empty);
    }

    Response.Write(FoundMethod.Invoke(MobService, parameters.ToArray()).JsonSerialize());

    }
    catch (Exception e)
    {
    Response.Write(new Result()
    {
    Success = false,
    Message = e.Message
    }.JsonSerialize());
    }
    }

    #endregion
    }
    }