views:

308

answers:

0

A problem with "Add Service Reference", and actually with SvcUtil over all its features. In order to reproduce you just need to add an OperationContract with argument or returning the following class :

[XmlSchemaProvider("MySchema")]
public class MyStructure : IXmlSerializable
{
    private XmlElement e;

    private static void Func(object o, ValidationEventArgs args)
    {
    }

    public static XmlQualifiedName MySchema(XmlSchemaSet xs)
    {
        //xs.XmlResolver = new XmlUrlResolver();             
        XmlSchema s = XmlSchema.Read(new XmlTextReader(new StringReader("<?xml version=\"1.0\"?><xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"&gt;&lt;xs:complexType name=\"MyStructure\"><xs:sequence><xs:any /></xs:sequence></xs:complexType></xs:schema>")), null);

        xs.Add(s);
        return new XmlQualifiedName("MyStructure");
    }

    #region IXmlSerializable Members

    public System.Xml.Schema.XmlSchema GetSchema()
    {
        throw new NotImplementedException();
    }

    public void ReadXml(XmlReader reader)
    {
       XmlDocument doc = new XmlDocument();
       e = (XmlElement)doc.ReadNode(reader);
    }

    public void WriteXml(XmlWriter writer)
    {
        e.WriteTo(writer);
    }

    #endregion
}

The result is that when you use AddWebReference or AddSerivceReference without a reference to the class library containing the MyStructure type, everything will be fine ad you will get an xmlElement representation at the auto created proxy.

However, when you have a reference you will get the following warning :

================

Warning 1 Custom tool warning: Cannot import wsdl:portType Detail: An exception was thrown while running a WSDL import extension: System.ServiceModel.Description.DataContractSerializerMessageContractImporter Error: Referenced type 'ServiceLibrary.MyStructure, ServiceLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' with data contract name 'MyStructure' in namespace '' cannot be used since it does not match imported DataContract. Need to exclude this type from referenced types. XPath to Error Source: //wsdl:definitions[@targetNamespace='http://tempuri.org/']/wsdl:portType[@name='IService1'] \Projects\WCFSample\WCFExample\TestAddReference\Service References\ServiceReference1\Reference.svcmap 1 1 TestAddReference

======================

And no proxy will be generated for you.

Now, the internet is full with descriptions of this when you have a generic DataContract, and/or using IsReference attribute.

This is a much serious problem, since any non-typed data will do this problem. Could not find any way to solve the problem. What if I want to know the type at the client side, by sharing the class library of the contracts ?