1

どうすれば追加できますか

<%@ 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();
}
4

1 に答える 1

1

CodeDom は、ASMX ファイルの生成を直接サポートしていません。C# や VB などの言語のみをサポートしています。

WebService ディレクティブを手動で挿入する必要があります。twCodeDom プロバイダーに渡す前に、最初にディレクティブをライターに書き込むことで、これを実行できる場合があります。

于 2010-01-04T18:50:14.797 に答える