【发布时间】:2020-02-28 20:24:52
【问题描述】:
我有一个带有 GUID 类型参数的 Web 服务:
<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>
服务有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>
但是当我尝试在 Visual Studio 2017 (.Net Framework 4.7.2) 上为此 wsdl 生成代理类时,GUID 类型被创建为字符串:
/// <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));
}
}
}
如何生成System.Guid类型?
【问题讨论】:
-
var guid = new System.Guid(yourGuidString); -
@jwdonahue 我知道我可以自己转换类型,但我想自动生成 guid 类型以转义转换类型的附加代码。以前我有从另一个服务自动生成的 guid 类型,但我找不到它在当前情况下不起作用的原因
-
他们将 GUID 定义为字符串类型。除非,或者直到他们支持别的东西,这就是你得到的。
-
您可能会编写或翻录一些包含该 GUID 的 XSD,然后在服务 XSD 上执行一些 XSLT 转换。如果您将任何 GUID 发送回给他们,您必须小心您的序列化方式。他们显然期望 128 位数字的文本呈现,而不是二进制形式。