2013-03-19 26 views
1

我需要在C#中的生產實體中列出成員。MDS 2012 WCF產品實體列表成員

下面是代碼,但我不知道如何構建成員列表屬性,例如來自集合的名稱。代碼如下。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
// additional references... 
using HelloMDS.MDService; /* for the created service reference */ 
using System.Collections.ObjectModel; /* supports collection objects used in the proxy */ 

namespace HelloMDSG_Members 
{ 
    class Program 
    { 
     private static ServiceClient mdsProxy; /* service proxy object */ 

     static void Main(string[] args) 
     { 
      // Create the service proxy 
      Console.WriteLine("Connecting..."); 
      try 
      { 
       mdsProxy = CreateMdsProxy("http://localhost/MDS/service/Service.svc"); 
       Console.WriteLine("Connected."); 
      } 
      catch (Exception ex) 
      { 
       Console.WriteLine("Error connecting: " + ex.Message); 
      } 

      Console.WriteLine("Call GetMembers"); 
      GetMembers(); 

      Console.WriteLine("Finished"); 
      Console.ReadKey(); 

     } 

     public static void GetMembers() 
     { 


      //declare a new MDS ServiceClient 
      ServiceClient client = new ServiceClient(); 
      //EntityMembersGetRequest request = new EntityMembersGetRequest(); 

      //Build a request for Entity Members of the Product Entity 
      //in the Product Model, Version 4 from the MDS Sample package on the MS Connect site 
      EntityMembersGetRequest request = new EntityMembersGetRequest(); 
      request.MembersGetCriteria = new EntityMembersGetCriteria(); 
      request.MembersGetCriteria.ModelId = new Identifier() { Name = "Model 1" }; 
      request.MembersGetCriteria.VersionId = new Identifier() { Name = "VERSION_1" }; 
      request.MembersGetCriteria.EntityId = new Identifier() { Name = "Product" }; 

      request.MembersGetCriteria.MemberReturnOption = MemberReturnOption.DataAndCounts; //without this the request doesn't return memebers ! 

      Console.WriteLine("Start getting data"); 

      //submit the request to the MDS Web service Client 
      EntityMembersGetResponse response = client.EntityMembersGet(request); 

      Console.WriteLine("Count:{0}", response.EntityMembersInformation.MemberCount); 

      //confirm that members were returned 
      if (response.EntityMembersInformation.MemberCount > 0) 
      { 
       System.Collections.ObjectModel.Collection<Member> members = response.EntityMembers.Members; 

       // HELP HERE PLEASE 
       // write members list with the name attribute 
       Console.WriteLine() member - name attribute);    

      } 

      HandleErrors(response.OperationResult); 
     } 

     // creates the service client proxy 
     private static ServiceClient CreateMdsProxy(string mdsURL) 
     { 
      // create an endpoint address using the URL 
      System.ServiceModel.EndpointAddress endptAddress = new System.ServiceModel.EndpointAddress(mdsURL); 

      // create and configure the WS Http binding 
      System.ServiceModel.WSHttpBinding wsBinding = new System.ServiceModel.WSHttpBinding(); 

      // create and return the client proxy 
      return new ServiceClient(wsBinding, endptAddress); 
     } 

     // Handles the operations results 
     private static void HandleErrors(OperationResult result) 
     { 
      string errorMessage = string.Empty; 
      if (result.Errors.Count() != 0) 
      { 
       for (int i = 0; i <= result.Errors.Count() - 1; i++) 
       { 
        errorMessage += " OperationResult:Error: " + result.Errors[i].Code + ":" 
         + result.Errors[i].Description + ":" + result.Errors[i].Context.Type.ToString(); 
       } 
       Console.WriteLine("Error: " + errorMessage); 
      } 
     } 
    } 
} 
+0

「!request.MembersGetCriteria.MemberReturnOption = MemberReturnOption.DataAndCounts; //沒有這個要求不返回承包商,客人」。這個評論爲我和一個無盡的嘗試和錯誤的團隊成員救了我。謝謝。 – 2016-09-16 20:42:58

回答

0

你是否在尋找:

foreach(var member in members) 
{ 
    Console.WriteLine(member.MemberId.Id); 
    foreach(var attribute in member.Attributes) 
    { 
     Console.WriteLine("Attribute: {0},\tValue: {1}", attribute.Identifier.Name, attribute.Value); 
    } 
} 
+0

謝謝,這有助於:)我將它簡化爲:foreach(var member in members) Console.WriteLine(「Code:{0} Name:{1}」,member.MemberId.Code,member.MemberId.Name ); } – bluerunx 2013-03-19 22:41:24

+0

@publicgk,你可以看看我的問題嗎?因爲我相信它是類似的:http://stackoverflow.com/questions/37548589/mds-wcf-web-service – w0051977 2016-05-31 14:35:53