Question

hello I have WCF service with webHttpEndpointBehavior under Sitecore 6 and I use Glass Mapper to read items, Glass mapper has depends on Castle library.

It is working well, but i have some methods with Contract like:

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

where I try return list of my class in JSON format, during debug my service with SvcTraceViewer.exe I find out next error:

There was an error while trying to serialize parameter :GetFamilyShadesResult. The InnerException message was 'Type 'Castle.Proxies.ShadeProxy' with data contract name 'ShadeProxy:http://schemas.datacontract.org/2004/07/Castle.Proxies' is not expected. Consider using a DataContractResolver or 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.'. Please see InnerException for more details.

How to resole this issue? Castle.Proxies.ShadeProxy is a dynamic class under Castle and I can't use KnownTypeAttribute.

Actually if I JSON.net library and return results as string everything works fine.

Was it helpful?

Solution

I assume you have a class that links to other classes loaded via Glass, e.g.

[SitecoreClass]
public class Shade{

    [SitecoreField]
    public virtual IEnumerable<AnotherClass> SomeField{get;set;}

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

To allow lazy loading of classes Glass uses proxies generated by Castle, so at runtime when the classes are loaded into the SomeField property you actually get a sub-class of the AnotherClass class.

To resolve this you have to explicitly tell class to load the other classes as concrete types, updating the SitecoreField attribute should fix this problem:

[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{}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top