Reputation: 161773
I just did an "Update Service Reference" to take a single change. A single operation was added to the service, and we wanted to use it.
Unfortunately, there were two proxy classes with the same name in different XML namespaces. The service reference generated these as "Entity" and "Entity1" in the service reference namespace.
Running "Update Service Reference" changed the order of these classes, so that what had been "Entity" was now "Entity1" and vice versa!
Is there some way to make these generated class names stable? Something that would allow me to say, "for complexType A in namespace y, make that Entity, and for complexType A in namespace z, make that Entity1". That way, the order could not change.
P.S. The truly unfortunate thing is that there is about an hour left on this contract - there literally is no tomorrow!
Upvotes: 3
Views: 948
Reputation: 996
VS Add/Update Service Reference functionality allows to configure itself through editing .svcmap file (here is more info on this). This file will preserve changes during update the server reference. Unforunately you can not change the order of classes generated during service update. But you can customize NamespaceMappings to specify a mapping from a WSDL or XML shema namespace to CLR namespace.
For example, this is a first entity:
namespace EntitiesServiceLibEntity1
{
[DataContract]
public class Entity
{
[DataMember]
public string StringValue
{
get { return m_stringValue; }
set { m_stringValue = value; }
}
private string m_stringValue;
}
}
And this is a second:
namespace EntitiesServiceLibEntity2
{
[DataContract]
public class Entity
{
[DataMember]
public int IntValue
{
get { return m_intValue; }
set { m_intValue = value; }
}
private int m_intValue;
}
}
Use "Show All Files" option for your project and expand you service reference. Then edit Reference.svcmap file, adding following 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>
After this you can use namespaces instead of (incorrectly) generated class names:
EntitiesServiceLibEntity1.Entity entity1 = client.GetEntity1();
EntitiesServiceLibEntity2.Entity entity2 = client.GetEntity2();
But if you have access to the service contract (you or your team developed it and can change without breaking another clients) you can use DataContract.Name attribute to set different names for classes, as @Patrice Gahide mentioned.
Upvotes: 2
Reputation: 3744
To override the default naming behaviour and resolve conflicts by yourself, set the Name
property of the DataContractAttribute
to an alternative name explicitly:
[DataContract(Name="Entity1")]
public class Entity
{ ... }
Upvotes: 2