通过过代理调用 wcf服务 

using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.ServiceModel;
using System.Threading.Tasks; namespace wcfproxy.Common
{
public interface IProxy
{
T SetProxy<T>();
}
public class Proxy : IProxy
{
private readonly IProxyOptions _proxyOptions; public Proxy(IOptionsSnapshot<IProxyOptions> proxyOptionsAccessor)
{
_proxyOptions = proxyOptionsAccessor.Value;
} public T SetProxy<T>()
{
return GetHttpsProxy<T>(); }
//HTTPS 请求
private T GetHttpsProxy<T>() { var bindingHttps = new BasicHttpsBinding();
bindingHttps.UseDefaultWebProxy = false;
string proxyAdress = _proxyOptions.ProcyAdress; bindingHttps.ProxyAddress = new Uri(proxyAdress);//代理
bindingHttps.Security.Mode = BasicHttpsSecurityMode.Transport;
bindingHttps.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
bindingHttps.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.Basic; var endpointAddress = new EndpointAddress(_proxyOptions.EndpointAddress); var factory = new ChannelFactory<T>(bindingHttps, endpointAddress);
//安全证书
factory.Credentials.ServiceCertificate.SslCertificateAuthentication = new System.ServiceModel.Security.X509ServiceCertificateAuthentication();
factory.Credentials.ServiceCertificate.SslCertificateAuthentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None; var serviceClient = (T)factory.CreateChannel(); return serviceClient;
}
//http 请求
private T GetHttpProxy<T>() {
var binding = new BasicHttpBinding();
binding.UseDefaultWebProxy = false; string proxyAdress = _proxyOptions.ProcyAdress; binding.ProxyAddress = new Uri(proxyAdress);//代理
binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.Basic; var endpointAddress = new EndpointAddress(_proxyOptions.EndpointAddress); var factory = new ChannelFactory<T>(binding, endpointAddress); var serviceClient = (T)factory.CreateChannel(); return serviceClient;
} }
public static class Util
{ public static void SetCertificatePolicy()
{
ServicePointManager.ServerCertificateValidationCallback += RemoteCertificateValidate;
} private static bool RemoteCertificateValidate( object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error) { // trust any certificate!!! System.Console.WriteLine("Warning, trust any certificate"); return true; } }
}
   public class IProxyOptions
{
/// <summary>
/// 代理地址
/// </summary>
public string ProcyAdress { get; set; } /// <summary>
/// EndpointAddress
/// </summary>
public string EndpointAddress { get; set; }
}

wcf 代理实例的更多相关文章

  1. WCF分布式开发步步为赢(9):WCF服务实例激活类型编程与开发

    .Net Remoting的激活方式也有三种:SingleTon模式.SingleCall模式.客户端激活方式,WCF服务实例激活类型包括三种方式:单调服务(Call Service),会话服务(Se ...

  2. WCF代理是怎么工作的?用代码说话

    1.WCF生成代理的方式 2.WCF代理原理 第一个问题引用 一篇Robin's博文[WCF生成客户端对象方式解析] 讲述了创建客户端服务对象的方法 1.代理构造法 a.开启服务后,添加服务引用 b. ...

  3. WCF小实例以及三种宿主

    WCF小实例以及三种宿主 最近一直在学习WCF相关知识,下面将通过一个小实例对所学的知识进行简单的回顾:本实例是一个简单三层操作数据库,并且也简单实现的三种宿主(控制台宿主,IIS宿主以及Window ...

  4. linux之反向代理,反向代理实例,负载均衡实例

    目录 nginx反向代理 1. 概述 2. 反向代理服务器的工作原理 (1)作为内容服务器的替身 (2)作为内容服务器的负载均衡器 二. nginx反向代理实例 1.前期准备 2.代理服务器配置 3. ...

  5. WCF之实例模型

    PerCall. 为每次调用创建新的服务对象. 内存使用量最小,增加整体的吞吐量. 状态不保存,服务实例及时释放. 单例的状态没有办法保存.所以应使用数据库或者文件或者全局变量来保存服务实例的状态.如 ...

  6. WCF揭秘(一)——简单的WCF开发实例

    一.WCF是什么 WCF是微软为了实现各个开发平台之间的无疑缝连接而开发一种崭新工具,它是为分布式处理而开发.WCF将DCOM.Remoting.Web Service.WSE.MSMQ.AJAX服务 ...

  7. WCF - 服务实例管理模式

    WCF 提供了三种实例上下文模式:PreCall.PreSession 以及 Single.开发人员通过 ServiceBehavior.InstanceContextMode 就可以很容易地控制服务 ...

  8. 基于net.tcp的WCF配置实例解析(转)

    http://www.cnblogs.com/scy251147/archive/2012/11/23/2784902.html 原文 本文主要通过文件配置来讲解如何编写一个基于net.tcp的Win ...

  9. UI层调用WCF服务实例(源码)

    WCF原理性的东西,暂时还没有深入研究,只是在公司的项目中使用到了,会调用,然后再多做了一些了解,现在将它抽出来了一个小实例,写了一个WCF的demo. 我写的这个WCF.Demo主要包括数据契约和服 ...

随机推荐

  1. [Javascript] Format console.log with CSS and String Template Tags

    The Chrome console allows you to format messages using CSS properties. This lesson walks you through ...

  2. JVM 调优 —— OutOfMemory

    零. 简单介绍 OutOfMemory 意思就是须要申请更大的内存, 可是内存限制无法申请到须要的内存. 一. 解决方法 基本上解决方向有两种: 检查程序是否有问题. 是不是写死循环不停地创建并持有对 ...

  3. NET中的System.Transactions(分布式事务)

    NET中的System.Transactions(分布式事务),当项目开发完成以后,调用的时候遇到了MSDTC的问题,在查阅了相关资料后将这个问题解决了,大致的问题主要是使用了分布式事务,而无法访问到 ...

  4. Haproxy解析

    简单介绍 HAProxy是一款提供高可用性.负载均衡以及基于TCP和HTTP应用的代理软件,HAProxy是全然免费的.借助HAProxy能够高速而且可靠的提供基于TCP和HTTP应用的代理解决方式. ...

  5. [NPM] Run npm scripts in series

    After creating several npm script it becomes useful to run multiple scripts back-to-back in series. ...

  6. 关闭 You need to use a Theme.AppCompat theme (or descendant) with this activity解决方法

    当我的MainActivity继承自v7包中的ActionBarActivity或者AppCompatActivity时,如果在style.xml文件中指定MainActivity所使用的样式如下: ...

  7. JS调用ATL COM中的C++接口的做法

    作者:朱金灿 来源:http://blog.csdn.net/clever101 首先创建一个ATL COM对象,其过程参考下面文章: C#调用ATLCOM 其实给COM对象添加方法和属性可以不用界面 ...

  8. Jquery前端分页插件pagination使用

    插件描述:JqueryPagination是一个轻量级的jquery分页插件.只需几个简单的配置就可以生成分页控件.并且支持ajax获取数据,自定义请求参数,提供多种方法,事件和回调函数,功能全面的分 ...

  9. Android中获取当前位置的使用步骤

    在Android中得到当前位置的步骤 1.在AndroidManifest.xml中声明权限 android.permission.ACCESS_FINE_LOCATION(或者android.per ...

  10. sqoop 创建job时 java.lang.NoClassDefFoundError: org/json/JSONObject

    缺少jar包 到maven仓库下载json in java 之后把jar包上传至sqoop的lib目录下即可