Question

I am developing a console application in VB NET using WCF which communicates with a secure vendor service. I want to serialize both the request and the response from the calls and store them in a XML file for analysis. 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.

Here is the code I have written (obj being the complex object):

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. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.'

As previously mentioned, this is just one of several classes generated by SVCUTIL and incorporated into my application. Is there a way to get SVCUTIL to emit the XMLInclude attribute for all of those classes? 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.

The attributes generated by SVCUTIL for each class typically looks like this:

<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.

This thread discusses the /reference option of svcutil. 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.

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:

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.

Dim extraTypes As Type() = 
   {GetType(type1),
    GetType(type2),
    GetType(type3),
    GetType(type4)}
Was it helpful?

Solution

Use extraType parameter of xmlserializer method. See updated comments included in original post.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top