Created
September 11, 2012 04:10
-
-
Save kareem613/3695898 to your computer and use it in GitHub Desktop.
blog post 2010/03/27/auto-gen-jquery-access-to-wcf-rest
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
| internal static string GenerateJqueryJs() | |
| { | |
| ServiceEndpointCollection endpoints = | |
| OperationContext.Current.Host.Description.Endpoints; | |
| OperationDescriptionCollection operations = null; | |
| Type contract = null; | |
| ServiceEndpoint currentEndpoint = | |
| endpoints.Where(e > e.Contract.Name == | |
| OperationContext.Current.EndpointDispatcher.ContractName).FirstOrDefault(); | |
| contract = currentEndpoint.Contract.ContractType; | |
| operations = currentEndpoint.Contract.Operations; | |
| String functions = GenerateFunctions(operations, contract); | |
| String classTemplate = JQueryTemplates.ClassTemplate; | |
| return classTemplate.Replace("{functions}", functions) | |
| .Replace("{class_name}", contract.Name); | |
| } |
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
| private static String GenerateFunctions(OperationDescriptionCollection operations, Type contract) | |
| { | |
| StringBuilder js = new StringBuilder(); | |
| foreach (OperationDescription operation in operations) | |
| { | |
| js.Append(Environment.NewLine); | |
| WebGetAttribute get = operation.Behaviors.Find<WebGetAttribute>(); | |
| WebInvokeAttribute invoke = operation.Behaviors.Find<WebInvokeAttribute>(); | |
| MethodInfo method = contract.GetMethod(operation.Name); | |
| if (get != null) | |
| { | |
| js.Append("\r\n" + | |
| GenerateGetMethod(JQueryTemplates.GetTemplate, get, method) + ","); | |
| js.Append("\r\n" + | |
| GenerateGetMethod(JQueryTemplates.GetByUriTemplate, get, method) + ","); | |
| } | |
| else if (invoke != null) | |
| { | |
| switch (invoke.Method) | |
| { | |
| case "PUT": | |
| js.Append("\r\n" + | |
| GenerateInvokeMethod(JQueryTemplates.PostTemplate,invoke, method) + ","); | |
| break; | |
| case "DELETE": | |
| js.Append("\r\n" + | |
| GenerateInvokeMethod(JQueryTemplates.DeleteTemplate,invoke, method) + ","); | |
| break; | |
| case "POST": | |
| js.Append("\r\n" + | |
| GenerateInvokeMethod(JQueryTemplates.PostTemplate,invoke, method) + ","); | |
| break; | |
| default: | |
| continue; | |
| } | |
| } | |
| else | |
| { | |
| continue; | |
| } | |
| } | |
| return js.ToString().TrimEnd(' ', '\n', '\r', ','); | |
| } |
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
| public const string GetTemplate = @" | |
| {method_name} : function({params}successCallback, errorCallback) { | |
| jQuery.ajax({ | |
| type: 'GET', | |
| url: {uri}, | |
| contentType: 'application/json; charset=utf-8', | |
| success: function (responseText) { | |
| var response = eval(responseText); | |
| successCallback(response); | |
| }, | |
| error: function (xhr, ajaxOptions, thrownError) { | |
| var error = eval('(' + xhr.responseText + ')'); | |
| errorCallback(error); | |
| } | |
| }); | |
| }"; |
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
| [WebGet(UriTemplate = "/jqueryservice.js", BodyStyle = WebMessageBodyStyle.Bare)] | |
| [OperationContract] | |
| public Stream Javascript() | |
| { | |
| string jsFileContents = WebHttpJavascriptGenerator.GenerateJqueryJs(); | |
| Encoding encoding = Encoding.ASCII; | |
| WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain"; | |
| byte[] returnBytes = encoding.GetBytes(jsFileContents); | |
| return new MemoryStream(returnBytes); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment