0

I have a web service with parameters of type GUID:

<xs:schema xmlns:ns1="http://schemas.microsoft.com/2003/10/Serialization/" xmlns:tns="http://schemas.datacontract.org/2004/07/TrafficApplicationServer.KP.Objects" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://schemas.datacontract.org/2004/07/TrafficApplicationServer.KP.Objects" attributeFormDefault="qualified" elementFormDefault="qualified">
  <xs:import namespace="http://schemas.microsoft.com/2003/10/Serialization/"/>
  <xs:element name="LtrDocumentObject" type="tns:LtrDocumentObject" nillable="true"/>
  <xs:complexType name="LtrDocumentObject">
    <xs:sequence>
      <xs:element name="UserGuid" type="ns1:guid" nillable="true" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

The service has a definition of GUID:

<xs:schema xmlns:tns="http://schemas.microsoft.com/2003/10/Serialization/" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://schemas.microsoft.com/2003/10/Serialization/" attributeFormDefault="qualified" elementFormDefault="qualified">
  <xs:element name="guid" type="tns:guid" nillable="true"/>
  <xs:simpleType name="guid">
    <xs:restriction base="xs:string">
      <xs:pattern value="[\da-fA-F]{8}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{12}"/>
    </xs:restriction>
  </xs:simpleType>
</xs:schema>

But when I try to generate proxy-class for this wsdl at Visual Studio 2017 (.Net Framework 4.7.2) GUID type is created as string:

/// <remarks/>
[System.Xml.Serialization.XmlIncludeAttribute(typeof(KpDocumentObject))]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(TeoDocumentObject))]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.8.3752.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://schemas.datacontract.org/2004/07/TrafficApplicationServer.KP.Objects")]
public partial class LtrDocumentObject : object, System.ComponentModel.INotifyPropertyChanged {
    private string userGuidField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(IsNullable=true, Order=48)]
    public string UserGuid {
        get {
            return this.userGuidField;
        }
        set {
            this.userGuidField = value;
            this.RaisePropertyChanged("UserGuid");
        }
    }

    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));
        }
    }
}

What should I do to generate System.Guid type?

CyClop
  • 46
  • 5
  • `var guid = new System.Guid(yourGuidString);` – jwdonahue Feb 28 '20 at 20:29
  • @jwdonahue I understand that I can convert type by myself but I want to auto-generate guid type to escape additional code of converting types. Previously I had auto-generated guid type from another service, but i can't find reason why it doesn't work at current case – CyClop Feb 29 '20 at 08:31
  • They defined their GUID to be of string type. Unless, or until they support something else, that's what you get. – jwdonahue Feb 29 '20 at 18:20
  • You could probably write or rip some XSD that includes that GUID and then do some XSLT transformation on the service XSD. You'd have to be careful about how you serialize, if your sending any GUID's back to them though. They are obviously expecting a textual rendering of a 128 bit number, not the binary form. – jwdonahue Feb 29 '20 at 18:39

1 Answers1

0

GUIDs are converted to strings in order to accomplish interoperability.

You can change the type back to Guid on the client side by doing:

Guid myguid = new System.Guid(guidString);
Gonzalo Diaz
  • 139
  • 1
  • 8
  • I understand that I can convert type by myself but I want to auto-generate guid type to escape additional code of converting types. Previously I had auto-generated guid type from another service, but i can't find reason why it doesn't work at current case – CyClop Feb 29 '20 at 08:26