XML序列化将请求发送到SOAP服务c#

时间:2016-04-24 06:42:31

标签: c# xml soap xsd

我有一项服务要求以XML格式发送请求。

我有一个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

1 个答案:

答案 0 :(得分:2)

  

我如何填充课程

与任何普通的.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客户端发送请求。这也将创建ServiceContract接口,可用于调用远程服务:

[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);
}

显然,在这个例子中,您需要替换SOAP服务的实际名称空间和正确的操作名称。

最后你可以调用这个操作:

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);
相关问题