让我先描述我的意图是什么,然后进入我的问题。
我正在尝试构建一个与 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 });
这是错误:
类型 Services.Soap.Models+ParentNode 不是预期的。使用 XmlInclude 或 SoapInclude 属性指定静态未知的类型。
现在,如果我将服务“请求”方法的参数更改为强类型,则请求构建良好并传递给 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]));
}
这不起作用,因为“已转换”仍被视为对象类型。
我还尝试在“GetWriterForMessage”方法中截取soap 信封,这样我就可以构建自己的信封,但我无法使用它。
所以我的问题是,如何使用参数类型的对象成功构建 SOAP 请求?我应该采取另一种方法来使我的架构正常工作吗?