代码之家  ›  专栏  ›  技术社区  ›  Chris McCall

如何将ASP.NET WebService指令添加到codedom生成的.asmx文件中

  •  1
  • Chris McCall  · 技术社区  · 15 年前

    如何添加

    <%@ webservice class="MyNamespace.MyClass" ... %>
    

    到codedom生成的.asmx文件的顶部?

    下面是一些代码来补充这个问题:

           public void Generate<T>()
            {
                string[] importNameSpaces = new string[] { "System","CoreData","System.Web.Services", "System.Data", "System.Text", "System.Collections.Generic" };
                targetUnit = new CodeCompileUnit();
    
    
                CodeNamespace samples = new CodeNamespace(TargetNamespace);
    
    
                foreach (string space in importNameSpaces)
                {
                    samples.Imports.Add(new CodeNamespaceImport(space));
                }
    
                ClassName = typeof(T).Name;
    
                CodeSnippetStatement WebServiceDirective = new CodeSnippetStatement("<%@ WebService Language=\"C#\" CodeBehind=\"Plans.asmx.cs\" Class=\"" + TargetNamespace + "." + ClassName + "\" %>");
    
                targetClass = new CodeTypeDeclaration(ClassName);
                targetClass.IsClass = true;
                targetClass.TypeAttributes =
                    TypeAttributes.Public;
                targetClass.IsPartial = true;
    
                CodeAttributeDeclaration WebServiceAtt = new CodeAttributeDeclaration(
                    new CodeTypeReference(
                        typeof(WebServiceAttribute)), new CodeAttributeArgument[] 
                { new CodeAttributeArgument("Namespace",new CodeSnippetExpression(@"http://tempuri.org"))});
                targetClass.CustomAttributes.Add(WebServiceAtt);
    
                WebServiceAtt = new CodeAttributeDeclaration(
                    new CodeTypeReference(
                        typeof(WebServiceAttribute)), 
                        new CodeAttributeArgument[] 
                        { new CodeAttributeArgument("ConformsTo", new CodeTypeReferenceExpression(WsiProfiles.BasicProfile1_1.GetType())) });
    
                targetClass.CustomAttributes.Add(WebServiceAtt);
    
                foreach (OperationType o in Enum.GetValues(typeof(OperationType)))
                {
                    if (CoreData.Utility.SupportsOperation(typeof(T),o))
                    {
                        targetClass.Members.Add(createWebServiceMethod(typeof(T).Name,typeof(T).GetProperties(),o));
                    }
    
                }
    
    
                samples.Types.Add(targetClass);
                targetUnit.Namespaces.Add(samples);
                //targetUnit.StartDirectives.Add(WebServiceDirective);
    
                CSharpCodeProvider provider = new CSharpCodeProvider();
    
                IndentedTextWriter tw = new IndentedTextWriter(new StreamWriter(OutputDirectory + @"\" + targetClass.Name + ".asmx", false));
                provider.GenerateCodeFromCompileUnit(targetUnit, tw, new CodeGeneratorOptions());
                tw.Close();
    }
    
    1 回复  |  直到 15 年前
        1
  •  1
  •   Eilon    15 年前

    codedom不直接支持生成asmx文件。它只支持C和VB等语言。

    您必须手动插入WebService指令。您可以通过首先将指令写入 tw 在将其传递到codedom提供程序之前编写。