[英]Data Contract Known Types and a set of interfaces inheriting each other
我開發了一個文件解析Web服務(重寫為WCF),該文件接受string[]
並返回ISection[]
但實際上這是一組嵌套接口:
namespace Project.Contracts // Project.Contracts.dll
{
public interface ISection { }
public interface ISummarySection : ISection { }
public interface IDataSection : ISection { }
}
和課程:
namespace Project.Format.A // Project.Format.A.dll
{
[DataContract]
public class SummarySectionFormatA : ISummarySection { }
[DataContract]
public class DataSectionFormatA : IDataSection { }
}
服務接口及其實現:
[ServiceContract]
public interface IService // Project.Contracts.dll
{
ISection[] Parse(string format, string[] data);
}
[ServiceKnownType(typeof(SummarySectionFormatA))] // tried this also
[ServiceKnownType(typeof(DataSectionFormatA))]
public class Service : IService // Project.Service.dll
{
public ISection[] Parse(string format, string[] data)
{
return Factory.Create(format).Parse(data);
}
}
我試圖在服務器和客戶端上都配置declaredTypes
:
<system.runtime.serialization>
<dataContractSerializer>
<declaredTypes>
<add type="Project.Contracts.ISumarySection, Project.Contracts">
<knownType type="Project.Format.A.SummarySectionFormatA, Project.Format.A" />
</add>
<add type="Project.Contracts.IDataSection, Project.Contracts">
<knownType type="Project.Format.A.DataSectionFormatA, Project.Format.A" />
</add>
</declaredTypes>
</dataContractSerializer>
</system.runtime.serialization>
但是仍然會出現相同的錯誤:
“不希望使用數據協定名稱為“ DataSection:http://schemas.example.com/Parse”的類型“ DataSectionFormatA”。
要么
基礎連接已關閉:連接意外關閉。
我不能用KnownTypeAttribute裝飾接口,因為Contracts項目沒有引用Format項目,而引用破壞了設計。 這就是為什么我要使用config。
看看下面的代碼
[ServiceContract]
[ServiceKnownType(typeof(SummarySectionFormatA))]
[ServiceKnownType(typeof(DataSectionFormatA))]
public interface IService {}
public class Service : IService {}
我相信您應該稍微改變實現方式...看看這個問題 ,看看是否有幫助。
嘗試使其工作:
[KnownType("GetKnownType")]
public class Section
{
static Type[] GetKnownType()
{
return new[]
{
Type.GetType("Project.Format.A.DataSectionFormatA, Project.Format.A")
};
}
}
但似乎服務器和客戶端都必須引用 Project.Format.A.dll才能使其正常工作(方法不返回null)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.