2011-03-04 65 views
4

我想知道當我們使用Silverlight 4客戶端時,是否可以按this answer中描述的方式配置已知類型?WCF + Silverlight 4:如何通過Web.config配置已知類型?

似乎工作的東西是使用KnownType屬性,我們想避免該解決方案,因爲類在不同的程序集中,我們不希望它們之間存在依賴關係。

以下是配置的一個示例:

<?xml version="1.0"?> 
<configuration> 
    <!-- *snip* --> 
    <system.runtime.serialization> 
    <dataContractSerializer> 
     <declaredTypes> 
     <add type="Foo.Dto.FooDto, Foo.Dto, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null, processorArchitecture=MSIL"> 
      <knownType type="Foo.Dto.BarDto, Foo.Dto, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null, processorArchitecture=MSIL" /> 
      <knownType type="Foo.Dto.BizDto, Foo.Dto, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null, processorArchitecture=MSIL" /> 
     </add> 
     </declaredTypes> 
    </dataContractSerializer> 
    </system.runtime.serialization> 
    <!-- *snip* --> 
</configuration> 

回答

1

時退房MSDN docs的話題 - 基本上,這就是你需要:

<configuration> 
    <system.runtime.serialization> 
     <dataContractSerializer> 
      <declaredTypes> 
      <add type="MyCompany.Library.Shape, MyAssembly, Version=2.0.0.0, Culture=neutral, PublicKeyToken=XXXXXX, processorArchitecture=MSIL"> 
       <knownType type="MyCompany.Library.Circle, MyAssembly, Version=2.0.0.0, Culture=neutral, PublicKeyToken=XXXXXX, processorArchitecture=MSIL"/> 
      </add> 
      </declaredTypes> 
     </dataContractSerializer> 
    </system.runtime.serialization> 
</configuration> 
+0

我可能做錯了什麼,但我只是無法獲取配置文件中指定的已知類型。以這種方式配置它們總是會導致Silverlight結尾出錯。 – chlb 2011-03-08 08:30:01

+0

@chlb:你可以向我們展示**你的配置文件嗎?請通過編輯更新您的原始問題......謝謝! – 2011-03-08 14:30:22

+0

我編輯了我的問題以包含配置示例。 – chlb 2011-03-09 13:03:14