this is a sample about how to create webservice dynamicly. But at first, you must create a webservice on your local machine,
such as:http://localhost:2704/TestWebService/Service.asmx
OK, let's go !
code for default.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Dynamic Web Service Sample</title>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td>WebService URL</td>
<td><asp:TextBox ID="wurl" runat="server">http://localhost:2704/TestWebService/Service.asmx</asp:TextBox></td>
</tr>
<tr>
<td>WebService Name</td>
<td><asp:TextBox ID="wname" runat="server">Service</asp:TextBox></td>
</tr>
<tr>
<td>Method Name</td>
<td><asp:TextBox ID="wmname" runat="server">RevSMS</asp:TextBox></td>
</tr>
<tr>
<td>Method Param0</td>
<td><asp:TextBox ID="wmparam0" runat="server">SYS06*</asp:TextBox></td>
</tr>
<tr>
<td>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /></td>
</tr>
<tr>
<td colspan="4"><asp:TextBox ID="result" runat="server" TextMode="MultiLine" Width="100%" Height="100px"></asp:TextBox></td>
</tr>
</table>
</form>
</body>
</html>
code for default.aspx.cs
public partial class _Default : System.Web.UI.Page
{
DynamicWebService we = new DynamicWebService("http://localhost:2704/TestWebService/Service.asmx", "Service");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
we = new DynamicWebService("http://localhost:2704/TestWebService/Service.asmx", "Service");
result.Text = we.InvokeMethod("HelloWorld", null).ToString();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(wurl.Text)
&& !string.IsNullOrEmpty(wname.Text)
&& !string.IsNullOrEmpty(wmname.Text))
{
we = new DynamicWebService(wurl.Text, wname.Text);
result.Text = we.InvokeMethod(wmname.Text, string.IsNullOrEmpty(wmparam0.Text) ? null : new object[] { wmparam0.Text }).ToString();
}
}
}
code for DynamicWebService.cs
using System.Net;//for webclient
using System.Reflection;// for assembly
using System.IO;//for Stream
using System.Web.Services.Description;//for servicedescription
using System.CodeDom;//for CodeNameSpace
using System.CodeDom.Compiler;//for CodeDomProvider
/// <summary>
/// Summary description for DynamicWebService
/// </summary>
public class DynamicWebService
{
public DynamicWebService()
{
//
// TODO: Add constructor logic here
//
}
Assembly service = null;
string _url;
string _name;
public string ServiceURL
{
get
{
if (!_url.ToUpper().EndsWith("?WSDL"))
{
_url += "?WSDL";
}
return _url;
}
}
public string ServiceClassName
{
get { return _name; }
}
public DynamicWebService(string serviceURL,string serviceClassName)
{
_url = serviceURL;
_name = serviceClassName;
GetService();
}
public void GetService()
{
// 1. 使用 WebClient 下载 WSDL 信息。
WebClient web = new WebClient();
Stream stream = web.OpenRead(ServiceURL);
// 2. 创建和格式化 WSDL 文档。
ServiceDescription description = ServiceDescription.Read(stream);
// 3. 创建客户端代理代理类。
ServiceDescriptionImporter importer = new ServiceDescriptionImporter();
importer.ProtocolName = "Soap";// 指定访问协议。
importer.Style = ServiceDescriptionImportStyle.Client;// 生成客户端代理。
importer.CodeGenerationOptions = System.Xml.Serialization.CodeGenerationOptions.GenerateProperties | System.Xml.Serialization.CodeGenerationOptions.GenerateNewAsync;
importer.AddServiceDescription(description, null, null);// 添加 WSDL 文档。
// 4. 使用 CodeDom 编译客户端代理类。
CodeNamespace nmspace = new CodeNamespace();// 为代理类添加命名空间,缺省为全局空间。
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.GenerateInMemory = true;
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)
{
service = result.CompiledAssembly;
}
else
{
throw new Exception("WebService have errors. ");
}
}
public object InvokeMethod(string MethodName,object[] parameters)
{
// 如果在前面为代理类添加了命名空间,此处需要将命名空间添加到类型前面。
System.Type t = service.GetType(ServiceClassName);
object o = Activator.CreateInstance(t);
MethodInfo method = t.GetMethod(MethodName);
return method.Invoke(o, parameters);
}
}
Tuesday, November 11, 2008
Subscribe to:
Post Comments (Atom)


No comments:
Post a Comment