创建动态代理

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值