0

我正在尝试使用 C# 构建演示性 OPC XML DA 服务器。开发正在进行中,但我被困在有关数组的序列化问题上。显然,当我尝试将 ItemProperty.Value(它是 Object 类型)设置为除 byte[] 之外的任何类型的数组时,我得到了这个异常:

System.InvalidOperationException:在此上下文中不能使用类型 <ArrayType>。

这是触发异常的示例 GetProperties() 方法的内容:

[WebMethodAttribute()]
[
    SoapDocumentMethodAttribute
    (
        "http://opcfoundation.org/webservices/XMLDA/1.0/GetProperties", 
        RequestNamespace = "http://opcfoundation.org/webservices/XMLDA/1.0/", 
        ResponseNamespace = "http://opcfoundation.org/webservices/XMLDA/1.0/", 
        Use = SoapBindingUse.Literal, 
        ParameterStyle = SoapParameterStyle.Wrapped
    )
]
public override ReplyBase GetProperties
(
    [XmlElementAttribute("ItemIDs")] ItemIdentifier[] ItemIDs, 
    [XmlElementAttribute("PropertyNames")] XmlQualifiedName[] PropertyNames, 
    [XmlAttributeAttribute()] string LocaleID, 
    [XmlAttributeAttribute()] string ClientRequestHandle, 
    [XmlAttributeAttribute()] string ItemPath, 
    [XmlAttributeAttribute()] [DefaultValueAttribute(false)] bool ReturnAllProperties,
    [XmlAttributeAttribute()] [DefaultValueAttribute(false)] bool ReturnPropertyValues,
    [XmlAttributeAttribute()] [DefaultValueAttribute(false)] bool ReturnErrorText, 
    [XmlElementAttribute("PropertyLists")] out PropertyReplyList[] PropertyLists, 
    [XmlElementAttribute("Errors")] out OPCError[] Errors
)
{
    ReplyBase Response = new ReplyBase();
    Response.RcvTime = System.DateTime.Now;

    Response.RevisedLocaleID = LocaleID;
    Response.ClientRequestHandle = ClientRequestHandle;

    Errors = null;

    PropertyLists = new PropertyReplyList[1] { new PropertyReplyList() };
    PropertyLists[0].Properties = new ItemProperty[1] { new ItemProperty() };
    PropertyLists[0].Properties[0].Value = new short[2] { 3, 4 };

    Response.ReplyTime = System.DateTime.Now;
    return Response;
}

完整堆栈跟踪(意大利语)

System.Web.Services.Protocols.SoapException: Impossibile elaborare la richiesta. ---> System.InvalidOperationException: Errore durante la generazione del documento XML. ---> System.InvalidOperationException: Il tipo System.Int16[] può non essere utilizzato in questo contesto.
in System.Xml.Serialization.XmlSerializationWriter.WriteTypedPrimitive(String name, String ns, Object o, Boolean xsiType)
in Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write2_Object(String n, String ns, Object o, Boolean isNullable, Boolean needType)
in Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write16_ItemProperty(String n, String ns, ItemProperty o, Boolean isNullable, Boolean needType)
in Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write19_PropertyReplyList(String n, String ns, PropertyReplyList o, Boolean isNullable, Boolean needType)
in Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write26_GetPropertiesResponse(Object[] p)
in Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer13.Serialize(Object objectToSerialize, XmlSerializationWriter writer)
in System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
--- Fine della traccia dello stack dell'eccezione interna ---
in System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
in System.Web.Services.Protocols.SoapServerProtocol.WriteReturns(Object[] returnValues, Stream outputStream)
in System.Web.Services.Protocols.WebServiceHandler.WriteReturns(Object[] returnValues)
in System.Web.Services.Protocols.WebServiceHandler.Invoke()
--- Fine della traccia dello stack dell'eccezione interna ---

我已经在我的项目中包含了由 wsdl.exe 生成的服务框架,并且对于非数组值绝对没有问题。我项目的目标框架是 .NET 4.0(但同样的问题也发生在 3.5 上)。

我的猜测是该方法不适用于服务框架中的 XmlIncludeAttribute() 装饰器。关于如何使它工作的任何线索?

感谢您的时间。

编辑:我尝试将 XmlIncludeAttribute(typeof(int[])) 装饰器添加到 GetProperties() 方法(我猜装饰器不可继承),但我没有收到异常。但是,客户端无法正确反序列化底层数据。反序列化后我从客户端(用 VB.NET 编写)得到的是 XmlNode 引用而不是 int[]。数据在那里,但没有正确反序列化。

这是预期的行为吗?可能是客户端问题?

4

1 回答 1

0

在对生成的 XML 进行更多分析后,我发现客户端需要一个特定的命名空间,即 OPC Web 服务默认命名空间,以便正确反序列化数据。我已经使用以下装饰器更新了 Web 服务实现的类定义:

[WebService(Namespace = "http://opcfoundation.org/webservices/XMLDA/1.0/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[XmlInclude(typeof(short[]))]

GetProperties() 方法现在可以正常运行,并且客户端能够正确反序列化 Int16 的数组。

于 2011-09-08T08:19:37.860 回答