{"id":2217,"date":"2022-08-30T15:23:01","date_gmt":"2022-08-30T15:23:01","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/01\/05\/signalr-hub-invoke-doesnt-work-when-returning-a-castle-dynamicproxy-object-without-a-default-constructor-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:23:01","modified_gmt":"2022-08-30T15:23:01","slug":"signalr-hub-invoke-doesnt-work-when-returning-a-castle-dynamicproxy-object-without-a-default-constructor-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/signalr-hub-invoke-doesnt-work-when-returning-a-castle-dynamicproxy-object-without-a-default-constructor-collection-of-common-programming-errors\/","title":{"rendered":"SignalR hub invoke doesn&#39;t work when returning a Castle DynamicProxy object without a default constructor-Collection of common programming errors"},"content":{"rendered":"<p>I have some problems invoking a signalr hub when the returned object is a Castle DynamicProxy.<\/p>\n<p>Let&#8217;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):<\/p>\n<pre><code>public Article Read()\n{\n    var article = new Article(0);\n    return article;\n}\npublic class Article\n{\n    public Article(int id)\n    {\n        Id = id;\n    }\n\n    public int Id { get; set; }\n}\n<\/code><\/pre>\n<p>The above method correctly returns my object. If I change this code to:<\/p>\n<pre><code>public Article Read()\n{\n    var proxyGenerator = new Castle.DynamicProxy.ProxyGenerator();\n    var entity = proxyGenerator.CreateClassProxy(typeof(Article), new object[]{0}, new TestInterceptor()) as Article; ;\n    return entity;\n}\nclass TestInterceptor : Castle.DynamicProxy.IInterceptor\n{\n    public void Intercept(Castle.DynamicProxy.IInvocation invocation)\n    {\n    }\n}\n<\/code><\/pre>\n<p>The object is never returned. The client (javascript) doesn&#8217;t receive any error and neither <code>done<\/code> or <code>fail<\/code> function are executed.<\/p>\n<p>I suspect it is a problem with the serialization. If I try to serialize the object using Newtonsoft I get the following error:<\/p>\n<pre><code>System.ArgumentNullException was unhandled by user code\n  HResult=-2147467261\n  Message=Value cannot be null.\nParameter name: key\n  Source=mscorlib\n  ParamName=key\n  StackTrace:\n       at System.Collections.ObjectModel.KeyedCollection`2.Contains(TKey key)\n       at Newtonsoft.Json.Serialization.JsonPropertyCollection.AddProperty(JsonProperty property)\n       at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateConstructorParameters(ConstructorInfo constructor, JsonPropertyCollection memberProperties)\n       at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateObjectContract(Type objectType)\n       at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateContract(Type objectType)\n       at Newtonsoft.Json.Serialization.DefaultContractResolver.ResolveContract(Type type)\n       at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.GetContractSafe(Object value)\n       at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)\n       at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)\n       at Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value, Type objectType)\n       at Newtonsoft.Json.JsonConvert.SerializeObject(Object value, Type type, Formatting formatting, JsonSerializerSettings settings)\n       at Newtonsoft.Json.JsonConvert.SerializeObject(Object value, Formatting formatting, JsonSerializerSettings settings)\n       at Newtonsoft.Json.JsonConvert.SerializeObject(Object value)\n  InnerException: \n<\/code><\/pre>\n<p>Any ideas? It is a serialization problem of signalr?<\/p>\n<p>EDIT:<\/p>\n<p>Thanks to Anders I have found that the problem occurs only when the class for which you have created the dynamic proxy doesn&#8217;t have a default constructor. For example <code>public Article(int id)<\/code>. It is a problem with Json.NET serialization?<\/p>\n<ol>\n<li>\n<p>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.<\/p>\n<\/li>\n<li>\n<p>Json.Net has no problem serialize a Dynamic proxy, this worked for me<\/p>\n<pre><code>class Program\n{\n    static void Main(string[] args)\n    {\n        var proxyGenerator = new Castle.DynamicProxy.ProxyGenerator();\n        var entity = proxyGenerator.CreateClassProxy(typeof(Article), new object[0], new TestInterceptor()) as Article; ;\n        var json = JsonConvert.SerializeObject(entity);\n    }\n}\n\npublic class Article\n{\n    public int Id { get; set; }\n}\n\nclass TestInterceptor : Castle.DynamicProxy.IInterceptor\n{\n    public void Intercept(Castle.DynamicProxy.IInvocation invocation)\n    {\n    }\n}   \n<\/code><\/pre>\n<p>Result was<\/p>\n<pre><code>{\"__interceptors\":[{}],\"Id\":0}\n<\/code><\/pre>\n<p>update:<\/p>\n<p>Without parameterless constructor I get an expection allready in Castle<\/p>\n<pre><code>Can not instantiate proxy of class: ConsoleApplication1.Article.\n\nCould not find a parameterless constructor.\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2014-01-05 09:04:05. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I have some problems invoking a signalr hub when the returned object is a Castle DynamicProxy. Let&#8217;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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2217","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2217","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2217"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2217\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2217"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2217"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2217"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}