How to remove xml namespace when return type is a generic type in wcf restful service

The following is an interface definition.

[OperationContract]
[WebGet(UriTemplate = "FacebookData/?accessToken={accessToken}")]
OperationResult<FacebookData> GetFacebookData(string accessToken);

Return type OperationResult<FacebookData>, this is a general type

Then I will get xml as below ...

OperationResultOfFacebookDataNcCATIYq xmlns: i = "http://www.w3.org/2001/XMLSchema-instance"

How to remove namespace and rename xml element as "OperationResult"

By the way, I already set the namespace empty

[DataContract(Namespace = "")]
public class OperationResult<T>

but I think that a generic type will generate a class for each T.

0
source share
3 answers

[DataContract(Name = "OperationResult")], RESTful-.

, class FacebookOperationResult : OperationResult<FacebookData> {}, .

+2
0

xmlns, , , WCF.

:

 [ServiceContract]
    public interface IService
    {
        [OperationContract]
        [WebInvoke(UriTemplate = "ProcessMessage")]
        AResponse ProcessMessage(ARequest content);
    }

: // ,

 [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] 
    public class Service : IService
    {
        public AResponse ProcessMessage(ARequest content)
        {
         //todo  
        }
    }

:

[XmlRoot("My_Root", Namespace = "")]
ARequest : IXmlSerializable
{
    public string PropertyA { get; set; }

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return (null);
        }

        public void ReadXml(System.Xml.XmlReader reader)
        {
            if (!reader.IsEmptyElement)
            {
                reader.ReadStartElement();
                PropertyA = reader.ReadElementString("PropertyA");
                reader.ReadEndElement();
            }
        }

        public void WriteXml(System.Xml.XmlWriter writer)
        {
            writer.WriteElementString("PropertyA", PropertyA);

        }

}
0

Source: https://habr.com/ru/post/1725430/


All Articles