关于c#:如何从响应中删除”方法”节点?

How do I remove the “method” node from the response?

我正在编写WCF服务,该服务应处理我无法控制的预定义SOAP / XML格式。

这是我的裸露服务合同:

1
2
3
[OperationContract]
[WebInvoke(Method ="POST")]
bool SavePets(Pets Pets);

此服务期望的SOAP是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Body>
    <SavePets xmlns="http://tempuri.org">
      <Pets>
        <Dog>
          <Name>Fido</Name>
        </Dog>
        <Dog>
          <Name>Duke</Name>
        </Dog>
        <Cat>
          <Name>Max</Name>
        </Cat>
      </Pets>
    </SavePets>
  </s:Body>
</s:Envelope>

但是,我需要删除方法名称(SavePets)或参数名称(Pets),因此服务不希望这样做:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Body>
      <Pets>
        <Dog>
          <Name>Fido</Name>
        </Dog>
        <Dog>
          <Name>Duke</Name>
        </Dog>
        <Cat>
          <Name>Max</Name>
        </Cat>
      </Pets>
  </s:Body>
</s:Envelope>

我没有使用DataContracts或MessageContracts。我的宠物课看起来像这样:

1
2
3
4
5
6
7
8
9
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml","4.0.30319.233")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = false, Namespace ="http://tempuri.org")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace ="http://tempuri.org", IsNullable = true)]
    public partial class Pets
{...
}

要删除这些元素之一,可以使用未package的消息协定(用[MessageContract(IsWrapped = false)]装饰的类型)。下面的代码显示了一种可以接收该请求的服务。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
public class StackOverflow_12733486
{
    [ServiceContract(Namespace ="")]
    [XmlSerializerFormat]
    public interface ITest
    {
        [OperationContract]
        SavePetsResponse SavePets(SavePetsRequest request);
    }
    public class Service : ITest
    {
        public SavePetsResponse SavePets(SavePetsRequest request)
        {
            return new SavePetsResponse { Result = true };
        }
    }
    [MessageContract(IsWrapped = false)]
    public class SavePetsRequest
    {
        [MessageBodyMember]
        public Pets Pets { get; set; }
    }
    [MessageContract(WrapperNamespace ="")]
    public class SavePetsResponse
    {
        [MessageBodyMember]
        public bool Result { get; set; }
    }
    public class Pets
    {
        [XmlElement(ElementName ="Dog")]
        public string[] Dogs;
        [XmlElement(ElementName ="Cat")]
        public string[] Cats;
    }
    static Binding GetBinding()
    {
        var result = new BasicHttpBinding();
        return result;
    }
    public static void Test()
    {
        string baseAddress ="http://" + Environment.MachineName +":8000/Service";
        ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
        host.AddServiceEndpoint(typeof(ITest), GetBinding(),"");
        host.Open();
        Console.WriteLine("Host opened");

        ChannelFactory<ITest> factory = new ChannelFactory<ITest>(GetBinding(), new EndpointAddress(baseAddress));
        ITest proxy = factory.CreateChannel();
        Pets pets = new Pets { Cats = new string[] {"Max" }, Dogs = new string[] {"Fido","Duke" } };
        proxy.SavePets(new SavePetsRequest { Pets = pets });

        ((IClientChannel)proxy).Close();
        factory.Close();

        WebClient c = new WebClient();
        c.Headers[HttpRequestHeader.ContentType] ="text/xml";
        c.Headers["SOAPAction"] ="urn:ITest/SavePets";
        string reqBody = @"<s:Envelope xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/"">
                <s:Body>
                    <Pets>
                        <Dog>Fido</Dog>
                        <Dog>Duke</Dog>
                        <Cat>Max</Cat>
                    </Pets>
                </s:Body>
            </s:Envelope>"
;
        Console.WriteLine(c.UploadString(baseAddress, reqBody));

        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }
}