XML 向 SOAP 服务发送请求的序列化 c#

XML serialization to send request to SOAP service c#

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

我有一个 xsd,我从中使用 xsd.exe 工具生成了一个 class,它自动创建了 xmlattributes。

但是我需要填充这个 class 但我没有快乐。所以我想填充 class 中的属性,然后通过请求将其发送到 soap 服务。

class 的示例如下所示。由于隐私原因,我只显示了部分信息。

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
}

如何填充 class 并使用 xmlserializer 将请求发送到服务。

提前致谢

此致

TJ

How can i populate the class

与任何普通的 .NET class 一样,您可以先创建它的实例,然后在该实例上设置 属性 值:

var request = new Request();
request.Items = new[] { "item1", "item2" };
request.ItemsElementName = new[]
{
    ItemsChoiceType.Name,
    ItemsChoiceType.Address1,
};

and use xmlserializer to send the request to the service.

现在是棘手的部分。首先,XmlSerializer class,顾名思义,可用于序列化为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);