Get web methods dynamically for an asmx service

主宰稳场 提交于 2019-12-13 05:22:15

问题


We have number of asmx services. I want to give an user a page with a textbox to input service url like http://abc.win.com/myservice/customerdata.asmx. When user hit "Load" button, dynamically I add all the web methods to the dropdown. I need some pointers: 1. How to dynamically get all the methods? 2. How can I get the SOAP request for the method selected? So that, we can replace the parameter values with actual values?

Appreciate your help.


回答1:


Kuul13 : You need to modify your webservice URL like http://abc.win.com/myservice/customerdata.asmx?wsdl when user clicks on load button. then you can use we "ServiceDescription" class to get wsdl description and then iterate that to get method names in 'WebMethodInfoCollection' class.

To get SOAP request you need to use SOAPExtension class.This will give you SOAP Request and Response XML.Refere this link for that : http://blog.encoresystems.net/articles/how-to-capture-soap-envelopes-when-consuming-a-web-service.aspx?www.microsoft.com

For dynamically calling webservice look a this V.Good article http://www.codeproject.com/KB/webservices/webservice_.aspx

Please reply me for any comment.




回答2:


System.Net.WebClient client = new System.Net.WebClient();
System.IO.Stream stream = client.OpenRead("http://www.webservicex.net/globalweather.asmx?wsdl");          
ServiceDescription description = ServiceDescription.Read(stream);  

ServiceDescriptionImporter importer = new ServiceDescriptionImporter();
importer.ProtocolName = "Soap12";  
importer.AddServiceDescription(description, null, null);  
importer.Style = ServiceDescriptionImportStyle.Client;     
importer.CodeGenerationOptions = System.Xml.Serialization.CodeGenerationOptions.GenerateProperties;

CodeNamespace nmspace = new CodeNamespace();
CodeCompileUnit unit1 = new CodeCompileUnit();
unit1.Namespaces.Add(nmspace);

ServiceDescriptionImportWarnings warning = importer.Import(nmspace, unit1);
if (warning == 0)
{              
    CodeDomProvider provider1 = CodeDomProvider.CreateProvider("CSharp");   

    string[] assemblyReferences = new string[2] { "System.Web.Services.dll", "System.Xml.dll" };
    CompilerParameters parms = new CompilerParameters(assemblyReferences);
    CompilerResults results = provider1.CompileAssemblyFromDom(parms, unit1);

    object[] args = new object[1];
    args[0] = "India";          

    object wsvcClass = results.CompiledAssembly.CreateInstance("GlobalWeather");
    MethodInfo mi = wsvcClass.GetType().GetMethod("GetCitiesByCountry");

    RegExpForCountryCity(mi.Invoke(wsvcClass, args).ToString());
}
else
{                
    Console.WriteLine("Warning: " + warning);
}


void RegExpForCountryCity(string strHTML)
{
    Regex qariRegex = new Regex(@"<Table>\s*<Country>(?<Country>[\s\S]*?)</Country>\s*<City>(?<City>[\s\S]*?)</City>\s*</Table>", RegexOptions.IgnoreCase | RegexOptions.Multiline);

    MatchCollection mc = qariRegex.Matches(strHTML);

    string strCountryCity = "";

    for (int i = 0; i < mc.Count; i++)
    {
        if (string.IsNullOrEmpty(strCountryCity))
            strCountryCity = "Country: " + "<b>" + mc[i].Groups["Country"].Value + "</b>" + " " + "City: " + "<b>" + mc[i].Groups["City"].Value + "</b>" + "</br>";
        else
            strCountryCity += "</br>" + "Country: " + "<b>" + mc[i].Groups["Country"].Value + "</b>" + " " + "City: " + "<b>" + mc[i].Groups["City"].Value + "</b>" + "</br>";
    }
    Response.Write(strCountryCity);
}


来源:https://stackoverflow.com/questions/5278905/get-web-methods-dynamically-for-an-asmx-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!