简体   繁体   English

svcutil生成的代码需要xmlinclude

[英]svcutil generated code needs xmlinclude

I am developing a console application in VB NET using WCF which communicates with a secure vendor service. 我正在使用WCF在VB NET中开发一个控制台应用程序,该应用程序可与安全的供应商服务进行通信。 I want to serialize both the request and the response from the calls and store them in a XML file for analysis. 我想序列化请求和调用的响应,并将它们存储在XML文件中进行分析。 Unfortunately, the object I am trying to serialize is complex. 不幸的是,我要序列化的对象很复杂。 It is based on the classes produced by SVCUTIL from the vendor supplied WSDL and xsd files. 它基于SVCUTIL从供应商提供的WSDL和xsd文件生成的类。

Here is the code I have written (obj being the complex object): 这是我编写的代码(obj是复杂对象):

Dim sr As StreamWriter
Dim x As XmlSerializer

sr = New StreamWriter("U:\logs\Responses.xml")
x = New XmlSerializer(obj.GetType)
x.Serialize(sr, obj)

When I hit the Serialize method, I get an InvalidOperationException with the InnerException message: 'The type [generated class name] was not expected. 当我点击Serialize方法时,我收到带有InnerException消息的InvalidOperationException:'预期的类型不是[生成的类名]。 Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.' 使用XmlInclude或SoapInclude属性来指定静态未知的类型。

As previously mentioned, this is just one of several classes generated by SVCUTIL and incorporated into my application. 如前所述,这只是SVCUTIL生成并合并到我的应用程序中的几个类之一。 Is there a way to get SVCUTIL to emit the XMLInclude attribute for all of those classes? 有没有办法让SVCUTIL为所有这些类发出XMLInclude属性? The WSDL is subject to change by the vendor and I'd hate to have to manually add the attribute for all those classes again when it does. WSDL可能会受到供应商的更改,因此我不希望再次为所有这些类手动添加属性。

The attributes generated by SVCUTIL for each class typically looks like this: SVCUTIL为每个类生成的属性通常如下所示:

<System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "4.0.30319.1"),  _
 System.SerializableAttribute(),  _
 System.Diagnostics.DebuggerStepThroughAttribute(),  _
 System.ComponentModel.DesignerCategoryAttribute("code"),  _
 System.Xml.Serialization.XmlTypeAttribute([Namespace]:="[Vendor Specific].xsd")>

This is the command that I use currently to generate proxy classes: 这是我当前用于生成代理类的命令:

svcutil *.wsdl *.xsd /language:vb /async /tcv:Version35 /out:VendorAsync.vb /config:VendorAsync.config

I added options /ImportXMLTypes /SER:XMLSerializer to the above command with no change in the proxy classes generated. 我将选项/ ImportXMLTypes / SER:XMLSerializer添加到上述命令中,而生成的代理类没有任何变化。

This thread discusses the /reference option of svcutil. 该线程讨论svcutil的/ reference选项。 I don't know if this will help and I'm not sure what to specify for the path. 我不知道这是否有帮助,我不确定为路径指定什么。 This reference suggests that /reference will not help with my XMLSerializer issue however. 该参考文献表明/ reference不能解决我的XMLSerializer问题。

Here is another thread that seems like it would be relevant. 这是另一个似乎很重要的话题。 Any guidance would certainly be appreciated. 任何指导当然将不胜感激。


I found this thread that really helped me to finally work around the serializer issue! 我发现此线程确实帮助我最终解决了序列化程序问题! It works like a charm now. 现在它就像一种魅力。 Basically, all you need to do is to call the serializer method with the extraTypes parameter, like so: 基本上,您需要做的就是使用extraTypes参数调用序列化程序方法,如下所示:

x = New XmlSerializer(obj.GetType, extraTypes)

ExtraTypes is an array of types that I was able to build quite easily since all of the types were included in the proxy classes created by SVCUTIL. ExtraTypes是一个类型数组,由于所有类型都包含在SVCUTIL创建的代理类中,因此我能够轻松构建它们。

Dim extraTypes As Type() = 
   {GetType(type1),
    GetType(type2),
    GetType(type3),
    GetType(type4)}

Use extraType parameter of xmlserializer method. 使用xmlserializer方法的extraType参数。 See updated comments included in original post. 查看原始帖子中包含的更新评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM