C# 调用 WebServices 接口

WebClient webClient = new WebClient();
Stream stream = webClient.OpenRead(System.Configuration.ConfigurationManager .AppSettings[ "wsServer"]);
ServiceDescription serviceDescription = ServiceDescription .Read(stream);
ServiceDescriptionImporter serviceDescriptionImporter = new ServiceDescriptionImporter ();
serviceDescriptionImporter.ProtocolName = "Soap" ;
serviceDescriptionImporter.Style = ServiceDescriptionImportStyle .Client;
serviceDescriptionImporter.CodeGenerationOptions = CodeGenerationOptions .GenerateProperties | CodeGenerationOptions .GenerateNewAsync;
serviceDescriptionImporter.AddServiceDescription(serviceDescription, null , null );
CodeNamespace codeNamespace = new CodeNamespace();
CodeCompileUnit codeCompileUnit = new CodeCompileUnit();
codeCompileUnit.Namespaces.Add(codeNamespace);
ServiceDescriptionImportWarnings serviceDescriptionImportWarnings = serviceDescriptionImporter.Import(codeNamespace, codeCompileUnit);
CodeDomProvider codeDomProvider = CodeDomProvider .CreateProvider( "CSharp");
CompilerParameters compilerParameters = new CompilerParameters();
compilerParameters.GenerateExecutable = false ;
compilerParameters.GenerateInMemory = true ;
compilerParameters.ReferencedAssemblies.Add( "System.dll" );
compilerParameters.ReferencedAssemblies.Add( "System.XML.dll" );
compilerParameters.ReferencedAssemblies.Add( "System.Web.Services.dll" );
compilerParameters.ReferencedAssemblies.Add( "System.Data.dll" );
CompilerResults compilerResults = codeDomProvider.CompileAssemblyFromDom(compilerParameters, codeCompileUnit);

2.调用指定接口中的方法(传递参数等):

if(!compilerResults.Errors.HasErrors){
	Assemblyassembly = compilerResults.CompiledAssembly;
	Type type = assembly.GetType("IWebServicesService");
	objecto =Activator.CreateInstance(type);
	MethodInfo methodInfo = type.GetMethod("login");
	object[] wsObject =newobject[] {this.loginName.Text,this.password.Text };
	string result = methodInfo.Invoke(o, wsObject).ToString();
	if(result.Equals("SUCCESS")){
		return true;
	}
}