2009-08-20 121 views
1

我有以下代碼:WCF問題序列化消息頭

public virtual void Initialise() 
{ 
    this.AddHeader("SystemContext", this.UserSettings.SystemContext); 
} 

public virtual void AddHeader(string key, object value) 
{ 
    var customHeader = MessageHeader.CreateHeader(key, this.SystemSettings.SystemServiceNamespace, value); 
    OperationContext.Current.OutgoingMessageHeaders.Add(customHeader); 
} 

當我嘗試,我已經運行上面的代碼後,再執行服務器,我收到以下錯誤:

Type 'ACSIS.Core.Common.Configuration.UserAcountDetials' with data contract name 'UserAcountDetials: http://schemas.datacontract.org/2004/07/ACSIS.Core.Common.Configuration ' is not expected. Add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.

現在UserSettings描述爲SystemContext,其類型爲IDictionary。我知道WCF不可能用神奇的方式神奇地創造出任何東西,所以我需要幫助它一點點。

是否有某種方法可以將對象的運行時類型傳遞到線上,並將其轉換回另一端的類型。我沒有使用WCF for .net SOA的東西或類似的東西,我知道具體類型將在電線的另一端。

如果WCF不真的支持這個(但必須有一些方法),有沒有一種方法可以將數據序列化爲二進制格式,將二進制文件附加到頭文件中並使用類型信息自己處理序列化我穿過。

+0

爲什麼使用MessageHeader.CreateHeader而不是消息合約? – 2009-08-20 23:22:30

+0

我會很樂意嘗試,因爲你有建議,你有任何參考我可以看看? – 2009-08-20 23:49:02

回答

1

你可以嘗試應用knownType屬性爲您的服務合同,是這樣的:

[ServiceKnownType(typeof(ACSIS.Core.Common.Configuration.UserAcountDetials))] 
[ServiceContract] 
public interface IMyService 
{... 

是的,雖然它是那麼優雅,您可以使用BinaryFormatter的序列化你的頭對象爲一個byte []數組,將其粘貼在標題中,然後在另一側反序列化。二進制序列化在版本控制方面非常棘手,所以你必須小心一點。