提问人:John Saunders 提问时间:2/20/2014 最后编辑:John Saunders 更新时间:2/2/2015 访问量:943
“更新服务参考”更改了某些代理类型的定义
"Update Service Reference" Changes the Definition of some Proxy Types
问:
我只是做了一个“更新服务参考”来进行一次更改。将单个操作添加到服务中,我们想使用它。
遗憾的是,在不同的 XML 命名空间中有两个同名的代理类。服务引用在服务引用命名空间中将它们生成为“Entity”和“Entity1”。
运行“更新服务引用”更改了这些类的顺序,因此原来的“Entity”现在是“Entity1”,反之亦然!
有没有办法使这些生成的类名稳定?允许我说,“对于命名空间 y 中的 complexType A,创建该实体,对于命名空间 z 中的 complexType A,创建该 Entity1”。这样,顺序就无法更改。
P.S. 真正不幸的是,这份合同还剩下大约一个小时——实际上没有明天!
答:
若要覆盖默认命名行为并自行解决冲突,请将 的属性显式设置为备用名称:Name
DataContractAttribute
[DataContract(Name="Entity1")]
public class Entity
{ ... }
VS 添加/更新服务引用功能允许通过编辑 .svcmap 文件来配置自身(此处是有关此内容的详细信息)。此文件将在更新服务器引用期间保留更改。很遗憾,您无法更改服务更新期间生成的类的顺序。但是,您可以自定义 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 所提到的。
评论