Beispiel #1
0
            private void LoadSMReferenceAssembly()
            {
                string         smReferenceArg           = _arguments.GetArgument(Options.Cmd.SMReference);
                IList <string> referencedAssembliesArgs = smReferenceArg.Split(';').ToList();

                if (referencedAssembliesArgs != null && referencedAssembliesArgs.Count > 0)
                {
                    string smassembly  = "";
                    string smpassembly = "";
                    foreach (string path in referencedAssembliesArgs)
                    {
                        var file = new FileInfo(path);

                        if (file.Name.Equals("System.ServiceModel.Primitives.dll", StringComparison.OrdinalIgnoreCase))
                        {
                            smassembly = path;
                        }
                        if (file.Name.Equals("System.Private.ServiceModel.dll", StringComparison.OrdinalIgnoreCase))
                        {
                            smpassembly = path;
                        }
                    }
                    if ((string.IsNullOrEmpty(smassembly)) || (string.IsNullOrEmpty(smpassembly)))
                    {
                        ToolConsole.WriteError("Missing one or both of the paths for System.ServiceModel.Primitives and System.Private.ServiceModel");
                        throw new ArgumentException("Invalid smreference value");
                    }

                    try
                    {
                        ToolConsole.WriteLine("Load Assembly From " + smpassembly);
                        InputModule.LoadAssembly(smpassembly);
                        ToolConsole.WriteLine($"Successfully Load {smpassembly}");
                    }
                    catch (Exception e)
                    {
                        ToolConsole.WriteError(string.Format("Fail to load the assembly {0} with the error {1}", smpassembly, e.Message));
                        throw;
                    }

                    try
                    {
                        ToolConsole.WriteLine("Load Assembly From " + smassembly);
                        Tool.SMAssembly = InputModule.LoadAssembly(smassembly);
                        ToolConsole.WriteLine($"Successfully Load {smassembly}");
                    }
                    catch (Exception e)
                    {
                        ToolConsole.WriteError(string.Format("Fail to load the assembly {0} with the error {1}", smassembly, e.Message));
                        throw;
                    }
                }
                else
                {
                    ToolConsole.WriteError("Need to pass the System.ServiceModel.Primitives.dll and the System.Private.ServiceModel.dll paths through the 'smreference' parameter.");
                    throw new ArgumentException("Need to pass the System.ServiceModel.Primitives.dll and the System.Private.ServiceModel.dll paths through the 'smreference' parameter.");
                }
            }
Beispiel #2
0
            private static bool IsContractType(Type type)
            {
                if (serviceContractAttributeType == null)
                {
                    string error = $"Type System.ServiceModel.ServiceContractAttribute not found type System.ServiceModel.ServiceContractAttribute in {Tool.SMAssembly.FullName}";
                    ToolConsole.WriteError(error);
                    throw new ToolRuntimeException(error);
                }

                return((type.IsInterface || type.IsClass) && (type.IsDefined(serviceContractAttributeType, false)));
            }
Beispiel #3
0
        internal static void Assert(bool condition, string message)
        {
            if (!condition)
            {
#if DEBUG
                ToolConsole.WriteError("Please file a bug or report the following issue with this tool:");
                StackTrace st = new StackTrace(true);
                ToolConsole.WriteLine(st.ToString());
#endif
                ToolConsole.WriteUnexpectedError(message);
                Tool.FailFast(message);
            }
        }
Beispiel #4
0
            private void LoadSMReferenceAssembly()
            {
                IList <string> referencedAssembliesArgs = _arguments.GetArguments(Options.Cmd.SMReference);

                if (referencedAssembliesArgs != null && referencedAssembliesArgs.Count > 0)
                {
                    string smassembly = referencedAssembliesArgs[0];
                    // Allow the reference to be for System.Private.ServiceModel as well.
                    if ((smassembly.LastIndexOf("System.ServiceModel.Primitives", StringComparison.OrdinalIgnoreCase) == -1) && (smassembly.LastIndexOf("System.Private.ServiceModel", StringComparison.OrdinalIgnoreCase) == -1))
                    {
                        ToolConsole.WriteError("Need to pass the right path of System.ServiceModel.Primitives for smreference parameter");
                        throw new ArgumentException("Invalid smreference value");
                    }

                    string smpassembly = smassembly.Replace("System.ServiceModel.Primitives", "System.Private.ServiceModel");
                    //for some lowercase path
                    smpassembly = smpassembly.Replace("system.servicemodel.primitives", "system.private.servicemodel");

                    int refplace = smassembly.LastIndexOf("ref");
                    if (refplace > 0)
                    {
                        smassembly  = smassembly.Remove(refplace, 3).Insert(refplace, "lib");
                        refplace    = smpassembly.LastIndexOf("ref");
                        smpassembly = smpassembly.Remove(refplace, 3).Insert(refplace, "lib");
                        int libplace = smpassembly.LastIndexOf("lib");
                        if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                        {
                            smpassembly = smpassembly.Remove(libplace, 3).Insert(libplace, @"runtimes\win\lib");
                        }
                        else
                        {
                            smpassembly = smpassembly.Remove(libplace, 3).Insert(libplace, @"runtimes/unix/lib");
                        }
                    }

                    try
                    {
                        ToolConsole.WriteLine("Load Assembly From " + smpassembly);
                        InputModule.LoadAssembly(smpassembly);
                        ToolConsole.WriteLine($"Successfully Load {smpassembly}");
                    }
                    catch (Exception e)
                    {
                        ToolConsole.WriteError(string.Format("Fail to load the assembly {0} with the error {1}", smpassembly, e.Message));
                        throw;
                    }

                    try
                    {
                        ToolConsole.WriteLine("Load Assembly From " + smassembly);
                        Tool.SMAssembly = InputModule.LoadAssembly(smassembly);
                        ToolConsole.WriteLine($"Successfully Load {smassembly}");
                    }
                    catch (Exception e)
                    {
                        ToolConsole.WriteError(string.Format("Fail to load the assembly {0} with the error {1}", smassembly, e.Message));
                        throw;
                    }
                }
                else
                {
                    ToolConsole.WriteError("Need pass the System.ServiceModel.Primitive.dll through SM parameter");
                    throw new ArgumentException("Need pass the System.ServiceModel.Primitive.dll through SM parameter");
                }
            }
Beispiel #5
0
        private List <Type> CollectXmlSerializerTypes(Assembly assembly, List <XmlMapping> mappings)
        {
            List <Type> types = new List <Type>();

            ExportModule.ContractLoader contractLoader = new ExportModule.ContractLoader(new Assembly[] { assembly }, _isTypeExcluded);
            contractLoader.ContractLoadErrorCallback = delegate(Type contractType, string errorMessage)
            {
                ToolConsole.WriteWarning(SR.Format(SR.WrnUnableToLoadContractForSGen, contractType, errorMessage));
            };

            Type contractDescriptionType = Tool.SMAssembly.GetType("System.ServiceModel.Description.ContractDescription");

            if (contractDescriptionType == null)
            {
                ToolConsole.WriteError($"Not found type System.ServiceModel.Description.ContractDescription in {Tool.SMAssembly.FullName}");
                throw new ToolRuntimeException();
            }

            PropertyInfo contractTypeProperty = contractDescriptionType.GetProperty("ContractType");

            if (contractTypeProperty == null)
            {
                ToolConsole.WriteError($"Not found property ContractType in type {contractDescriptionType}");
                throw new ToolRuntimeException();
            }

            Type xmlSerializerOperationBehaviorType = Tool.SMAssembly.GetType("System.ServiceModel.Description.XmlSerializerOperationBehavior");

            if (xmlSerializerOperationBehaviorType == null)
            {
                ToolConsole.WriteError($"Not found type System.ServiceModel.Description.XmlSerializerOperationBehaviorType in {Tool.SMAssembly.FullName}");
                throw new ToolRuntimeException();
            }

            Type operationType = Tool.SMAssembly.GetType("System.ServiceModel.Description.OperationDescription");

            if (operationType == null)
            {
                ToolConsole.WriteError($"Not found type System.ServiceModel.Description.OperationDescription in {Tool.SMAssembly.FullName}");
                throw new ToolRuntimeException();
            }

            PropertyInfo getBehaviorsProperty = operationType.GetProperty("Behaviors");

            if (getBehaviorsProperty == null)
            {
                ToolConsole.WriteError($"Not found method get_Behaviors in type {operationType}");
                throw new ToolRuntimeException();
            }

            Type keyedByTypeCollectionType = Tool.SMAssembly.GetType("System.Collections.Generic.KeyedByTypeCollection`1");

            if (keyedByTypeCollectionType == null)
            {
                ToolConsole.WriteError($"Not found type System.Collections.Generic.KeyedByTypeCollection`1 in {Tool.SMAssembly.FullName}");
                throw new ToolRuntimeException();
            }

            Type iOperationBehaviorType = Tool.SMAssembly.GetType("System.ServiceModel.Description.IOperationBehavior");

            if (iOperationBehaviorType == null)
            {
                ToolConsole.WriteError($"Not found type System.ServiceModel.Description.IOperationBehavior in {Tool.SMAssembly.FullName}");
                throw new ToolRuntimeException();
            }

            keyedByTypeCollectionType = keyedByTypeCollectionType.MakeGenericType(new Type[] { iOperationBehaviorType });
            if (keyedByTypeCollectionType == null)
            {
                ToolConsole.WriteError($"Cannot make Generic Type System.Collections.Generic.KeyedByTypeCollection<IOperationBehavior> in {Tool.SMAssembly.FullName}");
                throw new ToolRuntimeException();
            }

            MethodInfo findMethod = keyedByTypeCollectionType.GetMethod("Find");

            if (findMethod == null)
            {
                ToolConsole.WriteError($"Not found method find in type {keyedByTypeCollectionType}");
                throw new ToolRuntimeException();
            }

            findMethod = findMethod.MakeGenericMethod(new Type[] { xmlSerializerOperationBehaviorType });
            if (findMethod == null)
            {
                ToolConsole.WriteError($"Not found method Find<XmlSerializerOperationBehavior> in operation.Behaviors");
                throw new ToolRuntimeException();
            }

            MethodInfo getXmlMappingsMethod = xmlSerializerOperationBehaviorType.GetMethod("GetXmlMappings");

            if (getXmlMappingsMethod == null)
            {
                ToolConsole.WriteError($"Not found method getXmlMappings in XmlSerializerOperationBehavior");
                throw new ToolRuntimeException();
            }

            PropertyInfo operationsProperty = contractDescriptionType.GetProperty("Operations");

            if (operationsProperty == null)
            {
                ToolConsole.WriteError($"Not found property Operations in type {contractDescriptionType}");
                throw new ToolRuntimeException();
            }

            //Use reflection to replace the following code
            //foreach (ContractDescription contract in contractLoader.GetContracts())
            //{
            //    types.Add(contract.ContractType);
            //    foreach (OperationDescription operation in contract.Operations)
            //    {
            //        XmlSerializerOperationBehavior behavior = operation.Behaviors.Find<XmlSerializerOperationBehavior>();
            //        if (behavior != null)
            //        {
            //            foreach (XmlMapping map in behavior.GetXmlMappings())
            //            {
            //                mappings.Add(map);
            //            }
            //        }
            //    }
            //}

            foreach (object contract in contractLoader.GetContracts())
            {
                types.Add((Type)contractTypeProperty.GetValue(contract));

                System.Collections.IEnumerable operations = (System.Collections.IEnumerable)operationsProperty.GetValue(contract);
                foreach (var operation in operations)
                {
                    if (operation == null)
                    {
                        throw new ToolRuntimeException("operation is null");
                    }

                    var behaviors = getBehaviorsProperty.GetValue(operation);
                    var behavior  = findMethod.Invoke(behaviors, new object[] { });
                    if (behavior != null)
                    {
                        var xmlMappings = (Collection <XmlMapping>)getXmlMappingsMethod.Invoke(behavior, new object[] { });
                        if (xmlMappings != null)
                        {
                            foreach (XmlMapping map in xmlMappings)
                            {
                                mappings.Add(map);
                            }
                        }
                    }
                }
            }

            return(types);
        }