让我先描述一下我的意图,然后进入我的问题。如何从对象类型创建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