zoukankan      html  css  js  c++  java
  • 代理模式

    WCF 中用到代理了? 客户端是这个样子:父类也贴出来了

      public partial class Service1Client : System.ServiceModel.ClientBase<WCF代理客户端.ServiceReference1.IService1>, WCF代理客户端.ServiceReference1.IService1 {
            
            public Service1Client() {
            }
            
            public Service1Client(string endpointConfigurationName) : 
                    base(endpointConfigurationName) {
            }
            
            public Service1Client(string endpointConfigurationName, string remoteAddress) : 
                    base(endpointConfigurationName, remoteAddress) {
            }
            
            public Service1Client(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) : 
                    base(endpointConfigurationName, remoteAddress) {
            }
            
            public Service1Client(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : 
                    base(binding, remoteAddress) {
            }
            
            public void DoWork() {
                base.Channel.DoWork();
            }
            
            public System.Threading.Tasks.Task DoWorkAsync() {
                return base.Channel.DoWorkAsync();
            }
        }

     
     //
        // 摘要:
        //     提供基实现,用于创建可调用服务的 Windows Communication Foundation (WCF) 客户端对象。
        //
        // 类型参数:
        //   TChannel:
        //     用于连接服务的通道。
        public abstract class ClientBase<TChannel> : ICommunicationObject, IDisposable where TChannel : class
        {
            //
            // 摘要:
            //     使用应用程序配置文件中的默认目标终结点初始化 System.ServiceModel.ClientBase`1 类的新实例。
            //
            // 异常:
            //   T:System.InvalidOperationException:
            //     配置文件中没有默认的终结点信息,或者文件中有多个终结点,或者没有配置文件。
            protected ClientBase();
            //
            // 摘要:
            //     使用应用程序配置文件中由 System.ServiceModel.ClientBase`1 指定的配置信息来初始化 endpointConfigurationName
            //     类的新实例。
            //
            // 参数:
            //   endpointConfigurationName:
            //     应用程序配置文件中的终结点的名称。
            //
            // 异常:
            //   T:System.ArgumentNullException:
            //     指定的终结点信息为 null。
            //
            //   T:System.InvalidOperationException:
            //     无法找到终结点或者终结点协定无效。
            protected ClientBase(string endpointConfigurationName);
            //
            // 摘要:
            //     新实例初始化 System.ServiceModel.ClientBase`1 类使用指定 System.ServiceModel.Description.ServiceEndpoint。
            //
            // 参数:
            //   endpoint:
            //     允许客户端查找并与服务通信的服务的终结点。
            protected ClientBase(ServiceEndpoint endpoint);
            //
            // 摘要:
            //     通过使用 System.ServiceModel.ClientBase`1 作为双工对话中的回调对象来初始化 callbackInstance 类的新实例。
            //
            // 参数:
            //   callbackInstance:
            //     客户端应用程序用以侦听来自所连接服务的消息的回调对象。
            //
            // 异常:
            //   T:System.ArgumentNullException:
            //     回调实例为 null。
            //
            //   T:System.InvalidOperationException:
            //     配置文件中没有默认的终结点信息,或者文件中有多个终结点,或者没有配置文件。
            protected ClientBase(InstanceContext callbackInstance);
            //
            // 摘要:
            //     初始化 System.ServiceModel.ClientBase`1 类的新实例。
            //
            // 参数:
            //   endpointConfigurationName:
            //     应用程序配置文件中的终结点的名称。
            //
            //   remoteAddress:
            //     服务的地址。
            //
            // 异常:
            //   T:System.ArgumentNullException:
            //     终结点为 null。
            //
            //   T:System.ArgumentNullException:
            //     远程地址为 null。
            //
            //   T:System.InvalidOperationException:
            //     无法找到终结点或者终结点协定无效。
            protected ClientBase(string endpointConfigurationName, string remoteAddress);
            //
            // 摘要:
            //     使用指定的目标地址和终结点信息初始化 System.ServiceModel.ClientBase`1 类的新实例。
            //
            // 参数:
            //   endpointConfigurationName:
            //     应用程序配置文件中的终结点的名称。
            //
            //   remoteAddress:
            //     服务的地址。
            //
            // 异常:
            //   T:System.ArgumentNullException:
            //     终结点为 null。
            //
            //   T:System.ArgumentNullException:
            //     远程地址为 null。
            //
            //   T:System.InvalidOperationException:
            //     无法找到终结点或者终结点协定无效。
            protected ClientBase(string endpointConfigurationName, EndpointAddress remoteAddress);
            //
            // 摘要:
            //     使用指定的绑定和目标地址初始化 System.ServiceModel.ClientBase`1 类的新实例。
            //
            // 参数:
            //   binding:
            //     用于调用服务的绑定。
            //
            //   remoteAddress:
            //     服务终结点的地址。
            //
            // 异常:
            //   T:System.ArgumentNullException:
            //     绑定为 null。
            //
            //   T:System.ArgumentNullException:
            //     远程地址为 null。
            protected ClientBase(Binding binding, EndpointAddress remoteAddress);
            //
            // 摘要:
            //     使用指定的回调服务和终结点配置信息初始化 System.ServiceModel.ClientBase`1 类的新实例。
            //
            // 参数:
            //   callbackInstance:
            //     客户端用以侦听来自所连接服务的消息的回调对象。
            //
            //   endpointConfigurationName:
            //     应用程序配置文件中的终结点的名称。
            //
            // 异常:
            //   T:System.ArgumentNullException:
            //     回调实例为 null。
            //
            //   T:System.ArgumentNullException:
            //     终结点为 null。
            //
            //   T:System.InvalidOperationException:
            //     无法找到终结点或者终结点协定无效。
            protected ClientBase(InstanceContext callbackInstance, string endpointConfigurationName);
            //
            // 摘要:
            //     使用指定的 System.ServiceModel.ClientBase`1 和 System.ServiceModel.InstanceContext
            //     对象初始化 System.ServiceModel.Description.ServiceEndpoint 类的新实例。
            //
            // 参数:
            //   callbackInstance:
            //     客户端应用程序用以侦听来自所连接服务的消息的回调对象。
            //
            //   endpoint:
            //     允许客户端查找并与服务通信的服务的终结点。
            protected ClientBase(InstanceContext callbackInstance, ServiceEndpoint endpoint);
            //
            // 摘要:
            //     初始化 System.ServiceModel.ClientBase`1 类的新实例。
            //
            // 参数:
            //   callbackInstance:
            //     回调服务。
            //
            //   binding:
            //     用于调用服务的绑定。
            //
            //   remoteAddress:
            //     服务终结点的地址。
            //
            // 异常:
            //   T:System.ArgumentNullException:
            //     回调实例为 null。
            //
            //   T:System.ArgumentNullException:
            //     绑定为 null。
            //
            //   T:System.ArgumentNullException:
            //     远程地址为 null。
            protected ClientBase(InstanceContext callbackInstance, Binding binding, EndpointAddress remoteAddress);
            //
            // 摘要:
            //     初始化 System.ServiceModel.ClientBase`1 类的新实例。
            //
            // 参数:
            //   callbackInstance:
            //     客户端用以侦听来自所连接服务的消息的回调对象。
            //
            //   endpointConfigurationName:
            //     应用程序配置文件中的终结点的名称。
            //
            //   remoteAddress:
            //     服务的地址。
            //
            // 异常:
            //   T:System.ArgumentNullException:
            //     回调实例为 null。
            //
            //   T:System.ArgumentNullException:
            //     终结点为 null。
            //
            //   T:System.ArgumentNullException:
            //     远程地址为 null。
            //
            //   T:System.InvalidOperationException:
            //     无法找到终结点或者终结点协定无效。
            protected ClientBase(InstanceContext callbackInstance, string endpointConfigurationName, EndpointAddress remoteAddress);
            //
            // 摘要:
            //     初始化 System.ServiceModel.ClientBase`1 类的新实例。
            //
            // 参数:
            //   callbackInstance:
            //     客户端用以侦听来自所连接服务的消息的回调对象。
            //
            //   endpointConfigurationName:
            //     应用程序配置文件中的终结点的名称。
            //
            //   remoteAddress:
            //     服务的地址。
            //
            // 异常:
            //   T:System.ArgumentNullException:
            //     回调实例为 null。
            //
            //   T:System.ArgumentNullException:
            //     终结点为 null。
            //
            //   T:System.ArgumentNullException:
            //     远程地址为 null。
            //
            //   T:System.InvalidOperationException:
            //     无法找到终结点或者终结点协定无效。
            protected ClientBase(InstanceContext callbackInstance, string endpointConfigurationName, string remoteAddress);
    
            //
            // 摘要:
            //     获取或设置缓存设置。
            //
            // 返回结果:
            //     该缓存设置。
            public static CacheSetting CacheSetting { get; set; }
            //
            // 摘要:
            //     获取 WCF 客户端可连接的服务的目标终结点。
            //
            // 返回结果:
            //     目标终结点。
            public ServiceEndpoint Endpoint { get; }
            //
            // 摘要:
            //     获取基础 System.ServiceModel.IClientChannel 实现。
            //
            // 返回结果:
            //     WCF 客户端对象的客户端通道。
            public IClientChannel InnerChannel { get; }
            //
            // 摘要:
            //     获取 System.ServiceModel.ClientBase`1 对象的当前状态。
            //
            // 返回结果:
            //     对象的 System.ServiceModel.CommunicationState 的值。
            public CommunicationState State { get; }
            //
            // 摘要:
            //     获取用于调用操作的客户端凭据。
            //
            // 返回结果:
            //     返回一个 System.ServiceModel.Description.ClientCredentials,它表示客户端所提供的身份证明。
            public ClientCredentials ClientCredentials { get; }
            //
            // 摘要:
            //     获取基础 System.ServiceModel.ChannelFactory`1 对象。
            //
            // 返回结果:
            //     一个 System.ServiceModel.ChannelFactory`1 对象。
            public ChannelFactory<TChannel> ChannelFactory { get; }
            //
            // 摘要:
            //     获取用于将消息发送到不同配置的服务终结点的内部通道。
            //
            // 返回结果:
            //     指定类型的通道。
            protected TChannel Channel { get; }
    View Code

    这个叫泛型代理吧。

    2.https://github.com/aspnet/EntityFrameworkCore/blob/release/3.1/src/EFCore/DbContext.cs  这个DBContext  也是组合了一下,也是代理的一种应用。

    3.下面是自己该写的泛型代理

      public class GenericProxySubject<T>  : ISubject where T: new()
        {
            private ISubject realSubject;
    
            private ISubject _RealSubject
            {
                get
                {
                    if (this.realSubject == null)
                        realSubject  = new T() as ISubject;
                    return realSubject;
                }
            }
    
            /// <summary>
            /// 0 未初始化  1true  -1 false
            /// </summary>
            private static int _GetSomethingResult = 0;
    
            /// <summary>
            /// 
            /// </summary>
            public bool GetSomething()
            {
                try
                {
                    Console.WriteLine("GetSomething start...");
                    if (_GetSomethingResult == 0)
                    {
                        bool bResult = this._RealSubject.GetSomething();
                        _GetSomethingResult = bResult ? 1 : -1;
                    }
                    Console.WriteLine("GetSomething end...");
                    return _GetSomethingResult == 1;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw ex;
                }
    
            }
    
            /// <summary>
            /// 
            /// </summary>
            public void DoSomething()
            {
                Console.WriteLine("DoSomething start...");
                this._RealSubject.DoSomething();
                Console.WriteLine("DoSomething end...");
            }
        }
    View Code

     4.在Swagger 文档中,生成的访问接口,那就是代理了。

    5.在微服务里网关,也是一个代理。

    气功波(18037675651)
  • 相关阅读:
    Dubbo使用
    JVM内存分配及GC简述
    深入理解ThreadLocal
    Java的Timer定时器
    https与http的区别
    SpringBoot微服务
    Java的BIO,NIO,AIO
    Java常量池
    Java中的值传递与引用传递
    面向对象三大特征及代码优化七大原则
  • 原文地址:https://www.cnblogs.com/qgbo/p/11517383.html
Copyright © 2011-2022 走看看