jeudi 20 avril 2023

C# safe interface method invoking

I've got class that provides some data. I receive access to it through Remoting and call it's methods. But sometimes connection breaks. I want to have one method that is wrapping calls to this DataProvider, which first checks connection, reconnects if necessary and than invokes that method. In .Net there is MothodInfo, MethodBase and etc. But I didn't succeded trying to write such mechanism.

        public interface IDataProvider
        {
            void SomeMethod();
            void someParametrizedMethod(int p1);
            void someParametrizedMethod2(int p1, string p2);
            object someParametrizedMethodWithReturnValue(int p1, string p2);
            List<string> someParametrizedMethodWithReturnValue2(int p1, string p2);
        }

        public class DataProviderConnector
        {
            public T SafeCallToServer<T>(???)
            {
                // Check if connection is alive and reconnect if necessary
                Reconnect();
                // Invoke that ??? method and return T result
            }

            private void Reconnect()
            {
                // Do reconnect
            }

            IDataProvider serverValue;
            public IDataProvider Server
            {
                get
                {
                    if (serverValue == null)
                        serverValue = (IDataProvider)Activator.GetObject(typeof(IUltimaServer), GetServiceUrl(typeof(IUltimaServer)));

                    return serverValue;
                }
            }
        }

        public class UserClass
        {
            public UserClass()
            {
                var connector = new DataProviderConnector();

                // I want to be able to make calls like this
                connector.SafeCallToServer(IDataProvider.someParametrizedMethod2(1, "123"));
                // Or like this
                connector.SafeCallToServer(IDataProvider.someParametrizedMethod2, new object[] { 1, "123" });
            }
        }

Does anyone have some suggestions how do it?





Aucun commentaire:

Enregistrer un commentaire