wcf 代理实例
通过过代理调用 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 代理实例的更多相关文章
- WCF分布式开发步步为赢(9):WCF服务实例激活类型编程与开发
.Net Remoting的激活方式也有三种:SingleTon模式.SingleCall模式.客户端激活方式,WCF服务实例激活类型包括三种方式:单调服务(Call Service),会话服务(Se ...
- WCF代理是怎么工作的?用代码说话
1.WCF生成代理的方式 2.WCF代理原理 第一个问题引用 一篇Robin's博文[WCF生成客户端对象方式解析] 讲述了创建客户端服务对象的方法 1.代理构造法 a.开启服务后,添加服务引用 b. ...
- WCF小实例以及三种宿主
WCF小实例以及三种宿主 最近一直在学习WCF相关知识,下面将通过一个小实例对所学的知识进行简单的回顾:本实例是一个简单三层操作数据库,并且也简单实现的三种宿主(控制台宿主,IIS宿主以及Window ...
- linux之反向代理,反向代理实例,负载均衡实例
目录 nginx反向代理 1. 概述 2. 反向代理服务器的工作原理 (1)作为内容服务器的替身 (2)作为内容服务器的负载均衡器 二. nginx反向代理实例 1.前期准备 2.代理服务器配置 3. ...
- WCF之实例模型
PerCall. 为每次调用创建新的服务对象. 内存使用量最小,增加整体的吞吐量. 状态不保存,服务实例及时释放. 单例的状态没有办法保存.所以应使用数据库或者文件或者全局变量来保存服务实例的状态.如 ...
- WCF揭秘(一)——简单的WCF开发实例
一.WCF是什么 WCF是微软为了实现各个开发平台之间的无疑缝连接而开发一种崭新工具,它是为分布式处理而开发.WCF将DCOM.Remoting.Web Service.WSE.MSMQ.AJAX服务 ...
- WCF - 服务实例管理模式
WCF 提供了三种实例上下文模式:PreCall.PreSession 以及 Single.开发人员通过 ServiceBehavior.InstanceContextMode 就可以很容易地控制服务 ...
- 基于net.tcp的WCF配置实例解析(转)
http://www.cnblogs.com/scy251147/archive/2012/11/23/2784902.html 原文 本文主要通过文件配置来讲解如何编写一个基于net.tcp的Win ...
- UI层调用WCF服务实例(源码)
WCF原理性的东西,暂时还没有深入研究,只是在公司的项目中使用到了,会调用,然后再多做了一些了解,现在将它抽出来了一个小实例,写了一个WCF的demo. 我写的这个WCF.Demo主要包括数据契约和服 ...
随机推荐
- jquery-6 jquery属性选择器
jquery-6 jquery属性选择器 一.总结 一句话总结:jquery操作就是选择器加jquery对象的各种方法. 1.大量操作样式用什么方式? 大批量样式通过加类和减类完成 2.jquery中 ...
- Opencv Sift算子特征提取与匹配
SIFT算法的过程实质是在不同尺度空间上查找特征点(关键点),用128维方向向量的方式对特征点进行描述,最后通过对比描述向量实现目标匹配. 概括起来主要有三大步骤: 1.提取关键点: 2.对关键点附加 ...
- sql server操作远程数据库
--连接远程数据库 EXEC sp_addlinkedserver '服务器ip',N'SQL Server' --登录远程服务器 EXEC sp_addlinkedsrvlogin '服务器ip', ...
- php中如何获取数组长度
php获取数组的长度的方法 一.总结 一句话总结:count方法和sizeof方法 二.php获取数组的长度的方法 php获取数组长度的方法: 一). 获取一维数组的方法: 1.count.sizeo ...
- [CortexM0--stm32f0308]Option Byte
问题描写叙述 option byte,算是IC中比較简单的功能,就是用户能够写入数据,对IC的某些功能进行配置.而IC在reset时,会载入当中的内容,进行推断,从而使用户的配置生效. option ...
- [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 ...
- [SVG] Optimize SVGs for Better Performance using svgo
Just like a bitmap image, you can compress an SVG by removing various pieces of code that aren’t nec ...
- 【物理】概念的理解 —— Phase(相位)
Phase is the position of a point in time (an instant) on a waveform cycle. 相位指的是波形周期中点在某一时刻的位置.Phase ...
- PyCharm 重构(refactor)快捷键
提取变量(比如一个函数会返回一个变量值):ctrl + alt + v(v:variable) 将某段代码封装为一个函数(函数+函数调用):ctrl + alt + m(m:method)
- Vue Router的官方示例改造
基于Vue Router 2018年8月的官方文档示例,改造一下,通过一个最简单的例子,解决很多初学者的一个困惑. 首先是官方文档示例代码 <!DOCTYPE html> <html ...