WCF and Castle library (Glass Mapper)



你好,我在Sitecore 6下有一个带有webHttpEndpointBehavior的WCF服务,我使用Glass Mapper来读取项目,Glass Mapper依赖于Castle库。

它运行得很好,但我对Contract有一些方法,比如:

        [OperationContract]
        [WebInvoke(Method = "POST",BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        List<Shade> GetAllShades(int columns, int rows);

我尝试以JSON格式返回类的列表,在使用SvcTraceViewer.exe调试我的服务时,我发现了下一个错误:

尝试序列化参数时出错:获取FamilyShadesResult。InnerException消息为"Type具有数据协定名称的"Castle.Proxies.ShadeProxy"'着色代理:http://schemas.datacontract.org/2004/07/Castle.Proxies'是不应为。考虑使用DataContractResolver或添加任何类型对于已知类型列表来说不是静态已知的-例如,通过使用KnownTypeAttribute属性或将其添加到列表中传递给DataContractSerializer的已知类型的。'。请参阅InnerException获取更多详细信息。

如何解决这个问题?Castle.Proxies.ShadeProxy是Castle下的一个动态类,我不能使用KnownTypeAttribute

事实上,如果我JSON.net库并以字符串形式返回结果,一切都会很好。

我假设您有一个链接到通过Glass加载的其他类的类,例如

[SitecoreClass]
public class Shade{
    [SitecoreField]
    public virtual IEnumerable<AnotherClass> SomeField{get;set;}
    [SitecoreChildren]
    public virtual IEnumerable<AnotherClass> Children{get;set;}      
}
[SitecoreClass]
public class AnotherClass{}

为了允许类的延迟加载,Glass使用Castle生成的代理,因此在运行时,当类加载到SomeField属性中时,您实际上会得到AnotherClass类的一个子类。

为了解决这个问题,您必须明确地告诉类将其他类作为具体类型加载,更新SitecoreField属性应该可以解决这个问题:

[SitecoreClass]
public class Shade{
    [SitecoreField(Setting=SitecoreFieldSettings.DontLazyLoad)]
    public virtual IEnumerable<AnotherClass> SomeField{get;set;}
    [SitecoreChildren(IsLazy=false)]
    public virtual IEnumerable<AnotherClass> Children{get;set;}
}
[SitecoreClass]
public class AnotherClass{}

最新更新