SignalR hub invoke doesn't work when returning a Castle DynamicProxy object without a default constructor

StackOverflow https://stackoverflow.com/questions/18948827

Question

I have some problems invoking a signalr hub when the returned object is a Castle DynamicProxy.

Let's say that I have the following server code on the signalr hub (this is not the real code but just to show the problem):

public Article Read()
{
    var article = new Article(0);
    return article;
}
public class Article
{
    public Article(int id)
    {
        Id = id;
    }

    public int Id { get; set; }
}

The above method correctly returns my object. If I change this code to:

public Article Read()
{
    var proxyGenerator = new Castle.DynamicProxy.ProxyGenerator();
    var entity = proxyGenerator.CreateClassProxy(typeof(Article), new object[]{0}, new TestInterceptor()) as Article; ;
    return entity;
}
class TestInterceptor : Castle.DynamicProxy.IInterceptor
{
    public void Intercept(Castle.DynamicProxy.IInvocation invocation)
    {
    }
}

The object is never returned. The client (javascript) doesn't receive any error and neither done or fail function are executed.

I suspect it is a problem with the serialization. If I try to serialize the object using Newtonsoft I get the following error:

System.ArgumentNullException was unhandled by user code
  HResult=-2147467261
  Message=Value cannot be null.
Parameter name: key
  Source=mscorlib
  ParamName=key
  StackTrace:
       at System.Collections.ObjectModel.KeyedCollection`2.Contains(TKey key)
       at Newtonsoft.Json.Serialization.JsonPropertyCollection.AddProperty(JsonProperty property)
       at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateConstructorParameters(ConstructorInfo constructor, JsonPropertyCollection memberProperties)
       at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateObjectContract(Type objectType)
       at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateContract(Type objectType)
       at Newtonsoft.Json.Serialization.DefaultContractResolver.ResolveContract(Type type)
       at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.GetContractSafe(Object value)
       at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
       at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)
       at Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
       at Newtonsoft.Json.JsonConvert.SerializeObject(Object value, Type type, Formatting formatting, JsonSerializerSettings settings)
       at Newtonsoft.Json.JsonConvert.SerializeObject(Object value, Formatting formatting, JsonSerializerSettings settings)
       at Newtonsoft.Json.JsonConvert.SerializeObject(Object value)
  InnerException: 

Any ideas? It is a serialization problem of signalr?

EDIT:

Thanks to Anders I have found that the problem occurs only when the class for which you have created the dynamic proxy doesn't have a default constructor. For example public Article(int id). It is a problem with Json.NET serialization?

Was it helpful?

Solution 2

After some more investigation I have found the problem. It is a little bug on Json.NET. I have created a new issue on json.net website that now has been resolved.

OTHER TIPS

Json.Net has no problem serialize a Dynamic proxy, this worked for me

class Program
{
    static void Main(string[] args)
    {
        var proxyGenerator = new Castle.DynamicProxy.ProxyGenerator();
        var entity = proxyGenerator.CreateClassProxy(typeof(Article), new object[0], new TestInterceptor()) as Article; ;
        var json = JsonConvert.SerializeObject(entity);
    }
}

public class Article
{
    public int Id { get; set; }
}

class TestInterceptor : Castle.DynamicProxy.IInterceptor
{
    public void Intercept(Castle.DynamicProxy.IInvocation invocation)
    {
    }
}   

Result was

{"__interceptors":[{}],"Id":0}

update:

Without parameterless constructor I get an expection allready in Castle

Can not instantiate proxy of class: ConsoleApplication1.Article.

Could not find a parameterless constructor.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top