Exemple #1
0
        private void ProcessKnownTypes(Type t)
        {
            WebServiceTypeData typeData = WebServiceTypeData.GetWebServiceTypeData(t);
            bool alreadyProcessed       = false;

            if (typeData == null)
            {
                // indicates a type was used that is a built-in type
                return;
            }

            // if T implments IEnumerable or IDictionary, do not include type proxy for it
            // but still continue to get known types. I.e List<MyType> should ignore List<MyType>
            // but process MyType
            if (!(typeof(IEnumerable).IsAssignableFrom(t) || typeof(IDictionary).IsAssignableFrom(t)))
            {
                _clientTypeNameDictionary[t] = GetTypeStringRepresentation(typeData.TypeName);
                alreadyProcessed             = ProcessTypeData(typeData);
            }

            if (!alreadyProcessed)
            {
                IList <WebServiceTypeData> knownTypes = WebServiceTypeData.GetKnownTypes(t, typeData);
                foreach (WebServiceTypeData knownType in knownTypes)
                {
                    ProcessTypeData(knownType);
                }
            }
        }
Exemple #2
0
        internal string GetTypeStringRepresentation(WebServiceTypeData typeData)
        {
            //First check if typeData provides its string representaiton ( for WCF case)
            string typeString = typeData.StringRepresentation;

            if (typeString == null)
            {
                typeString = GetTypeStringRepresentation(typeData.TypeName, true);
            }
            return(typeString);
        }
Exemple #3
0
        private WebServiceData(WebServiceTypeData typeData)
        {
            _typeData   = typeData;
            _serializer = new JavaScriptSerializer(this);
#pragma warning disable 0436
            ScriptingJsonSerializationSection.ApplicationSettings settings = new ScriptingJsonSerializationSection.ApplicationSettings();
#pragma warning restore 0436
            _serializer.MaxJsonLength  = settings.MaxJsonLimit;
            _serializer.RecursionLimit = settings.RecursionLimit;
            _serializer.RegisterConverters(settings.Converters);
        }
Exemple #4
0
        public override Type ResolveType(string id)
        {
            WebServiceTypeData type = null;

            if (ClientTypeDictionary.TryGetValue(id, out type))
            {
                if (type != null)
                {
                    return(type.Type);
                }
            }
            return(null);
        }
Exemple #5
0
        internal void Initialize(WebServiceTypeData typeData, Dictionary <string, WebServiceMethodData> methods)
        {
            Dictionary <string, WebServiceTypeData> clientTypeDictionary = new Dictionary <string, WebServiceTypeData>();

            _clientTypesDictionary = clientTypeDictionary;
            Dictionary <string, WebServiceEnumData> enumTypeDictionary = new Dictionary <string, WebServiceEnumData>();

            _enumTypesDictionary      = enumTypeDictionary;
            _processedTypes           = new Hashtable();
            _clientTypesProcessed     = true;
            _clientTypeNameDictionary = new Dictionary <Type, string>();
            _typeData = typeData;
            _methods  = methods;
        }
        internal static List <WebServiceTypeData> GetKnownTypes(Type type, WebServiceTypeData typeData)
        {
            List <WebServiceTypeData> knownTypes = new List <WebServiceTypeData>();
            XsdDataContractExporter   exporter   = new XsdDataContractExporter();

            exporter.Export(type);
            ICollection schemas = exporter.Schemas.Schemas();

            foreach (XmlSchema schema in schemas)
            {
                // DataContractSerializer always exports built-in types into a fixed schema that can be ignored.
                if (schema.TargetNamespace == SerializationNamespace)
                {
                    continue;
                }

                foreach (XmlSchemaObject schemaObj in schema.Items)
                {
                    XmlSchemaType schemaType            = schemaObj as XmlSchemaType;
                    string        schemaTargetNamespace = XmlConvert.DecodeName(schema.TargetNamespace);
                    if (schemaType != null &&
                        !(schemaType.Name == typeData.TypeName && schemaTargetNamespace == typeData.TypeNamespace) &&
                        !String.IsNullOrEmpty(schemaType.Name))
                    {
                        WebServiceTypeData             knownTypeData = null;
                        XmlSchemaSimpleTypeRestriction simpleTypeRestriction;
                        if (CheckIfEnum(schemaType as XmlSchemaSimpleType, out simpleTypeRestriction))
                        {
                            knownTypeData = ImportEnum(XmlConvert.DecodeName(schemaType.Name), schemaTargetNamespace, schemaType.QualifiedName, simpleTypeRestriction, schemaType.Annotation);
                        }
                        else if (CheckIfCollection(schemaType as XmlSchemaComplexType))
                        {
                            continue;
                        }
                        else if (!(schemaType is XmlSchemaSimpleType))
                        {
                            knownTypeData = new WebServiceTypeData(XmlConvert.DecodeName(schemaType.Name), schemaTargetNamespace);
                        }
                        if (knownTypeData != null)
                        {
                            knownTypes.Add(knownTypeData);
                        }
                    }
                }
            }
            return(knownTypes);
        }
        //used for WCF known types
        internal static WebServiceTypeData GetWebServiceTypeData(Type type)
        {
            WebServiceTypeData      typeData = null;
            XsdDataContractExporter exporter = new XsdDataContractExporter();
            XmlQualifiedName        qname    = exporter.GetSchemaTypeName(type);

            if (!qname.IsEmpty)
            {
                if (type.IsEnum)
                {
                    bool isUlong = (Enum.GetUnderlyingType(type) == typeof(ulong));
                    typeData = new WebServiceEnumData(XmlConvert.DecodeName(qname.Name), XmlConvert.DecodeName(qname.Namespace), Enum.GetNames(type), Enum.GetValues(type), isUlong);
                }
                else
                {
                    typeData = new WebServiceTypeData(XmlConvert.DecodeName(qname.Name), XmlConvert.DecodeName(qname.Namespace));
                }
            }
            return(typeData);
        }
Exemple #8
0
        // returns true if typeData already exists in typeDictionary
        private bool ProcessTypeData(WebServiceTypeData typeData)
        {
            string typeString = GetTypeStringRepresentation(typeData.TypeName);
            bool   retval     = true;

            if (typeData is WebServiceEnumData)
            {
                if (!_enumTypesDictionary.ContainsKey(typeString))
                {
                    _enumTypesDictionary[typeString] = (WebServiceEnumData)typeData;
                    retval = false;
                }
            }
            else
            {
                if (!_clientTypesDictionary.ContainsKey(typeString))
                {
                    _clientTypesDictionary[typeString] = typeData;
                    retval = false;
                }
            }
            return(retval);
        }
Exemple #9
0
        internal void ProcessClientType(Type t, bool force, bool isWCF)
        {
            if (!force && _processedTypes.Contains(t))
            {
                return;
            }
            _processedTypes[t] = null;

            // Keep track of all enum Types
            if (t.IsEnum)
            {
                WebServiceEnumData enumData = null;
                if (isWCF)
                {
                    enumData = (WebServiceEnumData)WebServiceTypeData.GetWebServiceTypeData(t);
                }
                else
                {
                    enumData = new WebServiceEnumData(t.Name, t.Namespace, t, Enum.GetNames(t), Enum.GetValues(t), Enum.GetUnderlyingType(t) == typeof(ulong));
                }
                _enumTypesDictionary[GetTypeStringRepresentation(enumData.TypeName, false)] = enumData;
                return;
            }

            // For generics, we only allow generic types with one parameter, which we will try to process
            if (t.IsGenericType)
            {
                if (isWCF)
                {
                    ProcessKnownTypes(t);
                }
                else
                {
                    Type[] genericArgs = t.GetGenericArguments();
                    if (genericArgs.Length > 1)
                    {
                        return;
                    }
                    ProcessClientType(genericArgs[0], false, isWCF);
                }
            }
            // Support arrays explicitly
            else if (t.IsArray)
            {
                ProcessClientType(t.GetElementType(), false, isWCF);
            }
            else
            {
                // Ignore primitive types
                // Ignore DateTime, since we have special serialization handling for it in the JavaScriptSerializer
                // Ignore IDctionary and IEnumerables as well
                if (t.IsPrimitive || t == typeof(object) || t == typeof(string) || t == typeof(DateTime) ||
                    t == typeof(void) || t == typeof(System.Decimal) || t == typeof(Guid) ||
                    typeof(IEnumerable).IsAssignableFrom(t) || typeof(IDictionary).IsAssignableFrom(t) ||
                    (!isWCF && !ObjectConverter.IsClientInstantiatableType(t, _serializer)))
                {
                    return;
                }

                // Only add it to the list of client types if it can be instantiated.
                // pass false to skip the lock
                if (isWCF)
                {
                    ProcessKnownTypes(t);
                }
                else
                {
                    string typeStringRepresentation = GetTypeStringRepresentation(t.FullName, false);
                    _clientTypesDictionary[typeStringRepresentation] = new WebServiceTypeData(t.Name, t.Namespace, t);
                    _clientTypeNameDictionary[t] = typeStringRepresentation;
                }
            }
        }
 // Indigo entry point for creating WebServiceData
 internal WebServiceData(WebServiceTypeData typeData, Dictionary<string, WebServiceMethodData> methods)
     : this(typeData) {
     _methods = methods;
 }
        //used for WCF known types
        internal static WebServiceTypeData GetWebServiceTypeData(Type type) {

            WebServiceTypeData typeData = null;       
            XsdDataContractExporter exporter = new XsdDataContractExporter();
            XmlQualifiedName qname = exporter.GetSchemaTypeName(type);
            if (!qname.IsEmpty) {
                if (type.IsEnum) {
                    bool isUlong = (Enum.GetUnderlyingType(type) == typeof(ulong));
                    typeData = new WebServiceEnumData(XmlConvert.DecodeName(qname.Name), XmlConvert.DecodeName(qname.Namespace), Enum.GetNames(type), Enum.GetValues(type), isUlong);
                }
                else {
                    typeData = new WebServiceTypeData(XmlConvert.DecodeName(qname.Name), XmlConvert.DecodeName(qname.Namespace));
                }
            }
            return typeData;
        }
        internal static List<WebServiceTypeData> GetKnownTypes(Type type, WebServiceTypeData typeData) {
            List<WebServiceTypeData> knownTypes = new List<WebServiceTypeData>();
            XsdDataContractExporter exporter = new XsdDataContractExporter();
            exporter.Export(type);
            ICollection schemas = exporter.Schemas.Schemas();
            foreach (XmlSchema schema in schemas) {
                // DataContractSerializer always exports built-in types into a fixed schema that can be ignored.
                if (schema.TargetNamespace == SerializationNamespace) {
                    continue;
                }

                foreach (XmlSchemaObject schemaObj in schema.Items) {
                    XmlSchemaType schemaType = schemaObj as XmlSchemaType;
                    string schemaTargetNamespace = XmlConvert.DecodeName(schema.TargetNamespace);
                    if (schemaType != null
                        && !(schemaType.Name == typeData.TypeName && schemaTargetNamespace == typeData.TypeNamespace)
                        && !String.IsNullOrEmpty(schemaType.Name)) {
                        WebServiceTypeData knownTypeData = null;
                        XmlSchemaSimpleTypeRestriction simpleTypeRestriction;
                        if (CheckIfEnum(schemaType as XmlSchemaSimpleType, out simpleTypeRestriction)) {
                            knownTypeData = ImportEnum(XmlConvert.DecodeName(schemaType.Name), schemaTargetNamespace, schemaType.QualifiedName, simpleTypeRestriction, schemaType.Annotation);
                        }
                        else if (CheckIfCollection(schemaType as XmlSchemaComplexType)) {
                            continue;
                        }
                        else if (!(schemaType is XmlSchemaSimpleType)) {
                            knownTypeData = new WebServiceTypeData(XmlConvert.DecodeName(schemaType.Name), schemaTargetNamespace);
                        }
                        if (knownTypeData != null) {
                            knownTypes.Add(knownTypeData);
                        }
                    }
                }
            }
            return knownTypes;
        }
 internal string GetTypeStringRepresentation(WebServiceTypeData typeData) {
     //First check if typeData provides its string representaiton ( for WCF case)
     string typeString = typeData.StringRepresentation;
     if (typeString == null) {
         typeString = GetTypeStringRepresentation(typeData.TypeName, true);
     }
     return typeString;
 }
 // returns true if typeData already exists in typeDictionary
 private bool ProcessTypeData(WebServiceTypeData typeData) {
     string typeString = GetTypeStringRepresentation(typeData.TypeName);
     bool retval = true;
     if (typeData is WebServiceEnumData) {
         if (!_enumTypesDictionary.ContainsKey(typeString)) {
             _enumTypesDictionary[typeString] = (WebServiceEnumData)typeData;
             retval = false;
         }
     }
     else {
         if (!_clientTypesDictionary.ContainsKey(typeString)) {
             _clientTypesDictionary[typeString] = typeData;
             retval = false;
         }
     }
     return retval;
 }
        internal void ProcessClientType(Type t, bool force, bool isWCF)
        {
            if (!force && _processedTypes.Contains(t))
                return;
            _processedTypes[t] = null;

            // Keep track of all enum Types
            if (t.IsEnum) {
                WebServiceEnumData enumData = null;
                if (isWCF) {
                    enumData = (WebServiceEnumData)WebServiceTypeData.GetWebServiceTypeData(t);
                }
                else {
                    enumData = new WebServiceEnumData(t.Name, t.Namespace, t, Enum.GetNames(t), Enum.GetValues(t), Enum.GetUnderlyingType(t) == typeof(ulong));
                }
                _enumTypesDictionary[GetTypeStringRepresentation(enumData.TypeName, false)] = enumData;
                return;
            }

            // For generics, we only allow generic types with one parameter, which we will try to process
            if (t.IsGenericType) {
                if (isWCF) {
                     ProcessKnownTypes(t);
                }
                else {
                    Type[] genericArgs = t.GetGenericArguments();
                    if (genericArgs.Length > 1) {
                        return;
                    }
                    ProcessClientType(genericArgs[0], false, isWCF);
                }
            }
            // Support arrays explicitly
            else if (t.IsArray) {
                ProcessClientType(t.GetElementType(), false, isWCF);
            }
            else {
                // Ignore primitive types
                // Ignore DateTime, since we have special serialization handling for it in the JavaScriptSerializer
                // Ignore IDctionary and IEnumerables as well
                if (t.IsPrimitive || t == typeof(object) || t == typeof(string) || t == typeof(DateTime) ||
                    t == typeof(void) || t == typeof(System.Decimal) || t == typeof(Guid) ||
                    typeof(IEnumerable).IsAssignableFrom(t) || typeof(IDictionary).IsAssignableFrom(t) ||
                    (!isWCF && !ObjectConverter.IsClientInstantiatableType(t, _serializer)))
                    return;

                // Only add it to the list of client types if it can be instantiated.
                // pass false to skip the lock
                if (isWCF) {
                    ProcessKnownTypes(t);
                }
                else {
                    string typeStringRepresentation = GetTypeStringRepresentation(t.FullName, false);
                    _clientTypesDictionary[typeStringRepresentation] = new WebServiceTypeData(t.Name, t.Namespace, t);
                    _clientTypeNameDictionary[t] = typeStringRepresentation;

                }
            }
        }
 internal void Initialize(WebServiceTypeData typeData, Dictionary<string, WebServiceMethodData> methods) {
     Dictionary<string, WebServiceTypeData> clientTypeDictionary = new Dictionary<string, WebServiceTypeData>();
     _clientTypesDictionary = clientTypeDictionary;
     Dictionary<string, WebServiceEnumData> enumTypeDictionary = new Dictionary<string, WebServiceEnumData>();
     _enumTypesDictionary = enumTypeDictionary;
     _processedTypes = new Hashtable();
     _clientTypesProcessed = true;
     _clientTypeNameDictionary = new Dictionary<Type, string>();
     _typeData = typeData;
     _methods = methods;
 }
Exemple #17
0
 // Indigo entry point for creating WebServiceData
 internal WebServiceData(WebServiceTypeData typeData, Dictionary <string, WebServiceMethodData> methods)
     : this(typeData) {
     _methods = methods;
 }
        private WebServiceData(WebServiceTypeData typeData) {
            _typeData = typeData;
            _serializer = new JavaScriptSerializer(this);
#pragma warning disable 0436
            ScriptingJsonSerializationSection.ApplicationSettings settings = new ScriptingJsonSerializationSection.ApplicationSettings();
#pragma warning restore 0436
            _serializer.MaxJsonLength = settings.MaxJsonLimit;
            _serializer.RecursionLimit = settings.RecursionLimit;
            _serializer.RegisterConverters(settings.Converters);
        }