public class DynamicProxy
{
public DynamicProxy(Uri uri)
{
this.uri = uri;
ServicePointManager.ServerCertificateValidationCallback = CheckValidationResult;
}
private Uri uri = null;
public string serviceName;
public Assembly proxyAssembly;
private ServiceDescription serviceDesc;
public ServiceDescription ServiceDescription
{
get {
if (serviceDesc == null)
{
serviceDesc = GetServiceDescription();
}
return serviceDesc;
}
}
private ServiceDescription GetServiceDescription()
{
if (uri == null) return null;
WebRequest webReq = WebRequest.Create(uri);
Stream reqStrm = webReq.GetResponse().GetResponseStream();
return ServiceDescription.Read(reqStrm);
}
public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
if ((sslPolicyErrors == SslPolicyErrors.None) ||
((sslPolicyErrors & SslPolicyErrors.RemoteCertificateChainErrors) == SslPolicyErrors.RemoteCertificateChainErrors) ||
((sslPolicyErrors & SslPolicyErrors.RemoteCertificateNameMismatch) == SslPolicyErrors.RemoteCertificateNameMismatch) ||
((sslPolicyErrors & SslPolicyErrors.RemoteCertificateNotAvailable) == SslPolicyErrors.RemoteCertificateNotAvailable))
return true;
return false;
}
public MethodInfo[] GetWebMethods()
{
serviceName = this.ServiceDescription.Services[0].Name;
if (proxyAssembly == null)
{
proxyAssembly = GenerateProxyAssembly();
if (proxyAssembly == null)
throw new InvalidOperationException("Unable to generate the proxy assembly.");
}
//Use reflection to find the methods on the service requested by the user
Type service = proxyAssembly.GetType(serviceName);
return service.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.IgnoreCase |
BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.Public);
}
private Assembly GenerateProxyAssembly()
{
//Import the information about the service
ServiceDescriptionImporter servImport = new ServiceDescriptionImporter();
servImport.AddServiceDescription(this.ServiceDescription, String.Empty, String.Empty);
servImport.ProtocolName = "Soap";
servImport.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties;
//Set up to generate the code
CodeNamespace ns = new CodeNamespace();
CodeCompileUnit ccu = new CodeCompileUnit();
ccu.Namespaces.Add(ns);
ServiceDescriptionImportWarnings warnings = servImport.Import(ns, ccu);
//If we didn't find any methods there will be nothing to call later so just stop now.
if ((warnings == ServiceDescriptionImportWarnings.NoCodeGenerated) ||
(warnings == ServiceDescriptionImportWarnings.NoMethodsGenerated)) return null;
CSharpCodeProvider prov = new CSharpCodeProvider();
//Generate the code
StringWriter sw = new StringWriter(CultureInfo.CurrentCulture);
prov.GenerateCodeFromNamespace(ns, sw, null);
//Create the assembly
CompilerParameters param = new CompilerParameters(new String[] { "System.dll", "System.Xml.dll", "System.Web.Services.dll", "System.Data.dll" });
param.GenerateExecutable = false;
param.GenerateInMemory = false;
param.TreatWarningsAsErrors = false;
param.WarningLevel = 4;
CompilerResults results = new CompilerResults(null);
results = prov.CompileAssemblyFromSource(param, sw.ToString());
return results.CompiledAssembly;
}
}
创建动态代理
最新推荐文章于 2021-12-04 11:37:53 发布