2013-05-20 5 views
1

아마도 더 많이 검색하거나 조사해야합니다. 그러나 너희들에게 처음으로 묻는 생각 .. 나는 Windows에서 호스팅되는 몇 가지 WCF 서비스를 가지고 있으며 클라이언트 측에서는이 모든 서비스 계약을 가진 프록시를 가지고있다. 내 응용 프로그램은 그것들을 소비하고 있으며, 단지 정상적으로 작동하고 있습니다. 이제는 내가 가진 서비스 끝점/다른 것을 제공하면 각 계약에서 작업 목록을 얻을 수있는 방법이 있는지 알고 싶었습니다.wcf 프록시의 작업 계약 목록

최종 파인트

http://localhost:8080/myservice/Echo 

프록시

[ServiceContract] 
public interface IEcho 
{ 
    string Message { [OperationContract]get; [OperationContract]set; } 
    [OperationContract] 
    string SendEcho(); 
} 

나는이 경우 목록 opeartions에서 ... 나에게 서비스 계약 내 작업의 목록을 얻을 것이다 방법이 필요합니다 = SendEcho(); 이 점은 어떻게 알 수 있습니까?

답변

1

나는 샘플 코드를 작성,하지만 당신은 당신이 방법의 목록을 얻으려면 서비스와 같은 어셈블리의 에코 서비스를 만들 필요 :이 도움이

public System.Collections.Generic.List<string> SendEcho() 
    { 
     // Get the current executing assembly and return all exported types included in it: 
     Type[] exportedTypes = System.Reflection.Assembly.GetExecutingAssembly().GetExportedTypes(); 

     // The list to store the method names 
     System.Collections.Generic.List<string> methodsList = new System.Collections.Generic.List<string>(); 

     foreach (Type item in exportedTypes) 
     { 
      // Check the interfaces implemented in the current class: 
      foreach (Type implementedInterfaces in item.GetInterfaces()) 
      { 
       object[] customInterfaceAttributes = implementedInterfaces.GetCustomAttributes(false); 
       if (customInterfaceAttributes.Length > 0) 
       { 
        foreach (object customAttribute in customInterfaceAttributes) 
        { 
         // Extract the method names only if the current implemented interface is marked with "ServiceContract" 
         if (customAttribute.GetType() == typeof(System.ServiceModel.ServiceContractAttribute)) 
         { 
          System.Reflection.MemberInfo[] mi = implementedInterfaces.GetMembers(); 

          foreach (System.Reflection.MemberInfo member in mi) 
          { 
           if (System.Reflection.MemberTypes.Method == member.MemberType) 
           { 
            // If you want to have an idea about the method parameters you can get it from here: (count, type etc...) 
            System.Reflection.ParameterInfo[] pi = ((System.Reflection.MethodInfo)member).GetParameters(); 

            // Check the method attributes and make sure that it is marked as "OperationContract": 
            object[] methodAttributes = member.GetCustomAttributes(false); 
            if (methodAttributes.Length > 0 && methodAttributes.Any(p => p.GetType() == typeof(System.ServiceModel.OperationContractAttribute))) 
             methodsList.Add(member.Name); 
           } 
          } 
         } 

        } 
       } 
      } 
     } 

     return methodsList; 
    } 

희망을!

0

아마도 클라이언트가 인터페이스/서비스 계약을 참조하고 있습니까? 그렇다면 서비스를 조사 할 필요가 없습니다. 리플렉션을 사용하여 인터페이스의 메소드를 반복하면서 [OperationContract] 속성으로 표시된 객체를 확인하기 만하면됩니다. 다음과 같이