Codedom 생성 .AMSX 파일에 asp.net webservice 지시문을 추가하는 방법
-
18-09-2019 - |
문제
어떻게 추가 할 수 있습니까?
<%@ 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();
}
해결책
Codedom은 ASMX 파일 생성을 직접 지원하지 않습니다. C# 및 VB와 같은 언어 만 지원합니다.
웹 서비스 지시문을 수동으로 삽입해야합니다. 먼저 지침을 tw
작가를 Codedom 제공 업체에 전달하기 전에.
제휴하지 않습니다 StackOverflow