예제 #1
0
 private static bool CanBeDuplicated(XmlSchema existingSchema, XmlSchema schema)
 {
     if (XmlSchemas.IsDataSet(existingSchema) && XmlSchemas.IsDataSet(schema) &&
         existingSchema.Id == schema.Id)
     {
         return(true);
     }
     return(false);
 }
예제 #2
0
        /// <include file='doc\ServiceDescriptionReflector.uex' path='docs/doc[@for="ServiceDescriptionReflector.Reflect"]/*' />
        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        public void Reflect(Type type, string url)
        {
            serviceType = type;
            serviceUrl  = url;

            serviceAttr = WebServiceReflector.GetAttribute(type);

            methods = WebMethodReflector.GetMethods(type);
            CheckForDuplicateMethods(methods);

            descriptionsWithPost = descriptions;
            schemasWithPost      = schemas;

            if (reflectorsWithPost != null)
            {
                ReflectInternal(reflectorsWithPost);

                descriptions = new ServiceDescriptionCollection();
                schemas      = new XmlSchemas();
            }

            ReflectInternal(reflectors);

            if (serviceAttr.Description != null && serviceAttr.Description.Length > 0)
            {
                ServiceDescription.Documentation = serviceAttr.Description;
            }

            // need to preprocess all exported schemas to make sure that IXmlSerializable schemas are Merged and the resulting set is valid
            ServiceDescription.Types.Schemas.Compile(null, false);

            if (ServiceDescriptions.Count > 1)
            {
                // if defining interfaces, we move all schemas to the external collection
                // since the types therein may be referenced from any of the sdls
                Schemas.Add(ServiceDescription.Types.Schemas);
                ServiceDescription.Types.Schemas.Clear();
            }
            else if (ServiceDescription.Types.Schemas.Count > 0)
            {
                XmlSchema[] descriptionSchemas = new XmlSchema[ServiceDescription.Types.Schemas.Count];
                ServiceDescription.Types.Schemas.CopyTo(descriptionSchemas, 0);
                foreach (XmlSchema schema in descriptionSchemas)
                {
                    // we always move dataset schemas to the external schema's collection.
                    if (XmlSchemas.IsDataSet(schema))
                    {
                        ServiceDescription.Types.Schemas.Remove(schema);
                        Schemas.Add(schema);
                    }
                }
            }
        }
예제 #3
0
 public void Reflect(Type type, string url)
 {
     this.serviceType = type;
     this.serviceUrl  = url;
     this.serviceAttr = WebServiceReflector.GetAttribute(type);
     this.methods     = WebMethodReflector.GetMethods(type);
     this.CheckForDuplicateMethods(this.methods);
     this.descriptionsWithPost = this.descriptions;
     this.schemasWithPost      = this.schemas;
     if (this.reflectorsWithPost != null)
     {
         this.ReflectInternal(this.reflectorsWithPost);
         this.descriptions = new ServiceDescriptionCollection();
         this.schemas      = new XmlSchemas();
     }
     this.ReflectInternal(this.reflectors);
     if ((this.serviceAttr.Description != null) && (this.serviceAttr.Description.Length > 0))
     {
         this.ServiceDescription.Documentation = this.serviceAttr.Description;
     }
     this.ServiceDescription.Types.Schemas.Compile(null, false);
     if (this.ServiceDescriptions.Count > 1)
     {
         this.Schemas.Add(this.ServiceDescription.Types.Schemas);
         this.ServiceDescription.Types.Schemas.Clear();
     }
     else if (this.ServiceDescription.Types.Schemas.Count > 0)
     {
         XmlSchema[] array = new XmlSchema[this.ServiceDescription.Types.Schemas.Count];
         this.ServiceDescription.Types.Schemas.CopyTo(array, 0);
         foreach (XmlSchema schema in array)
         {
             if (XmlSchemas.IsDataSet(schema))
             {
                 this.ServiceDescription.Types.Schemas.Remove(schema);
                 this.Schemas.Add(schema);
             }
         }
     }
 }
예제 #4
0
        /// <include file='doc\ServiceDescriptionReflector.uex' path='docs/doc[@for="ServiceDescriptionReflector.Reflect"]/*' />
        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        public void Reflect(Type type, string url)
        {
            serviceType = type;
            serviceUrl  = url;

            serviceAttr = WebServiceReflector.GetAttribute(type);

            methods = WebMethodReflector.GetMethods(type);
            CheckForDuplicateMethods(methods);

            descriptionsWithPost = descriptions;
            schemasWithPost      = schemas;

            if (reflectorsWithPost != null)
            {
                ReflectInternal(reflectorsWithPost);

                descriptions = new ServiceDescriptionCollection();
                schemas      = new XmlSchemas();
            }

            ReflectInternal(reflectors);

            XmlSchema[] descriptionSchemas = new XmlSchema[ServiceDescription.Types.Schemas.Count];
            ServiceDescription.Types.Schemas.CopyTo(descriptionSchemas, 0);
            // if defining interfaces, we move all schemas to the external collection
            // since the types therein may be referenced from any of the sdls
            bool externalizeSchemas = ServiceDescriptions.Count > 1;

            foreach (XmlSchema schema in descriptionSchemas)
            {
                // we always move dataset schemas to the external schema's collection.
                if (externalizeSchemas || XmlSchemas.IsDataSet(schema))
                {
                    ServiceDescription.Types.Schemas.Remove(schema);
                    Schemas.Add(schema);
                }
            }
        }
예제 #5
0
        private void GenerateCode(ServiceDescriptionCollection sources, XmlSchemas schemas, string uriToWSDL,
                                  ICodeGenerator codeGen, string fileExtension)
        {
            proxyCode = " <ERROR> ";
            StringWriter w = null;

            compileUnit = new CodeCompileUnit();
            var importer = new ServiceDescriptionImporter();

            importer.Schemas.Add(schemas);
            foreach (ServiceDescription description in sources)
            {
                importer.AddServiceDescription(description, "", "");
            }
            importer.Style = ServiceDescriptionImportStyle.Client;
            Protocol protocol = WsdlProperties.Protocol;

            importer.ProtocolName = WsdlProperties.Protocol.ToString();
            var namespace2 = new CodeNamespace(proxyNamespace);

            compileUnit.Namespaces.Add(namespace2);
            ServiceDescriptionImportWarnings warnings = importer.Import(namespace2, compileUnit);

            try
            {
                try
                {
                    w = new StringWriter();
                }
                catch
                {
                    throw;
                }
                MemoryStream stream = null;
                if (schemas.Count > 0)
                {
                    compileUnit.ReferencedAssemblies.Add("System.Data.dll");
                    foreach (XmlSchema schema in schemas)
                    {
                        string targetNamespace = null;
                        try
                        {
                            targetNamespace = schema.TargetNamespace;
                            if (XmlSchemas.IsDataSet(schema))
                            {
                                if (stream == null)
                                {
                                    stream = new MemoryStream();
                                }
                                stream.Position = 0L;
                                stream.SetLength(0L);
                                schema.Write(stream);
                                stream.Position = 0L;
                                var dataSet = new DataSet();
                                dataSet.ReadXmlSchema(stream);
                                TypedDataSetGenerator.Generate(dataSet, namespace2, codeGen);
                            }
                        }
                        catch
                        {
                            throw;
                        }
                    }
                }
                try
                {
                    GenerateVersionComment(compileUnit.Namespaces[0]);
                    ChangeBaseType(compileUnit);
                    codeGen.GenerateCodeFromCompileUnit(compileUnit, w, null);
                }
                catch (Exception exception)
                {
                    if (w != null)
                    {
                        w.Write("Exception in generating code");
                        w.Write(exception.Message);
                    }
                    throw new InvalidOperationException("Error generating ", exception);
                }
            }
            finally
            {
                proxyCode = w.ToString();
                if (w != null)
                {
                    w.Close();
                }
            }
        }
예제 #6
0
        ///// <summary>
        ///// Clears the cache.
        ///// </summary>
        ///// <param name="wsdlLocation">WSDL location.</param>
        //public static void ClearCache(string wsdlLocation)
        //{
        //    CompiledAssemblyCache.ClearCache(wsdlLocation);
        //}

        ///// <summary>
        ///// Clear all cached DLLs.
        ///// </summary>
        //public static void ClearAllCached()
        //{
        //    CompiledAssemblyCache.ClearAllCached();
        //}

        /// <summary>
        /// Builds the assembly from WSDL.
        /// </summary>
        /// <param name="absoluteWsdlLocation">Absolute path to wsdl file.</param>
        /// /// <param name="wsdlContent">Actual content of wsdl file</param>
        /// <returns>Assembly containg proxy for service defined in <paramref name="absoluteWsdlLocation"/></returns>
        private Assembly BuildAssemblyFromWsdl(string absoluteWsdlLocation, string wsdlContent)
        {
            // Use an XmlTextReader to get the Web Service description
            StringReader  wsdlStringReader = new StringReader(wsdlContent);
            XmlTextReader tr = new XmlTextReader(wsdlStringReader);

            ServiceDescription.Read(tr);
            tr.Close();

            // WSDL service description importer
            CodeNamespace cns = new CodeNamespace(CodeConstants.CODENAMESPACE);

            sdi = new ServiceDescriptionImporter();
            //sdi.AddServiceDescription(sd, null, null);

            // check for optional imports in the root WSDL
            CheckForImports(absoluteWsdlLocation);

            sdi.ProtocolName = protocolName;
            sdi.Import(cns, null);

            // change the base class
            // get all available Service classes - not only the default one
            ArrayList newCtr = new ArrayList();

            foreach (CodeTypeDeclaration ctDecl in cns.Types)
            {
                if (ctDecl.BaseTypes.Count > 0)
                {
                    if (ctDecl.BaseTypes[0].BaseType == CodeConstants.DEFAULTBASETYPE)
                    {
                        newCtr.Add(ctDecl);
                    }
                }
            }

            foreach (CodeTypeDeclaration ctDecl in newCtr)
            {
                cns.Types.Remove(ctDecl);
                ctDecl.BaseTypes[0] = new CodeTypeReference(CodeConstants.CUSTOMBASETYPE);
                cns.Types.Add(ctDecl);
            }

            // source code generation
            CSharpCodeProvider cscp             = new CSharpCodeProvider();
            StringBuilder      srcStringBuilder = new StringBuilder();
            StringWriter       sw = new StringWriter(srcStringBuilder, CultureInfo.CurrentCulture);

            if (schemas != null)
            {
                foreach (XmlSchema xsd in schemas)
                {
                    if (XmlSchemas.IsDataSet(xsd))
                    {
                        MemoryStream mem = new MemoryStream();
                        mem.Position = 0;
                        xsd.Write(mem);
                        mem.Position = 0;
                        DataSet dataSet1 = new DataSet();
                        dataSet1.Locale = CultureInfo.InvariantCulture;
                        dataSet1.ReadXmlSchema(mem);
                        SDD.TypedDataSetGenerator.Generate(dataSet1, cns, cscp);
                    }
                }
            }

            cscp.GenerateCodeFromNamespace(cns, sw, null);
            proxySource = srcStringBuilder.ToString();
            sw.Close();

            // assembly compilation
            string location = "";

            if (HttpContext.Current != null)
            {
                location  = HttpContext.Current.Server.MapPath(".");
                location += @"\bin\";
            }

            CompilerParameters cp = new CompilerParameters();

            cp.ReferencedAssemblies.Add("System.dll");
            cp.ReferencedAssemblies.Add("System.Xml.dll");
            cp.ReferencedAssemblies.Add("System.Web.Services.dll");
            cp.ReferencedAssemblies.Add("System.Data.dll");
            cp.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly().Location);

            cp.GenerateExecutable      = false;
            cp.GenerateInMemory        = false;
            cp.IncludeDebugInformation = false;
            cp.TempFiles = new TempFileCollection(CompiledAssemblyCache.GetLibTempPath());

            CompilerResults cr = cscp.CompileAssemblyFromSource(cp, proxySource);

            if (cr.Errors.Count > 0)
            {
                throw new DynamicCompilationException(string.Format(CultureInfo.CurrentCulture, @"Building dynamic assembly failed: {0} errors", cr.Errors.Count));
            }

            Assembly compiledAssembly = cr.CompiledAssembly;

            return(compiledAssembly);
        }
예제 #7
0
        private void GenerateCode(ServiceDescriptionCollection sources, XmlSchemas schemas, string uriToWSDL,
                                  ICodeGenerator codeGen, string fileExtension)
        {
            proxyCode = " <ERROR> ";
            StringWriter writer1 = null;

            compileUnit = new CodeCompileUnit();
            ServiceDescriptionImporter importer1 = new ServiceDescriptionImporter();

            importer1.Schemas.Add(schemas);
            foreach (ServiceDescription description1 in sources)
            {
                importer1.AddServiceDescription(description1, "", "");
            }
            importer1.Style = ServiceDescriptionImportStyle.Client;
            Protocol protocol1 = WsdlProperties.Protocol;

            importer1.ProtocolName = WsdlProperties.Protocol.ToString();
            CodeNamespace namespace1 = new CodeNamespace(proxyNamespace);

            compileUnit.Namespaces.Add(namespace1);
            ServiceDescriptionImportWarnings warnings1 = importer1.Import(namespace1, compileUnit);

            try
            {
                try
                {
                    writer1 = new StringWriter();
                }
                catch
                {
                    throw;
                }
                MemoryStream stream1 = null;
                if (schemas.Count > 0)
                {
                    compileUnit.ReferencedAssemblies.Add("System.Data.dll");
                    foreach (XmlSchema schema1 in schemas)
                    {
                        string text1 = null;
                        try
                        {
                            text1 = schema1.TargetNamespace;
                            if (XmlSchemas.IsDataSet(schema1))
                            {
                                if (stream1 == null)
                                {
                                    stream1 = new MemoryStream();
                                }
                                stream1.Position = 0;
                                stream1.SetLength((long)0);
                                schema1.Write(stream1);
                                stream1.Position = 0;
                                DataSet set1 = new DataSet();
                                set1.ReadXmlSchema(stream1);
                                TypedDataSetGenerator.Generate(set1, namespace1, codeGen);
                            }
                            continue;
                        }
                        catch
                        {
                            throw;
                        }
                    }
                }
                try
                {
                    GenerateVersionComment(compileUnit.Namespaces[0]);
                    ChangeBaseType(compileUnit);
                    codeGen.GenerateCodeFromCompileUnit(compileUnit, writer1, null);
                }
                catch (Exception exception1)
                {
                    if (writer1 != null)
                    {
                        writer1.Write("Exception in generating code");
                        writer1.Write(exception1.Message);
                    }
                    throw new InvalidOperationException("Error generating ", exception1);
                }
            }
            finally
            {
                proxyCode = writer1.ToString();
                if (writer1 != null)
                {
                    writer1.Close();
                }
            }
        }