Winform客户端引用WCF客户端后,部分类无法正常使用

作者:jiankunking 出处:http://blog.csdn.net/jiankunking

在项目中用到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项目即可。

小注:

如果不进行修改,则在客户端调用WCF服务端方法的时候,获取函数入参类型或者返回值类型的类型(如果该入参、返回值 类型为自定义实体类),是通过WCF服务端获取的,即使该实体类在SPI中,客户端也有,调用的也是服务端的。

就相当于你自己显式将该自定义实体类标记为数据契约。

使用WebService也会出现这种问题,解决思路一样。
但产生这个问题的原因暂时还没有找到,因为UI项目中已经引用SPI项目了,还是会在Reference.cs文件中生成ConnectionInfo类的相关信息。希望知道原因的朋友留言说一下,谢谢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值