XML序列化将请求发送到SOAP服务c#
问题描述:
我有一个服务,它要求以XML格式发送请求。XML序列化将请求发送到SOAP服务c#
我有一个xsd,我用它来使用xsd.exe工具来生成一个它自动创建的xmlattributes的类。
但是我需要填充这个类,但我没有快乐。所以我想填充类中的属性,然后通过请求发送到soap服务。
该类的一个例子如下所示。由于隐私,我只显示部分信息。
public partial class Request {
private string[] itemsField;
private ItemsChoiceType[] itemsElementNameField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Name", typeof(string), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlElementAttribute("Address1", typeof(string), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlElementAttribute("Town", typeof(string), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlElementAttribute("County", typeof(string), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemsElementName")]
public string[] Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("ItemsElementName")]
[System.Xml.Serialization.XmlIgnoreAttribute()]
public ItemsChoiceType[] ItemsElementName {
get {
return this.itemsElementNameField;
}
set {
this.itemsElementNameField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(IncludeInSchema=false)]
public enum ItemsChoiceType {
/// <remarks/>
Name,
/// <remarks/>
Address1,
/// <remarks/>
Town,
/// <remarks/>
County
}
如何填充类并使用xmlserializer将请求发送到服务。
在此先感谢
问候
TJ
答
我怎么能填充类
正如你可以通过创建的实例启动任何普通的.NET类然后在此实例上设置属性值:
var request = new Request();
request.Items = new[] { "item1", "item2" };
request.ItemsElementName = new[]
{
ItemsChoiceType.Name,
ItemsChoiceType.Address1,
};
并使用xmlserializer将请求发送到服务。
现在这是棘手的部分。首先,顾名思义,XmlSerializer类可以用来序列化为XML,而不是发送请求。其次,XmlSerializer不会生成SOAP服务所需的SOAP信封。除了请求XML之外,SOAP信封还包含有关要调用哪个方法的信息。
我会使用较新的svcutil.exe
建议您以创建从WSDL C#的客户合同:
svcutil.exe http://someservice.com/?WSDL
这将使用最近的DataContract属性,你可以使用WCF客户端发送请求。在这个例子中,你将需要与SOAP服务的实际名称空间和适当的操作名称来代替
[System.ServiceModel.ServiceContract(Namespace="http://service.namespace")]
public interface IMyService
{
[System.ServiceModel.OperationContract(Action="http://service.namespace/SomeServiceMethod", ReplyAction="*")]
[System.ServiceModel.XmlSerializerFormat(SupportFaults=true)]
void SomeServiceMethod(Request request);
}
很明显:这也将创造的ServiceContract接口,可以用来调用远程服务。
最后,你可以调用操作:
var binding = new BasicHttpBinding();
var endpoint = new EndpointAddress("http://example.com/myservice");
var channelFactory = new ChannelFactory<IMyService>(binding, endpoint);
var client = channelFactory.CreateChannel();
client.SomeServiceMethod(request);
你可以访问SOAP服务的WSDL?您将需要此信息才能创建SOAP信封。数据协议不足以调用SOAP服务。 –
嗨,哟有访问权限,但这将是好的,因为我已经使用soapUI测试它。所以问题是我需要生成我放入请求的XML。因此,班级人口 – tjhack