在reference.cs中不会自动生成KnownType

时间:2016-07-05 09:23:59

标签: c# .net wcf service-reference known-types

我的wcf webservice中定义了两种类型。 </head> <body> <div class="container"> <div class="well well-sm list-grid-toggle"> <strong>Category Title</strong> <div class="btn-group"> <a href="#" id="list" class="btn btn-default btn-sm"><span class="glyphicon glyphicon-th-list"> </span>List</a> <a href="#" id="grid" class="btn btn-default btn-sm"><span class="glyphicon glyphicon-th"></span>Grid</a> </div> </div> <div id="products" class="row list-group"> <div class="item col-xs-6 col-lg-4"> <div class="thumbnail comany-detail"> <img class="group list-group-image" src="http://placehold.it/400x250/000/fff" alt="" /> <div class="caption"> <h4 class="group inner list-group-item-heading"> Company Name</h4> <p class="group inner list-group-item-text"> Product description... Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p> <div class="row"> <div class="col-xs-12 col-md-6"> <p class="lead"> $$$$</p> </div> <div class="col-xs-12 col-md-6 company-link-btn"> <a class="btn btn-success company-link" href="company-profile.html">View Menu</a> </div> </div> </div> </div> </div> <div class="item col-xs-6 col-lg-4"> <div class="thumbnail company-detail"> <img class="group list-group-image" src="http://placehold.it/400x250/000/fff" alt="" /> <div class="caption"> <h4 class="group inner list-group-item-heading"> Company Name</h4> <p class="group inner list-group-item-text"> Product description... Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p> <div class="row"> <div class="col-xs-12 col-md-6"> <p class="lead"> $$$$</p> </div> <div class="col-xs-12 col-md-6 company-link-btn"> <a class="btn btn-success company-link" href="company-profile.html">View Menu</a> </div> </div> </div> </div> </div> <div class="item col-xs-6 col-lg-4"> <div class="thumbnail company-detail"> <img class="group list-group-image" src="http://placehold.it/400x250/000/fff" alt="" /> <div class="caption"> <h4 class="group inner list-group-item-heading"> Company Name</h4> <p class="group inner list-group-item-text"> Product description... Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p> <div class="row"> <div class="col-xs-12 col-md-6"> <p class="lead"> $$$$</p> </div> <div class="col-xs-12 col-md-6 company-link-btn"> <a class="btn btn-success company-link" href="company-profile.html">View Menu</a> </div> </div> </div> </div> </div> <div class="item col-xs-6 col-lg-4"> <div class="thumbnail restaurant-detail"> <img class="group list-group-image" src="http://placehold.it/400x250/000/fff" alt="" /> <div class="caption"> <h4 class="group inner list-group-item-heading"> Restaurant Name</h4> <p class="group inner list-group-item-text"> Product description... Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p> <div class="row"> <div class="col-xs-12 col-md-6"> <p class="lead"> $$$$</p> </div> <div class="col-xs-12 col-md-6 restaurant-link-btn"> <a class="btn btn-success restaurant-link" href="restaurant-profile.html">View Menu</a> </div> </div> </div> </div> </div> <div class="item col-xs-6 col-lg-4"> <div class="thumbnail restaurant-detail"> <img class="group list-group-image" src="http://placehold.it/400x250/000/fff" alt="" /> <div class="caption"> <h4 class="group inner list-group-item-heading"> Company Name</h4> <p class="group inner list-group-item-text"> Product description... Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p> <div class="row"> <div class="col-xs-12 col-md-6"> <p class="lead"> $$$$</p> </div> <div class="col-xs-12 col-md-6 company-link-btn"> <a class="btn btn-success company-link" href="restaurant-profile.html">View product</a> </div> </div> </div> </div> </div> <div class="item col-xs-6 col-lg-4"> <div class="thumbnail company-detail"> <img class="group list-group-image" src="http://placehold.it/400x250/000/fff" alt="" /> <div class="caption"> <h4 class="group inner list-group-item-heading"> Company Name</h4> <p class="group inner list-group-item-text"> Product description... Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p> <div class="row"> <div class="col-xs-12 col-md-6"> <p class="lead"> $$$$</p> </div> <div class="col-xs-12 col-md-6 company-link-btn"> <a class="btn btn-success company-link" href="company-profile.html">View product</a> </div> </div> </div> </div> </div> </div> <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script> <script src="js/search-result.js"></script> </body> </html>DynamicFields

ClassA

当我在客户端添加服务作为服务引用时,我得到了[DataContract] [KnownType(typeof(ClassA[]))] public class DynamicFields { [DataMember] public Dictionary<string, object> properties = new Dictionary<string, object>(); public object this[string name] { get { if (properties.ContainsKey(name)) { return properties[name]; } return null; } set { properties[name] = value; } } } [DataContract] public class ClassA { [DataMember] public int Id { get; set; } ... }

类型

的例外情况
  

&#39; System.ServiceModel.Dispatcher.NetDispatcherFaultException&#39;发生了   在mscorlib.dll中但未在用户代码中处理

     

其他信息:格式化程序在尝试反序列化消息时抛出异常:尝试时出错   反序列化参数http://tempuri.org/:GetResultsResult。该   InnerException消息在第1行位置3900处是“错误”。元素   &#39; http://schemas.microsoft.com/2003/10/Serialization/Arrays:Value&#39;   包含映射到名称的类型的数据   &#39; http://schemas.datacontract.org/2004/07/SomeNamespace:ArrayOfClassA&#39 ;.   反序列化器不知道映射到此名称的任何类型。   如果您正在使用,请考虑使用DataContractResolver   DataContractSerializer或添加对应的类型   &#39; ArrayOfClassA&#39;到已知类型列表 - 例如,通过使用   KnownTypeAttribute属性或通过将其添加到已知列表中   传递给序列化程序的类型。&#39;。有关更多信息,请参阅InnerException   的信息。

我查看了客户端自动生成的reference.cs文件,发现KnownType属性不存在

System.ServiceModel.Dispatcher.NetDispatcherFaultException

然后我在reference.cs中手动添加属性[System.Diagnostics.DebuggerStepThroughAttribute()] [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")] [System.Runtime.Serialization.DataContractAttribute(Name="DynamicFields", Namespace="http://schemas.datacontract.org/2004/07/SomeNamespace")] [System.SerializableAttribute()] public partial class DynamicFields : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged { ... } ,它可以正常工作

[KnownType(typeof(ClassA))]

这里的问题是每次更新服务引用时,我都需要转到reference.cs并手动添加knowntype属性。有没有办法让客户自动生成它?

0 个答案:

没有答案