3

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

这是我公开的服务合同:

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

该服务期望的 SOAP 是:

<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),因此服务不需要它:

<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。我的 Pets 类如下所示:

[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
{...
}
4

1 回答 1

4

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

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();
    }
}
于 2012-10-04T20:07:27.497 回答