动态编译与WS服务,有关系么?今天就乱弹一番,如何使用动态编译动态生成WS服务调用的代理类,然后通过这个代理类调用WS服务。
首先,动态编译这玩意在.NET里面是非常简单的,实际上只涉及到两个类型:CodeDomProvider以及CompilerParameters他们都位于System.CodeDom.Compiler命名空间。
以下代码可将源码动态编译为一个程序集:
CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
CompilerParameters codeParameters = new CompilerParameters();
codeParameters.GenerateExecutable = false; //编译为dll,如果为true则编译为exe
codeParameters.GenerateInMemory = true; //编译后的程序集保存到内存中
StringBuilder code = new StringBuilder();
//此处构造源代码
CompilerResults results = provider.CompileAssemblyFromSource(codeParameters, code.ToString());
Assembly assembly = null; //动态编译生成的程序集
if (!results.Errors.HasErrors)
{
assembly = results.CompiledAssembly;
}
获得assembly后,随后我们即可以通过反射获取程序集里面的类型,然后实例化,调用类型方法…
不过在此之前,我们得构造WS服务的代理类,它是什么样子的呢?我们使用WCF框架,创建服务代理类也是十分简单的,常见的代理类结构如下:
[ServiceContract(Namespace="http://tempuri.org/")]
public interface TestService
{
[OperationContract(Action = "http://tempuri.org/HelloWorld", ReplyAction = "http://tempuri.org/HelloWorldResponse")]
string HelloWorld();
}
public class TestServiceClient : ClientBase<TestService>, TestService
{
public TestServiceClient(Binding binding, EndpointAddress address) :
base(binding, address)
{
}
public string HelloWorld()
{
return base.Channel.HelloWorld();
}
}
所以,我们要动态构造出代理类源码,应该知道服务的命名空间、服务方法的Action地址、ReplyAction地址,当然还有服务方法的名称,返回类型,参数列表。这里,我们省略掉服务方法的参数列表,构造代理类,实际上就是一个字符串组装的问题,先创建一个类型,用于保存构造代理类所要用到的参数:
public class WebServiceParamaters
{
public string address;
public string Address
{
get { return address; }
set
{
address = value;
}
}
private string serviceNamespace;
public string ServiceNamespace
{
get { return serviceNamespace; }
set
{
serviceNamespace = value;
}
}
private string methodAction;
public string MethodAction
{
get { return methodAction; }
set
{
methodAction = value;
}
}
private string methodReplyAction;
public string MethodReplyAction
{
get { return methodReplyAction; }
set
{
methodReplyAction = value;
}
}
private string methodName;
public string MethodName
{
get { return methodName; }
set
{
methodName = value;
}
}
private string returnType;
public string ReturnType
{
get { return returnType; }
set
{
returnType = value;
}
}
}
好,现在我们只需要构造出代理类源码,然后动态编译出代理类的程序集,最后通过反射调用服务方法:
public class WebServiceProxyCreator
{
public Object WebServiceCaller(WebServiceParamaters parameters)
{
CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
CompilerParameters codeParameters = new CompilerParameters();
codeParameters.GenerateExecutable = false;
codeParameters.GenerateInMemory = true;
StringBuilder code = new StringBuilder();
CreateProxyCode(code, parameters);
CompilerResults results = provider.CompileAssemblyFromSource(codeParameters, code.ToString());
Assembly assembly = null;
if (!results.Errors.HasErrors)
{
assembly = results.CompiledAssembly;
}
Type clientType = assembly.GetType("RuntimeServiceClient");
ConstructorInfo ci = clientType.GetConstructor(new Type[] { typeof(Binding), typeof(EndpointAddress) });
BasicHttpBinding binding = new BasicHttpBinding(); //只演示传统的WebService调用
EndpointAddress address = new EndpointAddress(parameters.address);
Object client = ci.Invoke(new object[] { binding, address });
MethodInfo mi = clientType.GetMethod(parameters.MethodName);
Object result = mi.Invoke(client, null);
mi = clientType.GetMethod("Close"); //关闭代理
mi.Invoke(client, null);
return result;
}
public static void CreateProxyCode(StringBuilder code, WebServiceParamaters parameters)
{
code.AppendLine("using System;");
code.AppendLine("using System.ServiceModel;");
code.AppendLine("using System.ServiceModel.Channels;");
code.Append(@"[ServiceContract(");
if (!String.IsNullOrEmpty(parameters.ServiceNamespace))
{
code.Append("Namespace=\"").Append(parameters.ServiceNamespace).Append("\"");
}
code.AppendLine(")]");
code.AppendLine("public interface IRuntimeService");
code.AppendLine("{");
code.Append("[OperationContract(");
if (!String.IsNullOrEmpty(parameters.MethodAction))
{
code.Append("Action=\"").Append(parameters.MethodAction).Append("\"");
if (!String.IsNullOrEmpty(parameters.MethodReplyAction))
{
code.Append(", ");
}
}
if (!String.IsNullOrEmpty(parameters.MethodReplyAction))
{
code.Append("ReplyAction=\"").Append(parameters.MethodReplyAction).Append("\"");
}
code.AppendLine(")]");
code.Append(parameters.ReturnType).Append(" ");
code.Append(parameters.MethodName).AppendLine("();");
code.AppendLine("}");
code.AppendLine();
code.AppendLine("public class RuntimeServiceClient : ClientBase<IRuntimeService>, IRuntimeService");
code.AppendLine("{");
code.AppendLine("public RuntimeServiceClient(Binding binding, EndpointAddress address) :base(binding, address)");
code.AppendLine("{");
code.AppendLine("}");
code.Append("public ").Append(parameters.ReturnType).Append(" ");
code.Append(parameters.MethodName).AppendLine("()");
code.AppendLine("{");
code.Append("return base.Channel.").Append(parameters.MethodName).AppendLine("();");
code.AppendLine("}");
code.AppendLine("}");
}
}
注意,红色部分,由于代理类使用了WCF框架,所以编译时我们需要添加System.ServiceModel的引用,当然System.dll肯定是必须的,这里要注意,System.ServiceModel.dll应该保存到应用程序目录,否则动态编译时会引发异常,很简单,在工程引用中添加System.ServiceModel的引用,然后在属性中将拷贝到本地属性设置为true。
到此,我们就可以直接通过传入的服务地址、服务方法名称以及相关的命名空间,即可调用服务(尽管我们只能调用无参服务,并且尽管我们也只能调用使用BasicHttpBinding绑定的服务,这些限制的原因是…我懒,好吧,相信只要经过一点改动即可去掉这些限制)。
可惜,我们的程序还很傻:每次调用服务都需要去生成代码、编译、创建代理实例最后再调用,嗯…那就缓存吧:
在WebServiceParameters类中重写GetHashCode方法:
1: public override int GetHashCode()
2: {
3: return String.Concat(serviceNamespace, methodAction, methodReplyAction, methodName, returnType).GetHashCode();
4: }
5:
然后在WebServiceProxyCreator中加入缓存机制:
1: public class WebServiceProxyCreator
2: {
3: private static Dictionary<int, Type> proxyTypeCatch = new Dictionary<int, Type>();
4:
5: public Object WebServiceCaller(WebServiceParamaters parameters)
6: {
7: int key = parameters.GetHashCode();
8: Type clientType = null;
9: if (proxyTypeCatch.ContainsKey(key))
10: {
11: clientType = proxyTypeCatch[key];
12: Debug.WriteLine("使用缓存");
13: }
14: else
15: {
16:
17: CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
18: CompilerParameters codeParameters = new CompilerParameters();
19: codeParameters.GenerateExecutable = false;
20: codeParameters.GenerateInMemory = true;
21:
22: StringBuilder code = new StringBuilder();
23: CreateProxyCode(code, parameters);
24:
25: codeParameters.ReferencedAssemblies.Add("System.dll");
26: codeParameters.ReferencedAssemblies.Add("System.ServiceModel.dll");
27:
28: CompilerResults results = provider.CompileAssemblyFromSource(codeParameters, code.ToString());
29: Assembly assembly = null;
30: if (!results.Errors.HasErrors)
31: {
32: assembly = results.CompiledAssembly;
33: }
34:
35: clientType = assembly.GetType("RuntimeServiceClient");
36:
37: proxyTypeCatch.Add(key, clientType);
38: }
39: ConstructorInfo ci = clientType.GetConstructor(new Type[] { typeof(Binding), typeof(EndpointAddress) });
40: BasicHttpBinding binding = new BasicHttpBinding(); //只演示传统的WebService调用
41: EndpointAddress address = new EndpointAddress(parameters.address);
42: Object client = ci.Invoke(new object[] { binding, address });
43:
44: MethodInfo mi = clientType.GetMethod(parameters.MethodName);
45: Object result = mi.Invoke(client, null);
46: mi = clientType.GetMethod("Close"); //关闭代理
47: mi.Invoke(client, null);
48: return result;
49: }
50:
51: }