Why does the XmlRoot attribute gets ignored in WCF and how to overcome this

后端 未结 2 1919
悲哀的现实
悲哀的现实 2020-12-16 04:07

We\'ve observed that when we expose a WCF service which uses classes decorated with various xml serialisation attributes, despite the fact that we use the XmlSerializerForma

2条回答
  •  渐次进展
    2020-12-16 04:55

    I don't know why WCF ignores XmlRoot, so I can't answer that part of your question. But I do have a couple ways to solve the problem.

    1. start with WSDL first.
      If you have a particular set of XML namespaces you would like to apply to the messages that get sent and receieved, use WSDL and XML Schema to explicitly specify them.

      Then, generate the Server-side stub code, or the client-side proxy code, directly from that WSDL via the svcutil.exe tool.

    2. use a custom ServiceHost
      The other option open to you, described at this link, is to use a custom ServiceHost that overrides WCF's decision to disregard the XmlRoot or XmlType attributes on message types.


    If you choose to go for the WSDL-First approach, the WSDL should look like this:

    
    
    
    
        
          
            
              
                
              
            
            
          
    
    
          
            
              
                
                
                
              
            
            
          
    
        
    
    
    
    
       
    
    
       
    
    
    
    
    
      
          
          
       
    
    
    
    
    
        
    
        
            
               
              
        
    
    
    
    

    This WSDL is very simple - it defines a single operation, with a single request message and a single response message.

    Notice there are three xml namespaces:

    • urn:The-Service-namespace
      used for the element that wraps the request and response - the first element inside the
    • urn:The-Request-namespace
      used for the element wrapped inside that request wrapper, which gets deserialized into an instance of Type1.
    • urn:The-Response-namespace
      used for the element wrapped inside that response wrapper, which gets deserialized into an instance of Type2.

    If your web services interface is more complicated, has more operations and consequently more request and response message types, you can add more namespaces, if you like, for all those additional types.

提交回复
热议问题