My application has its own scripting language of which I cannot get rid of (lots of customer-specific scripts written). Now my customers are asking if it would be possible to call a SOAP service from within that scripting language. Of course, the SOAP service that needs to be called will be different for every customer. This leaves me with several options:
- Use the WSDL utility to generate customer-specific SOAP client proxies and put the customer-specific logic in my application
- Use the WSDL utility to generate customer-specific SOAP client proxies, put the customer-specific logic in customer-specific DLL's and foresee a plug-in system where the application can call the plug-in in a generic way
- Write a generic module that dynamically generates the SOAP call
The first 2 options are no real alternative in my case since I don't want any customer-specific logic in the application, or customer-specific DLL's.
For me, the 3rd option is, in the long term, the best since it allows my consultant colleagues to call the SOAP service via my scripting language without doing any customer-specific developments. Dynamically adding functions to my scripting language is not a problem, generating the dynamic SOAP call is.
I started by looking at the output of the WSDL utility. Then I started to remove things until it didn't work anymore. The following piece of code is the one still working:
[System::CodeDom::Compiler::GeneratedCodeAttribute(L"wsdl", L"4.0.30319.1"),
System::Diagnostics::DebuggerStepThroughAttribute,
System::ComponentModel::DesignerCategoryAttribute(L"code"),
System::Web::Services::WebServiceBindingAttribute(Name=L"MyOwnScriptingSoapClient", Namespace=L"http://microsoft.com/webservices/")]
public ref class MyWebService : public System::Web::Services::Protocols::SoapHttpClientProtocol
{
public:
MyWebService() {}
public:
[System::Web::Services::Protocols::SoapDocumentMethodAttribute(L"http://microsoft.com/webservices/GetPrimeNumbers", RequestNamespace=L"http://microsoft.com/webservices/",
ResponseNamespace=L"http://microsoft.com/webservices/", Use=System::Web::Services::Description::SoapBindingUse::Literal, ParameterStyle=System::Web::Services::Protocols::SoapParameterStyle::Wrapped)]
System::String^ GetPrimeNumbers(System::Int32 max);
};
inline System::String^ MyWebService::GetPrimeNumbers(System::Int32 max) {
cli::array< System::Object^ >^ results = this->Invoke(L"GetPrimeNumbers", gcnew cli::array< System::Object^ >(1) {max});
return (cli::safe_cast<System::String^ >(results[0]));
}
The URL of the web service can be dynamically by setting the Url property, but I can't find a way to make the method name dynamic.
Adding a generic method like this still seems to work:
...
[System::Web::Services::Protocols::SoapDocumentMethodAttribute(L"http://microsoft.com/webservices/GetPrimeNumbers", RequestNamespace=L"http://microsoft.com/webservices/",
ResponseNamespace=L"http://microsoft.com/webservices/", Use=System::Web::Services::Description::SoapBindingUse::Literal, ParameterStyle=System::Web::Services::Protocols::SoapParameterStyle::Wrapped)]
cli::array< System::Object^ >^ CallWs(cli::array< System::Object^ >^ args);
...
inline cli::array< System::Object^ >^ MyWebService::CallWs(cli::array< System::Object^ >^ args) {
cli::array< System::Object^ >^ results = this->Invoke(L"GetPrimeNumbers", args);
return results;
But as soon as I remove the GetPrimeNumbers method, the call doesn't work anymore and reports the following error:
Unhandled Exception: System.ArgumentException: GetPrimeNumbers Web Service method name is not valid.
at System.Web.Services.Protocols.SoapHttpClientProtocol.BeforeSerialize(WebRequest request, String methodName, Object[] parameters)
at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)
at MyWebService.CallWs(Object[] args)
at main(Int32 argc, SByte** argv)
at _mainCRTStartup()
Also, changing the web service name in the SoapDocumentMethodAttribute attribute (e.g. to GetPrimo), gives this same error.
Therefore, my questions:
- Does it make sense to continue on this path, i.e. looking at the WSDL generated logic trying to 'generalize' the call to a (any) SOAP service or will this simply never work?
- Are there any other good ways of generating the SOAP call in a dynamic way (using .Net)?
- Or is the only method to create the XML (Soap Envelope) yourself to do the SOAP call?
- Any chance of finding some example code that I can continue to work on?
Thanks in advance, Patrick