WebClient client = new WebClient ();
String url = "Http://localhost/MESAPIWebService/MESAPI.asmx?WSDL";//This address can be written in the config file, which is taken out here. Add: After the original address. Wsdl
Stream stream = client. OpenRead (URL);
ServiceDescription Description = Servicedescription.read (stream);
ServiceDescriptionImporter importer = new ServiceDescriptionImporter ();//Create a client proxy proxy class.
Importer. ProtocolName = "Soap"; Specifies the Access protocol.
Importer. Style = servicedescriptionimportstyle.client; Generates a client agent.
Importer. CodeGenerationOptions = Codegenerationoptions.generateproperties | Codegenerationoptions.generatenewasync;
Importer. Addservicedescription (description, NULL, NULL); Add a WSDL document.
CodeNamespace nmspace = new CodeNamespace (); Name space
Nmspace. Name = "Meswebservice";
CodeCompileUnit unit = new CodeCompileUnit ();
Unit. Namespaces.add (Nmspace);
Servicedescriptionimportwarnings warning = importer. Import (nmspace, unit);
CodeDomProvider Provider = Codedomprovider.createprovider ("CSharp");
CompilerParameters parameter = new CompilerParameters ();
Parameter. GenerateExecutable = false;
Parameter. outputassembly = "MyTest.dll";//The name of the output assembly
Parameter. Referencedassemblies.add ("System.dll");
Parameter. Referencedassemblies.add ("System.XML.dll");
Parameter. Referencedassemblies.add ("System.Web.Services.dll");
Parameter. Referencedassemblies.add ("System.Data.dll");
CompilerResults result = provider.compileassemblyfromdom (parameter, unit);
if (result. Errors.haserrors)
{
Display compilation error message
}
Assembly asm = Assembly.LoadFrom ("MyTest.dll");//Load the previously generated assembly
Type t = asm. GetType ("Meswebservice.service");
Object o = activator.createinstance (t);
MethodInfo method = T.getmethod ("GetUserInfo"),//getpersons is the name of the service side of the methods, you want to invoke the service side of any method can be changed here, it is best to encapsulate
DataSet ds = (DataSet) method. Invoke (o, new object[] {"CN04633"});
"Grooming" Dynamically loading web Services