2013-05-20 73 views
1

我可能需要搜索或調查更多。但想到問你們先.. 我有幾個WCF服務託管在Windows和客戶端我有所有這些服務合同的代理。我的應用程序正在消耗它們,而且它正在正常工作。現在我想知道,如果我給出服務終點/我擁有的其他東西,是否有任何方法可以從每個合同中獲得操作列表。來自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]屬性。