简体   繁体   English

在没有默认构造函数的情况下返回Castle DynamicProxy对象时,SignalR hub调用不起作用

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

I have some problems invoking a signalr hub when the returned object is a Castle DynamicProxy . 当返回的对象是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. 客户端(javascript)没有收到任何错误,也没有执行donefail功能。

I suspect it is a problem with the serialization. 我怀疑这是序列化的一个问题。 If I try to serialize the object using Newtonsoft I get the following error: 如果我尝试使用Newtonsoft序列化对象,我会收到以下错误:

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. 感谢Anders我发现仅当您为其创建动态代理的类没有默认构造函数时才会出现问题。 For example public Article(int id) . 例如public Article(int id) It is a problem with Json.NET serialization? 这是Json.NET序列化的一个问题?

Json.Net has no problem serialize a Dynamic proxy, this worked for me Json.Net对序列化动态代理没有问题,这对我有用

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 如果没有无参数构造函数,我会在Castle中获得一个预期

Can not instantiate proxy of class: ConsoleApplication1.Article.

Could not find a parameterless constructor.

After some more investigation I have found the problem. 经过一番调查,我发现了问题。 It is a little bug on Json.NET. 这是Json.NET上的一个小错误。 I have created a new issue on json.net website that now has been resolved. 我在json.net网站上创建了一个新问题 ,现在已经解决了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM