我正在编写一个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
{...
}
答案 0 :(得分:3)
要删除其中一个元素,您可以使用未包装的消息合约(使用[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();
}
}