none
Problem pri pouziti vice nullable enum ve webove sluzbe

    Dotaz

  • Potrebujeme vytvorit web service, jehoz navratova hodnota odpovida schematu daneho jinym systemem. Pokud toto schema obsahuje vice enum polozek, ktere maji atribut nillable="true", dochazi pri volani sluzby k chybe.

    Problem lze demostrovat na prikladu, kdy schema obsahuje pouze dva elementy typu enum (Class1.xsd):

     

    <?xml version="1.0" encoding="utf-8" ?>
    <sTongue Tiedchema id="Class1"
        targetNamespace="http://oriflame.com/webservices/"
        elementFormDefault="qualified"
        xmlns="http://oriflame.com/webservices/"
        xmlnsTongue Tied="http://www.w3.org/2001/XMLSchema">
      <s:element name="Class1">
        <s:complexType>
          <sTongue Tiedequence>
            <s:element name="E1" nillable="true" minOccurs="0">
              <sTongue TiedimpleType>
                <s:restriction base="sTongue Tiedtring">
                  <s:enumeration value="E1a" />
                  <s:enumeration value="E1b" />
                  <s:enumeration value="E1c" />
                </s:restriction>
              </sTongue TiedimpleType>
            </s:element>
            <s:element name="E2" nillable="true" minOccurs="0">
              <sTongue TiedimpleType>
                <s:restriction base="sTongue Tiedtring">
                  <s:enumeration value="E2a" />
                  <s:enumeration value="E2b" />
                  <s:enumeration value="E2c" />
                </s:restriction>
              </sTongue TiedimpleType>
            </s:element>
          </sTongue Tiedequence>
        </s:complexType>
      </s:element>
    </sTongue Tiedchema>

    Pomoci utility xsd.exe je vygenerovan trida Class1.cs, ktera je pouzita jako navratova hodnota funkce F1. Pri volani teto funkce (http://localhost:1424/Service1.asmx/F1) dochazi k nasledujici chybe:

    System.InvalidOperationException: Unable to generate a temporary class (result=1).
    error CS0266: Cannot implicitly convert type 'Class1E1?' to 'Class1E2?'. An explicit conversion exists (are you missing a cast?)

       at System.Xml.Serialization.Compiler.Compile(Assembly parent, String ns, XmlSerializerCompilerParameters xmlParameters, Evidence evidence)
       at System.Xml.Serialization.TempAssembly.GenerateAssembly(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace, Evidence evidence, XmlSerializerCompilerParameters parameters, Assembly assembly, Hashtable assemblies)
       at System.Xml.Serialization.TempAssembly..ctor(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace, String location, Evidence evidence)
       at System.Xml.Serialization.XmlSerializer.FromMappings(XmlMapping[] mappings, Evidence evidence)
       at System.Web.Services.Protocols.XmlReturn.GetInitializers(LogicalMethodInfo[] methodInfos)
       at System.Web.Services.Protocols.XmlReturnWriter.GetInitializers(LogicalMethodInfo[] methodInfos)
       at System.Web.Services.Protocols.MimeFormatter.GetInitializers(Type type, LogicalMethodInfo[] methodInfos)
       at System.Web.Services.Protocols.HttpServerType..ctor(Type type)
       at System.Web.Services.Protocols.HttpServerProtocol.Initialize()
       at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing)

    Chyba se projevi jiz pri pohledu na definici rozhrani webove sluzby http://localhost:1424/Service1.asmx?WSDL, kde je nesparavne definovan enum E2 (obsahuje hodnoty z E1):

                <s:element minOccurs="0" maxOccurs="1" name="F1Result">
                  <s:complexType>
                    <sTongue Tiedequence>
                      <s:element minOccurs="0" maxOccurs="1" name="E1" nillable="true">
                        <sTongue TiedimpleType>
                          <s:restriction base="sTongue Tiedtring">
                            <s:enumeration value="E1a" />
                            <s:enumeration value="E1b" />
                            <s:enumeration value="E1c" />
                          </s:restriction>
                        </sTongue TiedimpleType>
                      </s:element>
                      <s:element minOccurs="0" maxOccurs="1" name="E2" nillable="true">
                        <sTongue TiedimpleType>
                          <s:restriction base="sTongue Tiedtring">
                            <s:enumeration value="E1a" />
                            <s:enumeration value="E1b" />
                            <s:enumeration value="E1c" />
                          </s:restriction>
                        </sTongue TiedimpleType>
                      </s:element>
                    </sTongue Tiedequence>
                  </s:complexType>
                </s:element>

    Na pozadani muzu zaslat cely priklad demonstrujici vyse zmineny problem.


    K podobnym problemum dochazi, jestlize volame webovou sluzbu obsahujici jako parametr volani objekt s nekolika nullable enums. V tomto pripade dochazi k zamene vyctovych typu a generovane XML obsahuje pouze hodnoty odpovidajici prvnimu z enumu.

     

    Delam neco spatne nebo se jedna o chybu v knihovnach .NET?

    čtvrtek 28. února 2008 16:09

Odpovědi