2011-08-06 144 views

回答

3

這應該只是工作(見下面的例子)。你得到的完全例外是什麼?

public class StackOverflow_6966835 
{ 
    [DataContract()] 
    public class MyClass 
    { 
     [DataMember()] 
     public Dictionary<string, List<string>> dictionary = new Dictionary<string, List<string>>(); 
    } 
    [ServiceContract] 
    public interface ITest 
    { 
     [OperationContract] 
     MyClass GetMyClass(); 
    } 
    public class Service : ITest 
    { 
     public MyClass GetMyClass() 
     { 
      return new MyClass 
      { 
       dictionary = new Dictionary<string, List<string>> 
       { 
        { "one", "uno un eins".Split().ToList() }, 
        { "two", "dos deux zwei".Split().ToList() }, 
        { "three", "tres trois drei".Split().ToList() } 
       } 
      }; 
     } 
    } 
    public static void Test() 
    { 
     Console.WriteLine("Stand-alone serialization"); 
     MemoryStream ms = new MemoryStream(); 
     MyClass c = new Service().GetMyClass(); 
     DataContractSerializer dcs = new DataContractSerializer(typeof(MyClass)); 
     dcs.WriteObject(ms, c); 
     Console.WriteLine("Serialized: {0}", Encoding.UTF8.GetString(ms.ToArray())); 

     Console.WriteLine(); 
     Console.WriteLine("Now using in a service"); 

     string baseAddress = "http://" + Environment.MachineName + ":8000/Service"; 
     ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress)); 
     host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), ""); 
     host.Open(); 
     Console.WriteLine("Host opened"); 

     ChannelFactory<ITest> factory = new ChannelFactory<ITest>(new BasicHttpBinding(), new EndpointAddress(baseAddress)); 
     ITest proxy = factory.CreateChannel(); 

     Console.WriteLine(proxy.GetMyClass()); 

     ((IClientChannel)proxy).Close(); 
     factory.Close(); 

     Console.Write("Press ENTER to close the host"); 
     Console.ReadLine(); 
     host.Close(); 
    } 
} 
+0

錯誤類似於「讀取管道時出錯:無法識別的異常109(0x6d)」(被翻譯成英文)。順便說一句,我使用NetNamedPipeBinding。 – Peladao

+0

由於某種原因,服務器可能會關閉該通道。嘗試在客戶端和服務器上啓用跟蹤,跟蹤應包​​含原因。 – carlosfigueira

+0

我發現了這個問題。正如你所說,它應該工作得很好,而且我的代碼示例本身確實沒有什麼錯。然而,這個例子顯然被簡化了,問題出現在真實的代碼中。 (我偶然嘗試通過未標記[DataContract]或KnownType的線路發送繼承類型)。感謝您的保證,它應該已經奏效! – Peladao

相關問題