您的位置:首页 > Web前端

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

2015-09-08 18:00 405 查看
作者: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类的相关信息。希望知道原因的朋友留言说一下,谢谢。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息