Last active
December 24, 2015 07:28
-
-
Save tufanbarisyildirim/6763661 to your computer and use it in GitHub Desktop.
an http module example to publish .net asmx webservices as a restful service.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
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; } } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment