private static void InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary <string, object> rawParams) { // Initialize HttpCachePolicy InitializeCachePolicy(methodData, context); // Create an new instance of the class object target = null; if (!methodData.IsStatic) { target = Activator.CreateInstance(methodData.Owner.TypeData.Type); } // Make the actual method call on it object retVal = methodData.CallMethodFromRawParams(target, rawParams); string contentType; string responseString = null; if (methodData.UseXmlResponse) { responseString = retVal as string; // If it's a string, output it as is unless XmlSerializeString is set if (responseString == null || methodData.XmlSerializeString) { // Use the Xml Serializer try { responseString = ServicesUtilities.XmlSerializeObjectToString(retVal); } catch (Exception e) { // Throw a better error if Xml serialization fails throw new InvalidOperationException( String.Format(CultureInfo.CurrentCulture, AtlasWeb.WebService_InvalidXmlReturnType, methodData.MethodName, retVal.GetType().FullName, e.Message)); } } contentType = "text/xml"; } else { // Convert the result to a JSON string // DevDiv 88409:Change JSON wire format to prevent CSRF attack // We wrap the returned value inside an object , and assign the returned value // to member "d" of the object. We do so as JSOM for object will never be parsed // as valid Javascript , unlike arrays. responseString = @"{""d"":" + methodData.Owner.Serializer.Serialize(retVal) + "}"; contentType = "application/json"; } // Set the response content-type context.Response.ContentType = contentType; // Write the string to the response if (responseString != null) { context.Response.Write(responseString); } }
private void AppendClientTypeDeclaration(string ns, string typeName, bool genClass, bool ensureNS) { // Register the namespace if any // e.g. registerNamespace('MyNS.MySubNS'); string name = GetClientTypeNamespace(ServicesUtilities.GetClientTypeName(typeName)); if (!String.IsNullOrEmpty(ns)) { if (ensureNS) { EnsureNamespace(ns); } } else if (!genClass) { // If there is no namespace, we need a var to declare the variable if (!name.Contains(".")) { // if name contains '.', an object graph was already ensured and we dont need 'var'. _builder.Append("var "); } } _builder.Append(name); if (genClass) { _builder.Append(".prototype"); } _builder.Append('='); _ensuredObjectParts[name] = null; }
/* e.g * var Qqq = function() { this.__type = "Qqq"; } */ private void GenerateClientTypeProxies(WebServiceData data) { bool first = true; foreach (WebServiceTypeData t in data.ClientTypes) { if (first) { _builder.Append("var gtc = Sys.Net.WebServiceProxy._generateTypedConstructor;\r\n"); first = false; } string typeID = data.GetTypeStringRepresentation(t); string typeNameWithClientNamespace = GetClientTypeNamespace(t.TypeName); string typeName = ServicesUtilities.GetClientTypeName(typeNameWithClientNamespace); string clientTypeNamespace = GetClientTypeNamespace(t.TypeNamespace); EnsureNamespace(t.TypeNamespace); EnsureObjectGraph(clientTypeNamespace, typeName); _builder.Append("if (typeof(").Append(typeName).Append(") === 'undefined') {\r\n"); AppendClientTypeDeclaration(clientTypeNamespace, typeNameWithClientNamespace, false, false); // Need to use the _type id, which isn't necessarly the real name _builder.Append("gtc(\""); _builder.Append(typeID); _builder.Append("\");\r\n"); _builder.Append(typeName).Append(".registerClass('").Append(typeName).Append("');\r\n}\r\n"); } }
// Create client stubs for all the enums private void GenerateEnumTypeProxies(IEnumerable <WebServiceEnumData> enumTypes) { foreach (WebServiceEnumData t in enumTypes) { EnsureNamespace(t.TypeNamespace); string typeNameWithClientNamespace = GetClientTypeNamespace(t.TypeName); string typeName = ServicesUtilities.GetClientTypeName(typeNameWithClientNamespace); string[] enumNames = t.Names; long[] enumValues = t.Values; Debug.Assert(enumNames.Length == enumValues.Length); EnsureObjectGraph(GetClientTypeNamespace(t.TypeNamespace), typeName); _builder.Append("if (typeof(").Append(typeName).Append(") === 'undefined') {\r\n"); if (typeName.IndexOf('.') == -1) { _builder.Append("var "); } _builder.Append(typeName).Append(" = function() { throw Error.invalidOperation(); }\r\n"); _builder.Append(typeName).Append(".prototype = {"); for (int i = 0; i < enumNames.Length; i++) { if (i > 0) { _builder.Append(','); } _builder.Append(enumNames[i]); _builder.Append(": "); if (t.IsULong) { _builder.Append((ulong)enumValues[i]); } else { _builder.Append(enumValues[i]); } } _builder.Append("}\r\n"); _builder.Append(typeName).Append(".registerEnum('").Append(typeName).Append('\''); _builder.Append(", true);\r\n}\r\n"); } }
private void BuildArgsDictionary(WebServiceMethodData methodData, StringBuilder args, StringBuilder argsDict, StringBuilder docComments) { argsDict.Append('{'); foreach (WebServiceParameterData paramData in methodData.ParameterDatas) { string name = paramData.ParameterName; if (docComments != null) { // looks like: /// <param name="foo" type="ClientType">Namespace.ServerType</param> // client type may not match server type for built in js types like date, number, etc. // client type may be omitted for type Object. docComments.Append("/// <param name=\"").Append(name).Append("\""); Type serverType = ServicesUtilities.UnwrapNullableType(paramData.ParameterType); string clientType = GetClientTypeNamespace(ServicesUtilities.GetClientTypeFromServerType(methodData.Owner, serverType)); if (!String.IsNullOrEmpty(clientType)) { docComments.Append(" type=\"").Append(clientType).Append("\""); } docComments.Append(">").Append(serverType.FullName).Append("</param>\r\n"); } if (args.Length > 0) { args.Append(','); argsDict.Append(','); } args.Append(name); argsDict.Append(name).Append(':').Append(name); } if (docComments != null) { // append the built-in comments that all methods have (success, failed, usercontext parameters) docComments.Append(DebugXmlComments); } argsDict.Append("}"); if (args.Length > 0) { args.Append(','); } args.Append("succeededCallback, failedCallback, userContext"); }
// Normally returns MyNS.MySubNS.MyWebService OR var MyWebService, PageMethods will return PageMethods protected virtual string GetProxyTypeName(WebServiceData data) { return(ServicesUtilities.GetClientTypeName(data.TypeData.TypeName)); }