2011-09-23 18 views
0

我有以下幾種類型:屬性是用來

public enum MyEnum 
{ 
    Value1, 
    Value2 
} 

[DataContract] 
public class Configuration 
{ 
    [DataMember] 
    public MyEnum MyValue { get; set; } 
    [DataMember] 
    public Credentials CredentialValues { get; set; } 
} 

[DataContract, KnownType(typeof(CustomCredentials))] 
public class Credentials 
{ 

} 

[DataContract] 
public class CustomCredentials : Credentials 
{ 
    [DataMember] 
    public string Property1 { get; set; } 
    [DataMember] 
    public string Property2 { get; set; } 
} 

我的服務接口上,我有一個返回的Configuration與實例的功能其CredentialValues屬性設置爲CustomCredentials的完全填充實例。我沒有從客戶端或服務器收到錯誤消息,但是當數據在服務器上被序列化並由客戶端接收時,CustomCredentials上的屬性永遠不會有值。爲了讓這些屬性在客戶端正確反序列化,我需要在這裏更改什麼?

作爲參考,使用由客戶端和服務應用程序共享的數據/服務合同項目(該服務是自託管的),客戶端和服務器之間的連接是通過DuplexChannelFactoryNetTcpBinding上進行的,因此沒有服務代理類型可能需要重新生成。

回答

0

將此代碼與DataContracts一起添加到Contracts項目中。

[ServiceContract(Namespace = "http://schemas.platinumray.com/duplex", SessionMode = SessionMode.Required, CallbackContract = typeof(IService1Callback))] 
public interface IService1 
{ 
    [OperationContract(IsOneWay = true)] 
    void GetData(); 
} 

public interface IService1Callback 
{ 
    [OperationContract(IsOneWay = true)] 
    void SetData(Configuration config); 
} 

創建該服務。

public class Service1 : IService1 
{ 
    public void GetData() 
    { 
     var x = new Configuration() 
     { 
      MyValue = MyEnum.Value1, 
      CredentialValues = new CustomCredentials { Property1 = "Something", Property2 = "Something else" } 
     }; 
     OperationContext.Current.GetCallbackChannel<IService1Callback>().SetData(x); 
    } 
} 

class Program 
{ 
    static void Main(string[] args) 
    { 

     using (ServiceHost host = new ServiceHost(typeof(Service1), new Uri[] { new Uri("net.tcp://localhost:6789") })) 
     { 
      host.AddServiceEndpoint(typeof(IService1), new NetTcpBinding(), "Service1"); 
      host.Open(); 
      Console.ReadLine(); 
      host.Close(); 
     } 
    } 
} 

創建客戶端。

public class CallbackHandler : IService1Callback 
{ 
    public void SetData(Configuration config) 
    { 
     Console.WriteLine(config.CredentialValues.GetType().Name); 
     Console.WriteLine(((CustomCredentials)config.CredentialValues).Property1); 
     Console.WriteLine(((CustomCredentials)config.CredentialValues).Property2); 
    } 
} 

class Program 
{ 
    static void Main(string[] args) 
    { 
     // Setup the client 
     var callbacks = new CallbackHandler(); 
     var endpoint = new EndpointAddress(new Uri("net.tcp://localhost:6789/Service1")); 
     using (var factory = new DuplexChannelFactory<IService1>(callbacks, new NetTcpBinding(), endpoint)) 
     { 
      var client = factory.CreateChannel(); 
      client.GetData(); 
      Console.ReadLine(); 
      factory.Close(); 
     } 
    } 
} 

輸出以下預期:

CustomCredentials 
Something 
Something else 

所以實際上的工作沒有修改任何數據的合同......同樣的結果,如果我恢復到一個雙向操作,只是直接返回Configuration而不使用回調。

還嘗試製作憑據抽象,但無法複製您的問題。

我錯過了什麼嗎?

+0

我正在接受和獎勵賞金,因爲這原來是我與WCF完全無關的骨頭錯誤。謝謝你的幫助。 –

+0

謝謝Adam。在過去,我犯這樣的錯誤比我多,我懷疑這可能是事實。很高興它全部分類。問候,同事Bonehead。 –

相關問題