VS 添加/更新服务参考功能允许通过编辑 .svcmap 文件来配置自身(here 对此有更多信息)。此文件将在更新服务器引用期间保留更改。不幸的是,您无法更改服务更新期间生成的类的顺序。但是您可以自定义 NamespaceMappings 以指定从 WSDL 或 XML shema 命名空间到 CLR 命名空间的映射。
例如,这是第一个实体:
namespace EntitiesServiceLibEntity1
{
[DataContract]
public class Entity
{
[DataMember]
public string StringValue
{
get { return m_stringValue; }
set { m_stringValue = value; }
}
private string m_stringValue;
}
}
这是一秒钟:
namespace EntitiesServiceLibEntity2
{
[DataContract]
public class Entity
{
[DataMember]
public int IntValue
{
get { return m_intValue; }
set { m_intValue = value; }
}
private int m_intValue;
}
}
为您的项目使用“显示所有文件”选项并扩展您的服务参考。然后编辑 Reference.svcmap 文件,添加以下 NamespaceMappings:
<NamespaceMappings>
<NamespaceMapping TargetNamespace="http://schemas.datacontract.org/2004/07/EntitiesServiceLibEntity1" ClrNamespace="EntitiesServiceLibEntity1" />
<NamespaceMapping TargetNamespace="http://schemas.datacontract.org/2004/07/EntitiesServiceLibEntity2" ClrNamespace="EntitiesServiceLibEntity2" />
</NamespaceMappings>
在此之后,您可以使用命名空间来代替(错误地)生成的类名:
EntitiesServiceLibEntity1.Entity entity1 = client.GetEntity1();
EntitiesServiceLibEntity2.Entity entity2 = client.GetEntity2();
但是,如果您有权访问服务合同(您或您的团队开发了它并且可以在不破坏其他客户的情况下进行更改),您可以使用 DataContract.Name 属性为类设置不同的名称,正如@Patrice Gahide 提到的那样。