OPC XML DA服务器的序列化问题:类型不能在此上下文中使用

本文关键字:不能 上下文 ArrayType 服务器 DA XML 序列化 类型 问题 OPC | 更新日期: 2023-09-27 18:07:49

我正在尝试使用c#构建一个示范性的OPC XML DA服务器。开发正在进行中,但是我被一个关于数组的序列化问题卡住了。显然,当我尝试设置ItemProperty。值(对象类型)到任何类型的数组,但byte[],我得到这个异常:

系统。InvalidOperationException:类型<不能在此上下文中使用。>

下面是触发异常的示例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[]。数据在那里,但没有正确地反序列化。

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

OPC XML DA服务器的序列化问题:类型<ArrayType>不能在此上下文中使用

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

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

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