Beispiel #1
0
        public static T Create <T>(string apiUrl)
        {
            var client = clientCache.GetOrAdd(apiUrl, (url) => new HttpClient()
            {
                BaseAddress = new Uri(url)
            });

            return(ProxyGenerator.Create <T>(new HttpClientProxy(client)));
        }
        public TI Resolve <TI>()
        {
            if (serviceProvider == null)
            {
                serviceProvider = BuildServiceProvider();
            }
            object obj = serviceProvider.GetService <TI>();

            return(ProxyGenerator <TI> .Create((TI)obj));


            //return obj;
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            var poxy1 = (targetInterface)ProxyGenerator.Create(typeof(targetInterface), new SampleProxy("coreproxy1"));

            poxy1.Write("here was invoked"); //---> "here was invoked by coreproxy1"

            var poxy2 = (targetInterface)ProxyGenerator.Create(typeof(targetInterface), typeof(SampleProxy), "coreproxy2");

            poxy2.Write("here was invoked"); //---> "here was invoked by coreproxy2"

            var poxy3 = ProxyGenerator.Create <targetInterface, SampleProxy>("coreproxy3");

            poxy3.Write("here was invoked"); //---> "here was invoked by coreproxy3"
        }
Beispiel #4
0
        public static Dictionary <Type, object> CreateAll(string apiUrl, Action <HttpClientProxyOption> optionAction)
        {
            var option = new HttpClientProxyOption();

            optionAction(option);

            return(option.InterfaceTypes.ToDictionary(x => x, x =>
            {
                var client = clientCache.GetOrAdd(apiUrl, (url) => new HttpClient()
                {
                    BaseAddress = new Uri(url)
                });
                return ProxyGenerator.Create(x, new HttpClientProxy(client));
            }));
        }
Beispiel #5
0
        /// <summary>
        /// Creates a new ScsServiceClient object.
        /// </summary>
        /// <param name="client">Underlying IScsClient object to communicate with server</param>
        /// <param name="clientObject">The client object that is used to call method invokes in client side.
        /// May be null if client has no methods to be invoked by server.</param>
        public ScsServiceClient(IScsClient client, object clientObject)
        {
            _client       = client;
            _clientObject = clientObject;

            _client.Connected    += Client_Connected;
            _client.Disconnected += Client_Disconnected;

            _requestReplyMessenger = new RequestReplyMessenger <IScsClient>(client);
            _requestReplyMessenger.MessageReceived += RequestReplyMessenger_MessageReceived;

            _realServiceProxy = new AutoConnectRemoteInvokeProxy <T, IScsClient>(_requestReplyMessenger, this);
            ServiceProxy      = ProxyGenerator.Create <T>(_realServiceProxy);
            //ServiceProxy = (T)_realServiceProxy.GetTransparentProxy();
        }
Beispiel #6
0
        public void Test1()
        {
            var poxy1 = (ITestInterface)ProxyGenerator.Create(typeof(ITestInterface), new SampleProxy("coreproxy1"));

            poxy1.Write("here was invoked"); //---> "here was invoked by coreproxy1"

            var poxy2 = (ITestInterface)ProxyGenerator.Create(typeof(ITestInterface), typeof(SampleProxy), "coreproxy2");

            poxy2.Write("here was invoked"); //---> "here was invoked by coreproxy2"

            var poxy3 = ProxyGenerator.Create <ITestInterface, SampleProxy>("coreproxy3");

            poxy3.Write("here was invoked"); //---> "here was invoked by coreproxy3"

            var poxy4 = ProxyGenerator.Create <ITestInterface>(new SampleProxy("coreproxy4"));

            poxy4.Write("here was invoked"); //---> "here was invoked by coreproxy4"
        }
        private static void BindInterceptorTypeMap <TInterface>(IServiceCollection services, TypeMap typeMap, ServiceLifetime lifetime)
        {
            var implementationType = typeMap.ImplementationType;

            var serviceTypes = typeMap.ServiceTypes.Where(t => t != typeof(IProxySelector) &&
                                                          t != typeof(IInterceptorContext) &&
                                                          t != typeof(IProxyGenerator) &&
                                                          t != typeof(ISingletonType) &&
                                                          t != typeof(IScopedType) &&
                                                          t != typeof(ITransientType)).ToList();

            foreach (var serviceType in serviceTypes)
            {
                var oldDescription = services.FirstOrDefault(t => t.ServiceType == typeof(TInterface));
                if (oldDescription != null)
                {
                    services.Remove(oldDescription);
                }

                services.Add(new ServiceDescriptor(implementationType, implementationType, lifetime));

                services.AddTransient(serviceType, sp =>
                {
                    IProxySelector proxySelector = sp.GetRequiredService <IProxySelector>();

                    bool isIncludeAspect = proxySelector.ShouldInterceptType(implementationType);

                    if (!implementationType.IsAssignableTo(serviceType))
                    {
                        throw new InvalidOperationException($@"Type ""{implementationType.ToFriendlyName()}"" is not assignable to ""${serviceType.ToFriendlyName()}"".");
                    }

                    object obj = sp.GetRequiredService(implementationType);
                    IProxyGenerator objProxy    = new ProxyGenerator(proxySelector);
                    IInterceptorContext context = new InterceptorContext();
                    return(objProxy.Create(serviceType, implementationType, obj, context));
                });
            }
        }
Beispiel #8
0
 /// <summary>
 /// Gets the client proxy interface that provides calling client methods remotely.
 /// </summary>
 /// <typeparam name="T">Type of client interface</typeparam>
 /// <returns>Client interface</returns>
 public T GetClientProxy <T>() where T : class
 {
     _realProxy = new RemoteInvokeProxy <T, IScsServerClient>(_requestReplyMessenger);
     return(ProxyGenerator.Create <T>(_realProxy));
     //return (T)_realProxy.GetTransparentProxy();
 }
Beispiel #9
0
 public static object Create(Type type, HttpClient httpClient, IHttpContextAccessor httpContextAccessor)
 {
     return(ProxyGenerator.Create(type, new HttpClientProxy(httpClient, httpContextAccessor)));
 }
Beispiel #10
0
 public static object Create(Type type, HttpClient httpClient)
 {
     return(ProxyGenerator.Create(type, new HttpClientProxy(httpClient)));
 }
Beispiel #11
0
 public static T Create <T>(HttpClient httpClient)
 {
     return(ProxyGenerator.Create <T>(new HttpClientProxy(httpClient)));
 }