WCF服务(如果在DataContract对象图中具有接口)导致reference.cs中的数据类型为Object [英] WCF Services if have Interface in DataContract object graph results in data type Object in reference.cs

查看:54
本文介绍了WCF服务(如果在DataContract对象图中具有接口)导致reference.cs中的数据类型为Object的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VS 2010,.NET 4.

I am working on VS 2010, .NET 4.

我遇到了一个问题,其中WCF服务引用在对象"对象中生成了结果.作为除实际具体(DTO_Vehicle)类型类对象之外的数据类型.

I am running into an issue where WCF service reference generated results in "Object" as datatype other than actual concrete(DTO_Vehicle) type class object.

下面是一些代码段. (我很想将解决方案作为压缩内容附加,但不知道该怎么做)

Below is some code snippet. (I would love to attach solution as zipped content but dont know how to do that )

DTO_Dealer由Sevice发送,该服务具有与其关联的车辆列表.我正在使用unknownType属性在reference.cs中获取DTO_Vehicle的定义,因为WCF仅招待具体的类型对象,而不招待接口.

The DTO_Dealer is sent out by Sevice which has list of Vehicles associated with it. I am using knownType attribute to get the definition of DTO_Vehicle out in reference.cs as WCF only entertains concrete type objects and not interfaces. 

我遇到的问题是DTO_Dealer reference.cs上的签名.用于列表< IVehicle> StockVehicles,我的假设是我将获得"DTO_Vehicle [] StockVehicles".

The issue I face is the signature on DTO_Dealer reference.cs is changed to type "Object[]" for List<IVehicle> StockVehicles, where my assumption was I will get "DTO_Vehicle[] StockVehicles".

在reference.cs中,我能以什么方式实现"DTO_Vehicle [] StockVehicles"?除了使用具体类型而不是接口之外?

 Any ways I can achieve "DTO_Vehicle[] StockVehicles" in reference.cs Other than using concrete type instead of interfaces ?

请在下一节中看到一个包含reference.cs内容的代码片段

Please see one more code snippet in next section has reference.cs contents

 

DataContract] 
[KnownType(typeof(DTO_Vehicle))] 
public class DTO_Dealer : ICarDealer 
{ [DataMember] public string Name { get; set; } [DataMember] public List<IVehicle> StockVehicles { get; set; } } 

public interface IVehicle { string Name { get; set; } string Make { get; set; } string Model { get; set; } int Year { get; set; } } 
public interface ICarDealer { string Name { get; set; } List<IVehicle> StockVehicles { get; set; } } 

public class DTO_Vehicle: IVehicle { public string Name { get; set; } public string Make { get; set; } public string Model { get; set; } public int Year { get; set; } } 

[ServiceContract] 
public interface IService1 { 
[OperationContract] 
DTO.DTO_Dealer GetDealerData(int value); } 

public class Service1 : IService1 
{ 
public DTO.DTO_Dealer GetDealerData(int value) 
{ DTO.DTO_Dealer d = new DTO.DTO_Dealer(); 
d.StockVehicles = new List<ISharedInterfaces.IVehicle>() { new DTO.DTO_Vehicle(){ Name="Dodge" , Make="Cheverolet", Model="Van", Year=1995}, new DTO.DTO_Vehicle(){ Name="Honda" , Make="Accord", Model="Sedan", Year=2005} }; return d; } }

Reference.cs code snippet.

[System.Diagnostics.DebuggerStepThroughAttribute()] 

 [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")] [System.Runtime.Serialization.DataContractAttribute(Name="DTO_Dealer", Namespace="http://schemas.datacontract.org/2004/07/DTO")] [System.SerializableAttribute()] [System.Runtime.Serialization.KnownTypeAttribute(typeof(object[]))] [System.Runtime.Serialization.KnownTypeAttribute(typeof(Consumer.DealerService.DTO_Vehicle))] public partial class DTO_Dealer : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged { [System.NonSerializedAttribute()] private System.Runtime.Serialization.ExtensionDataObject extensionDataField; [System.Runtime.Serialization.OptionalFieldAttribute()] private string NameField; [System.Runtime.Serialization.OptionalFieldAttribute()] private object[] StockVehiclesField; [global::System.ComponentModel.BrowsableAttribute(false)] public System.Runtime.Serialization.ExtensionDataObject ExtensionData { get { return this.extensionDataField; } set { this.extensionDataField = value; } } [System.Runtime.Serialization.DataMemberAttribute()] public string Name { get { return this.NameField; } set { if ((object.ReferenceEquals(this.NameField, value) != true)) { this.NameField = value; this.RaisePropertyChanged("Name"); } } } [System.Runtime.Serialization.DataMemberAttribute()] public object[] StockVehicles { get { return this.StockVehiclesField; } set { if ((object.ReferenceEquals(this.StockVehiclesField, value) != true)) { this.StockVehiclesField = value; this.RaisePropertyChanged("StockVehicles"); } } } public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged; protected void RaisePropertyChanged(string propertyName) { System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged; if ((propertyChanged != null)) { propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName)); } } } [System.Diagnostics.DebuggerStepThroughAttribute()] [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")] [System.Runtime.Serialization.DataContractAttribute(Name="DTO_Vehicle", Namespace="http://schemas.datacontract.org/2004/07/DTO")] [System.SerializableAttribute()] public partial class DTO_Vehicle : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged { [System.NonSerializedAttribute()] private System.Runtime.Serialization.ExtensionDataObject extensionDataField; [System.Runtime.Serialization.OptionalFieldAttribute()] private string MakeField; [System.Runtime.Serialization.OptionalFieldAttribute()] private string ModelField; [System.Runtime.Serialization.OptionalFieldAttribute()] private string NameField; [System.Runtime.Serialization.OptionalFieldAttribute()] private int YearField; [global::System.ComponentModel.BrowsableAttribute(false)] public System.Runtime.Serialization.ExtensionDataObject ExtensionData { get { return this.extensionDataField; } set { this.extensionDataField = value; } } [System.Runtime.Serialization.DataMemberAttribute()] public string Make { get { return this.MakeField; } set { if ((object.ReferenceEquals(this.MakeField, value) != true)) { this.MakeField = value; this.RaisePropertyChanged("Make"); } } } [System.Runtime.Serialization.DataMemberAttribute()] public string Model { get { return this.ModelField; } set { if ((object.ReferenceEquals(this.ModelField, value) != true)) { this.ModelField = value; this.RaisePropertyChanged("Model"); } } } [System.Runtime.Serialization.DataMemberAttribute()] public string Name { get { return this.NameField; } set { if ((object.ReferenceEquals(this.NameField, value) != true)) { this.NameField = value; this.RaisePropertyChanged("Name"); } } } [System.Runtime.Serialization.DataMemberAttribute()] public int Year { get { return this.YearField; } set { if ((this.YearField.Equals(value) != true)) { this.YearField = value; this.RaisePropertyChanged("Year"); } } } public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged; protected void RaisePropertyChanged(string propertyName) { System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged; if ((propertyChanged != null)) { propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName)); } } }

 

 

感谢您的帮助.

 

预先感谢

Mandar.

推荐答案

如果可以使用[DataContract]将接口和类移动到单独的项目中,然后将此程序集添加为客户端项目(当然还有WCF服务库),代码段不会自动生成DTO_Dealer类 DTO_Vehicle和DTO_Vehicle,应用程序将根据需要使用这些接口.

If you can move the interfaces and the classes with [DataContract] into a separate project and add this assembly as a reference for the project of the client (and the WCF service library, of course), the code snippet won't auto-generate the classes DTO_Dealer and DTO_Vehicle and the application will work with the interfaces as you want.

此解决方案是一种很好的方法,因为您在另一个项目中拥有域对象模型(DOM),然后又有了关注点分离. DOM在双方,客户端和服务器端的项目之间共享.

This solution is a good approach as you have the Domain Object Model (DOM) in another project and then you have Separation of Concerns. The DOM is shared among projects, both sides, client-side and server-side.

如果您不能将DOM封装到一个新项目中,那么您别无选择,只能使用具体的类型.

If you can't encapsulate the DOM into a new project, then you have no choice but using the concrete types.


这篇关于WCF服务(如果在DataContract对象图中具有接口)导致reference.cs中的数据类型为Object的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆