动态获取Asmx服务的Web方法 [英] Get web methods dynamically for an asmx service

查看:113
本文介绍了动态获取Asmx服务的Web方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有大量的asmx服务.我想给用户一个带有文本框的页面来输入服务URL,例如 http://abc.win .com/myservice/customerdata.asmx .当用户点击加载"按钮时,我会动态地将所有Web方法添加到下拉列表中.我需要一些提示: 1.如何动态获取所有方法? 2.如何获取所选方法的SOAP请求?这样,我们就可以用实际值替换参数值了吗?

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?

感谢您的帮助.

推荐答案

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);
}

这篇关于动态获取Asmx服务的Web方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆