2016-04-24 113 views
2

我有一個服務,它要求以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

+0

你可以訪問SOAP服務的WSDL?您將需要此信息才能創建SOAP信封。數據協議不足以調用SOAP服務。 –

+0

嗨,喲有訪問權限,但這將是好的,因爲我已經使用soapUI測試它。所以問題是我需要生成我放入請求的XML。因此,班級人口 – tjhack

回答

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客戶端發送請求。在這個例子中,你將需要與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); 
+0

謝謝你會給出一個出手 – tjhack

+0

svcutil.exe不會創建一個接口,而只是與合同的.cs文件。我如何強制創建一個接口? – tjhack

+0

這意味着您沒有WSDL,只有XSD和合同。如果您沒有WSDL,則可以手動創建ServiceContract接口。您當然必須知道方法名稱和服務名稱空間。 –