讓我先描述一下我的意圖,然後進入我的問題。如何從對象類型創建SOAP請求?
我試圖構建一個SOAP服務所在的SOAP請求在運行時有點未知通信的系統。最終,我需要做的是從未知對象生成SOAP請求。我將用適當的屬性和屬性從頭開始動態創建對象,然後將其傳遞給我的服務「請求」方法以發送到SOAP服務。以下是我一直在使用的代碼,然後是我收到的錯誤。
SOAP客戶端:
/// <summary>
/// GOSService proxy class
/// </summary>
[DebuggerStepThroughAttribute()]
[DesignerCategoryAttribute("code")]
[WebServiceBindingAttribute(Name = "ServiceSoapBinding", Namespace = "service.domain.com", ConformsTo = WsiProfiles.None)]
[SoapRpcService(RoutingStyle = SoapServiceRoutingStyle.RequestElement)]
public partial class TestService : SoapHttpClientProtocol
{
/// <summary>
/// Initializes a new instance of the TestService class.
/// </summary>
public TestService()
{
this.Url = "https://restsv01.domain.com/ServiceTest/services/TestService";
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(OnRemoteCertificateValidationCallback);
}
/// <summary>
/// Verifies the remote Secure Sockets Layer (SSL) certificate used for authentication.
/// </summary>
/// <param name="sender">An object that contains state information for this validation.</param>
/// <param name="certificate">The certificate used to authenticate the remote party.</param>
/// <param name="chain">The chain of certificate authorities associated with the remote certificate.</param>
/// <param name="sslPolicyErrors">One or more errors associated with the remote certificate.</param>
/// <returns>A Boolean value that determines whether the specified certificate is accepted for authentication.</returns>
private bool OnRemoteCertificateValidationCallback(Object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
}
/// <summary>
///
/// </summary>
/// <param name="order"></param>
/// <returns></returns>
[SoapRpcMethodAttribute("", RequestNamespace = "service.domain.com", ResponseNamespace = "service.domain.com")]
[SampleSoap.LoggerSoapExtensionAttribute]
[return: SoapElementAttribute("requestReturn")]
public object request(object parm)
{
object[] results = this.Invoke("request", new object[] { parm });
return ((object)(results[0]));
}}
測試模型: (不會有任何預先定義的車型,它們將被動態生成的,但出於測試目的,我使用這個模型來測試。 )
[SerializableAttribute()]
[DebuggerStepThroughAttribute()]
[DesignerCategoryAttribute("code")]
[SoapTypeAttribute(Namespace = "http://entity.domain.com")]
public class ParentNode
{
private string nameField = "1";
[SoapElementAttribute(IsNullable = true)]
public string Name
{
get { return this.nameField; }
set { this.nameField = value; }
}
}
測試呼叫代碼:
Services.Soap.Models.ParentNode parent = new Services.Soap.Models.ParentNode();
parent.Name = "John Doe";
Services.Soap.TestService service = new Services.Soap.TestService();
object resp = service.request(parent);
當我運行此代碼,
object[] results = this.Invoke("request", new object[] { parm });
這是錯誤:在該行中出現n錯誤
The type Services.Soap.Models+ParentNode was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.
現在,如果我更改服務的參數「請求」的方法,以一個很強的類型,請求建立精細和傳遞給SOAP服務,就像這樣。
public object request(ParentNode parm)
我試過大概50事情得到這個工作,包括通過類型作爲參數傳遞給請求方法和創建對象的「動態」的實例來傳遞。
public object request(object parm, Type t)
{
dynamic converted = Convert.ChangeType(parm, t);
object[] results = this.Invoke("request", new object[] { converted });
return ((object)(results[0]));
}
這沒有效果,因爲「轉換」仍然被視爲對象類型。
我還試圖攔截SOAP信封中的「GetWriterForMessage」方法,這樣我就可以建立自己的信封,但我沒能充分利用這一點。
所以我的問題是,如何才能得到SOAP請求到帶有參數類型的對象成功打造?我可以採取另一種方法讓我的架構正常工作嗎?
只是將我的評論移至答案。見下文。 – Namphibian