jeudi 29 janvier 2015

Dynamic Web Service Client

I have written a dynamic web service client. That works with SOAP 1.1 but fails with SOAP 1.2


When I use ServiceDescriptionImporter.Import I get the following warning: OptionalExtensionsIgnored


Below is the code to prepare the web service:



using (var client = new WebClient())
{
//Trust all certificates
System.Net.ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);
client.Credentials = new NetworkCredential(@"domain\user","password");
using (var stream = client.OpenRead(url))
{
// Get a WSDL file describing the service.
ServiceDescription description = ServiceDescription.Read(stream);

// Initialize a service description importer.
ServiceDescriptionImporter importer = new ServiceDescriptionImporter();
importer.ProtocolName = webServiceProtocol.ToString();
importer.Style = ServiceDescriptionImportStyle.Client;
importer.AddServiceDescription(description, null, null);

// Report on the service descriptions.
Console.WriteLine("Importing {0} service descriptions with {1} associated schemas.",
importer.ServiceDescriptions.Count, importer.Schemas.Count);

// Add any imported files
foreach (System.Xml.Schema.XmlSchema wsdlSchema in description.Types.Schemas)
{
foreach (System.Xml.Schema.XmlSchemaObject externalSchema in wsdlSchema.Includes)
{
if (externalSchema is System.Xml.Schema.XmlSchemaImport)
{
Uri baseUri = new Uri(url);
Uri schemaUri = new Uri(baseUri, ((System.Xml.Schema.XmlSchemaExternal)externalSchema).SchemaLocation);
using (var schemaStream = client.OpenRead(schemaUri))
{
System.Xml.Schema.XmlSchema schema = System.Xml.Schema.XmlSchema.Read(schemaStream, null);
importer.Schemas.Add(schema);
}
Console.WriteLine(((System.Xml.Schema.XmlSchemaExternal)externalSchema).SchemaLocation);
}
}
}



// Generate a proxy client.
importer.Style = ServiceDescriptionImportStyle.Client;

// Generate properties to represent primitive values.
importer.CodeGenerationOptions = System.Xml.Serialization.CodeGenerationOptions.GenerateProperties;

// Initialize a Code-DOM tree into which we will import the service.
CodeNamespace nmspace = new CodeNamespace();
CodeCompileUnit unit1 = new CodeCompileUnit();
unit1.Namespaces.Add(nmspace);

// Import the service into the Code-DOM tree. This creates proxy code
// that uses the service.
ServiceDescriptionImportWarnings warning = importer.Import(nmspace, unit1);
Console.WriteLine("Warning: " + warning);

if (warning == 0 || warning == ServiceDescriptionImportWarnings.OptionalExtensionsIgnored)
{
// Generate and print the proxy code in C#.
CodeDomProvider provider1 = CodeDomProvider.CreateProvider("CSharp");

// Compile the assembly with the appropriate references
string[] assemblyReferences = new string[2] { "System.Web.Services.dll", "System.Xml.dll" };
CompilerParameters parms = new CompilerParameters(assemblyReferences);
CompilerResults results = provider1.CompileAssemblyFromDom(parms, unit1);
assembly = results.CompiledAssembly;

foreach (CompilerError oops in results.Errors)
{
Console.WriteLine("========Compiler error============");
Console.WriteLine(oops.ErrorText);
}

}
else
{
// Print an error message.
Console.WriteLine("Warning: " + warning);
}
}
}


If I ignore the warning and compile the code using CodeDomProvider it compiles with no errors. The problem is when I call a method from the web service I then get the following error: Exception has been thrown by the target of an invocation. SOAP header Action was not understood.


The code to call the method is below:



//Invoke the web service method
object service = GetAssembly().CreateInstance("BizTalkServiceInstance");
Type serviceType = service.GetType();
PropertyInfo propInfo = serviceType.GetProperty("Credentials");
propInfo.SetValue(service, new NetworkCredential("user", "pass", "domain"), null);

object request = GetObjectFromString(requestName, requestValue);


object response = serviceType.InvokeMember(methodName, System.Reflection.BindingFlags.InvokeMethod, null, service, new object[] { request });
Console.WriteLine(GetValueFromObject(responseName,response));
Console.ReadLine();
return null;


I really cannot work out what I am missing.






Aucun commentaire:

Enregistrer un commentaire