動態編譯與WS服務
首先
以下代碼可將源碼動態編譯為一個程序集
動態編譯
CodeDomProvider provider = CodeDomProvider
CompilerParameters codeParameters = new CompilerParameters();
codeParameters
codeParameters
StringBuilder code = new StringBuilder();
//此處構造源代碼
CompilerResults results = provider
Assembly assembly = null; //動態編譯生成的程序集
if (!results
{
assembly = results
}
獲得assembly後
不過在此之前
服務調用代理類
[ServiceContract(Namespace="]
public interface TestService
{
[OperationContract(Action = "
string HelloWorld();
}
public class TestServiceClient : ClientBase<TestService>
{
public TestServiceClient(Binding binding
base(binding
{
}
public string HelloWorld()
{
return base
}
}
所以
服務代理類構造參數
復制代碼 代碼如下: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;
}
}
}
好
WebServiceProxyCreator
public class WebServiceProxyCreator
{
public Object WebServiceCaller(WebServiceParamaters parameters)
{
CodeDomProvider provider = CodeDomProvider
CompilerParameters codeParameters = new CompilerParameters();
codeParameters
codeParameters
StringBuilder code = new StringBuilder();
CreateProxyCode(code
codeParameters
codeParameters
CompilerResults results = provider
Assembly assembly = null;
if (!results
{
assembly = results
}
Type clientType = assembly
ConstructorInfo ci = clientType
BasicHttpBinding binding = new BasicHttpBinding(); //只演示傳統的WebService調用
EndpointAddress address = new EndpointAddress(parameters
Object client = ci
MethodInfo mi = clientType
Object result = mi
mi = clientType
mi
return result;
}
public static void CreateProxyCode(StringBuilder code
{
code
code
code
code
if (!String
{
code
}
code
code
code
code
if (!String
{
code
if (!String
{
code
}
}
if (!String
{
code
}
code
code
code
code
code
code
code
code
code
code
code
code
code
code
code
code
}
}
注意
到此
可惜
在WebServiceParameters類中重寫GetHashCode方法
public override int GetHashCode()
{
return String
}
然後在WebServiceProxyCreator中加入緩存機制
public class WebServiceProxyCreator
{
private static Dictionary<int
public Object WebServiceCaller(WebServiceParamaters parameters)
{
int key = parameters
Type clientType = null;
if (proxyTypeCatch
{
clientType = proxyTypeCatch[key];
Debug
}
else
{
CodeDomProvider provider = CodeDomProvider
CompilerParameters codeParameters = new CompilerParameters();
codeParameters
codeParameters
StringBuilder code = new StringBuilder();
CreateProxyCode(code
codeParameters
codeParameters
CompilerResults results = provider
Assembly assembly = null;
if (!results
{
assembly = results
}
clientType = assembly
proxyTypeCatch
}
ConstructorInfo ci = clientType
BasicHttpBinding binding = new BasicHttpBinding(); //只演示傳統的WebService調用
EndpointAddress address = new EndpointAddress(parameters
Object client = ci
MethodInfo mi = clientType
Object result = mi
mi = clientType
mi
return result;
}
}
From:http://tw.wingwit.com/Article/program/net/201311/14381.html