Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将ASP.net webservice指令添加到CodeDom生成的.asmx文件_Asp.net_Web Services_Codedom - Fatal编程技术网

如何将ASP.net webservice指令添加到CodeDom生成的.asmx文件

如何将ASP.net webservice指令添加到CodeDom生成的.asmx文件,asp.net,web-services,codedom,Asp.net,Web Services,Codedom,我怎样才能补充呢 <%@ webservice class="MyNamespace.MyClass" ... %> 到CodeDom生成的.asmx文件的顶部 下面是一些代码,可以更详细地填写这个问题: public void Generate<T>() { string[] importNameSpaces = new string[] { "System","CoreData","System.Web.Se

我怎样才能补充呢

<%@ 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();
}
public void Generate()
{
string[]importNameSpaces=新字符串[]{“System”、“CoreData”、“System.Web.Services”、“System.Data”、“System.Text”、“System.Collections.Generic”};
targetUnit=新的CodeCompileUnit();
CodeNamespace示例=新的CodeNamespace(TargetNamespace);
foreach(importNameSpaces中的字符串空间)
{
samples.Imports.Add(新的CodeNamespaceImport(空格));
}
ClassName=typeof(T).Name;
CodeSnippetStatement WebServiceDirective=新的CodeSnippetStatement(“”);
targetClass=新的代码类型声明(类名称);
targetClass.IsClass=true;
targetClass.TypeAttributes=
类型属性。公共;
targetClass.IsPartial=true;
CodeAttributeDeclaration WebServiceAtt=新的CodeAttributeDeclaration(
新代码类型引用(
typeof(WebServiceAttribute)),新的CodeAttributeArgument[]
{new CodeAttributeArgument(“名称空间”,新的代码片段表达式(@)”http://tempuri.org"))});
targetClass.CustomAttributes.Add(WebServiceAtt);
WebServiceAtt=新代码属性声明(
新代码类型引用(
类型(WebServiceAttribute)),
新的CodeAttributeArgument[]
{新的CodeAttributeArgument(“符合”,新的CodeTypeReferenceExpression(WsiProfiles.BasicProfile1_1.GetType())});
targetClass.CustomAttributes.Add(WebServiceAtt);
foreach(Enum.GetValues中的OperationType o(类型化(OperationType)))
{
if(CoreData.Utility.SupportsOperation(类型(T),o))
{
Add(createWebServiceMethod(typeof(T).Name,typeof(T).GetProperties(),o));
}
}
samples.Types.Add(targetClass);
targetUnit.Namespaces.Add(示例);
//targetUnit.StartDirectives.Add(WebServiceDirective);
CSharpCodeProvider provider=新的CSharpCodeProvider();
IndentedTextWriter tw=新的IndentedTextWriter(新的StreamWriter(OutputDirectory+@“\”+targetClass.Name+.asmx,false));
provider.GenerateCodeFromCompileUnit(targetUnit,tw,new CodeGeneratorOptions());
tw.Close();
}

CodeDom不直接支持生成ASMX文件。它只支持C#和VB等语言


您必须手动插入WebService指令。您可以先将指令写入
tw
writer,然后再将其传递给CodeDom提供程序来实现这一点。

CodeDom不直接支持生成ASMX文件。它只支持C#和VB等语言

您必须手动插入WebService指令。您可以先将指令写入
tw
writer,然后再将其传递给CodeDom提供程序,这样做