/// <summary> /// Generates the ControlSpecific JavaScript. This script is safe to /// allow multiple callbacks to run simultaneously. /// </summary> private void GenerateControlSpecificJavaScript() { // Figure out the initial URL we're going to // Either it's the provided URL from the control or // we're posting back to the current page string Url = null; if (string.IsNullOrEmpty(ServerUrl)) { Url = Context.Request.Path; } else { Url = ResolveUrl(ServerUrl); } Uri ExistingUrl = Context.Request.Url; // Must fix up URL into fully qualified URL for XmlHttp if (!ServerUrl.ToLower().StartsWith("http")) { Url = ExistingUrl.Scheme + "://" + ExistingUrl.Authority + Url; } GenerateClassWrapperForCallbackMethods(); }
/// <summary> /// Create /// </summary> private void GenerateClassWrapperForWcfAndAsmx() { StringBuilder sb = new StringBuilder(); if (GenerateClientProxyClass == ProxyClassGenerationModes.jsdebug) { ClientScriptProxy.Current.RegisterClientScriptInclude(this, typeof(ControlResources), ServerUrl.ToLower() + "/jsdebug", ScriptRenderModes.Script); return; } string serverUrl = ServerUrl; if (!serverUrl.EndsWith("/")) { serverUrl += "/"; Type objectType = null; if (ClientProxyTargetType != null) { objectType = ClientProxyTargetType; } else { throw new InvalidOperationException(); } sb.Append("var " + ID + " = { "); MethodInfo[] Methods = objectType.GetMethods(BindingFlags.Instance | BindingFlags.Public); foreach (MethodInfo Method in Methods) { if ((ServiceType == AjaxMethodCallbackServiceTypes.Wcf && Method.GetCustomAttributes(typeof(OperationContractAttribute), false).Length > 0) || (ServiceType == AjaxMethodCallbackServiceTypes.Asmx && Method.GetCustomAttributes(typeof(WebMethodAttribute), false).Length > 0)) { sb.Append("\r\n " + Method.Name + ": function("); string ParameterList = ""; foreach (ParameterInfo Parm in Method.GetParameters()) { ParameterList += Parm.Name + ","; } sb.Append(ParameterList + "completed,errorHandler) "); sb.AppendFormat( @" {{ var _cb = new {0}_GetProxy(); _cb.invoke(""{1}"",{{", ID, Method.Name); string[] parms = ParameterList.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries); foreach (string parm in parms) { sb.Append(parm + ": " + parm + ","); } if (parms.Length > 0) { sb.Length--; // remove last comma } sb.Append(@"},completed,errorHandler); return _cb; },"); } } if (sb.Length > 0) { sb.Length--; // strip trailing , } // End of class sb.Append("\r\n}\r\n"); } string Url = null; if (string.IsNullOrEmpty(ServerUrl)) { Url = Context.Request.Path; } else { Url = ResolveUrl(ServerUrl); } sb.Append( "function " + ID + @"_GetProxy() { var _cb = new ServiceProxy('" + serverUrl + @"', { timeout: " + Timeout.ToString() + @", isWcf: " + (ServiceType == AjaxMethodCallbackServiceTypes.Wcf).ToString().ToLower() + @"}); return _cb; } "); ClientScriptProxy.RegisterStartupScript(this, GetType(), ID + "_ClientProxy", sb.ToString(), true); }
/// <summary> /// Creates the JavaScript client side object that matches the /// server side method signature. The JScript function maps /// to a CallMethod() call on the client. /// </summary> private void GenerateClassWrapperForCallbackMethods() { if (ServiceType != AjaxMethodCallbackServiceTypes.AjaxMethodCallback) { GenerateClassWrapperForWcfAndAsmx(); return; } StringBuilder sb = new StringBuilder(); if (GenerateClientProxyClass == ProxyClassGenerationModes.jsdebug) { ClientScriptProxy.Current.RegisterClientScriptInclude(this, typeof(ControlResources), ServerUrl.ToLower() + "/jsdebug", ScriptRenderModes.Script); return; } if (GenerateClientProxyClass == ProxyClassGenerationModes.Inline) { Type objectType = null; if (ClientProxyTargetType != null) { objectType = ClientProxyTargetType; } else if (TargetInstance != null) { objectType = TargetInstance.GetType(); } // assume Page as default else { objectType = Page.GetType(); } sb.Append("var " + ID + " = { "); MethodInfo[] Methods = objectType.GetMethods(BindingFlags.Instance | BindingFlags.Public); foreach (MethodInfo Method in Methods) { if (Method.GetCustomAttributes(typeof(CallbackMethodAttribute), false).Length > 0) { sb.Append("\r\n " + Method.Name + ": function " + "("); string ParameterList = ""; foreach (ParameterInfo Parm in Method.GetParameters()) { ParameterList += Parm.Name + ","; } sb.Append(ParameterList + "completed,errorHandler)"); sb.AppendFormat( @" {{ var _cb = {0}_GetProxy(); _cb.callMethod(""{1}"",[{2}],completed,errorHandler); return _cb; }},", ID, Method.Name, ParameterList.TrimEnd(',')); } } if (sb.Length > 0) { sb.Length--; // strip trailing , } // End of class sb.Append("\r\n}\r\n"); } string Url = null; if (string.IsNullOrEmpty(ServerUrl)) { Url = Context.Request.Path; } else { Url = ResolveUrl(ServerUrl); } sb.Append( "function " + ID + @"_GetProxy() { var _cb = new AjaxMethodCallback('" + ID + "','" + Url + @"', { timeout: " + Timeout.ToString() + @", postbackMode: '" + PostBackMode.ToString() + @"', formName: '" + PostBackFormName + @"' }); return _cb; } "); ClientScriptProxy.RegisterStartupScript(this, GetType(), ID + "_ClientProxy", sb.ToString(), true); }
public bool IsAgainstCloudant() { return(ServerUrl.ToLower().Contains("cloudant.com")); }
public void Sets_the_server_base_address() { Assert.Equal(ServerUrl.ToLower(), HttpClient.BaseAddress.ToString()); }