通过过代理调用 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] Combine Objects with Object.assign and Lodash merge

    Learn how to use Object.assign to combine multiple objects together. This pattern is helpful when wr ...

  2. 基于bootstrap的富文本框——wangEditor【欢迎增加开发】

    先来一张效果图: 01. 引言 老早就開始研究富文本框的东西,在写完<深入理解javascript原型与闭包>之后,就想着要去做一个富文本框的插件的样例. 如今网络上开源的富文本框插件许多 ...

  3. 【前端统计图】hcharts实现堆叠柱形图(与后台数据交互)

    原型图类似如下: 图片.png <!DOCTYPE > <html> <head> <meta charset="utf-8">&l ...

  4. 配置Apache支持PHP5

    环境:window 7.Apache/2.2.19 (Win32) PHP/5.2.9-1 1.配置server名称 将里面的 #ServerName localhost:80 凝视去掉就可以. 又一 ...

  5. Android Material Design 5.0 PickerDialog

    5.0系统下的时间选择器效果图: 该项目兼容到3.0下面所以用第三方开源项目:actionbarsherlock,动画效果兼容:nineoldandroids-2.4.0.jar,格式转换器:joda ...

  6. SQL Server高速生成SQL增删改查语句

    你还在手写程序生成SQL语句吗?你还在为由于马虎出错的SQL语句而感到无语吗?你还在为不知如何表达复杂的SQL语句而纠结吗?假设你的回答为"是".那你就OUT啦.快来试试应用SQL ...

  7. [Ramda] Create an Array From a Seed Value with Ramda's unfold

    In this lesson we'll look at how you can use Ramda's unfold function to generate a list of values ba ...

  8. 一个完整的Erlang应用

    http://blog.chinaunix.net/uid-25876834-id-3308693.html 这里介绍构建一个完整的Erlang/OTP应用的例子,最后还给出了一个在实际生成环境中,如 ...

  9. 学习鸟哥的Linux私房菜笔记(4)——文件

    一.检查文件 用ls -l以长模式查看文件的详细信息,包含当前目录的硬盘使用空间.文件类型.文件权限.硬连接数.文件拥有者.文件所属组.文件大小.更改时间.文件名称. 用file检查文件类型 由于li ...

  10. vector, list, deque的选用(vector适用少量对象,list适用大量对象),以及效率问题

    如何选择这三个容器中哪一个,应根据你的需要而定,一般应遵循下面的原则:  1.如果你需要高效的随机存取,而不在乎插入和删除的效率,使用vector  2.如果你需要大量的插入和删除,而不关心随机存取( ...