在项目中用到WCF,项目的结构是这样的:
在SPI项目中编写该解决方案中公共的类及函数;
在WCFService项目中写的是svc文件及WCF服务接口;
在Client中添加WCF服务引用的时候该WCFService项目,然后再UI项目中初始化Client的实例。通过该实例WCFService中的方法。
问题:
有时会出现这么一种情况,在UI中通过初始化Client的实例。通过该实例WCFService中的方法MethodA,该方法的入参是SPI中类ConnectionInfo的实例,然后在UI中再次使用类ConnectionInfo时会发现ConnectionInfo是在Client命名空间下的类,而不是在SPI命名空间下的类。
在WCF自动生成的客户端代理类(Reference.cs文件)中,会将SPI中的类ConnectionInfo,序列化成:
[System.Diagnostics.DebuggerStepThroughAttribute()] [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")] [System.Runtime.Serialization.DataContractAttribute(Name="ConnectionInfo", Namespace="http://schemas.datacontract.org/2004/07/HaiChuang.AMAC.DataSourceSettings.SPI.Mod" + "el")] [System.SerializableAttribute()] public partial class ConnectionInfo : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged { [System.NonSerializedAttribute()] private System.Runtime.Serialization.ExtensionDataObject extensionDataField; [System.Runtime.Serialization.OptionalFieldAttribute()] private string ConnectionStringField; [System.Runtime.Serialization.OptionalFieldAttribute()] private string GuidField; [System.Runtime.Serialization.OptionalFieldAttribute()] private string LocationField; [System.Runtime.Serialization.OptionalFieldAttribute()] private string MenuGuidField; [System.Runtime.Serialization.OptionalFieldAttribute()] private string NameField; [System.Runtime.Serialization.OptionalFieldAttribute()] private string TypeField; [System.Runtime.Serialization.OptionalFieldAttribute()] private int idField; [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 ConnectionString { get { return this.ConnectionStringField; } set { if ((object.ReferenceEquals(this.ConnectionStringField, value) != true)) { this.ConnectionStringField = value; this.RaisePropertyChanged("ConnectionString"); } } } [System.Runtime.Serialization.DataMemberAttribute()] public string Guid { get { return this.GuidField; } set { if ((object.ReferenceEquals(this.GuidField, value) != true)) { this.GuidField = value; this.RaisePropertyChanged("Guid"); } } } [System.Runtime.Serialization.DataMemberAttribute()] public string Location { get { return this.LocationField; } set { if ((object.ReferenceEquals(this.LocationField, value) != true)) { this.LocationField = value; this.RaisePropertyChanged("Location"); } } } [System.Runtime.Serialization.DataMemberAttribute()] public string MenuGuid { get { return this.MenuGuidField; } set { if ((object.ReferenceEquals(this.MenuGuidField, value) != true)) { this.MenuGuidField = value; this.RaisePropertyChanged("MenuGuid"); } } } [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 string Type { get { return this.TypeField; } set { if ((object.ReferenceEquals(this.TypeField, value) != true)) { this.TypeField = value; this.RaisePropertyChanged("Type"); } } } [System.Runtime.Serialization.DataMemberAttribute()] public int id { get { return this.idField; } set { if ((this.idField.Equals(value) != true)) { this.idField = value; this.RaisePropertyChanged("id"); } } } 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)); } } }
该实体类代码为:
/// <summary> /// 数据源信息 /// </summary> public class ConnectionInfo { public int id { get; set; } public string Guid { get; set; } public string Name { get; set; } public string Location { get; set; } public string Type { get; set; } public string ConnectionString { get; set; } public string MenuGuid { get; set; } }
在 CLient项目的wcf引用上右键,可以看到该实体类
在项目中可以找到该dll
打开该dll,可以看到该类的信息
此处看到的类信息是被转成WCF数据契约形式的类信息
解决方案:
如果要在UI项目中使用SPI命名空间下的类ConnectionInfo,则将Reference.cs文件中的public partial class ConnectionInfo删除,再在UI中引用SPI项目即可。
小注:
使用WebService也会出现这种问题,解决思路一样。
但产生这个问题的原因暂时还没有找到,因为UI项目中已经引用SPI项目了,还是会在Reference.cs文件中生成ConnectionInfo类的相关信息。希望知道原因的朋友留言说一下,谢谢。
版权声明:作者:jiankunking 出处:http://blog.csdn.net/jiankunking 本文版权归作者和CSDN共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。