El cliente wcf llama al método de interfaz de la clase principal mediante reflexión

Hay herencia en la interfaz wcf

 [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string GetTestString1();

       
    }

    [ServiceContract]
    public interface IService2 : IService1
    {
        [OperationContract]
        string GetTestString2();

    }

llamada del cliente

static void Main(string[] args)
        {
            IService2 service = GetServiceInstance("net.tcp://*****"); ;
            Type t = typeof(IService2);
            //父类接口调用
            var parentClass = t.GetInterfaces()[0];
            MethodInfo method = parentClass.GetMethod("GetTestString1");
            Console.WriteLine(method.Name);
            var s1 = method.Invoke(service, new object[] { });
            //子类接口调用
            MethodInfo method2 = t.GetMethod("GetTestString2");
            Console.WriteLine(method2.Name);
            var s2 = method2.Invoke(service, new object[] { });
            Console.ReadLine();
        }

        public static IService2 GetServiceInstance(string address)
        {
            EndpointAddress addresstemp = new EndpointAddress(address);
            NetTcpBinding binding = new NetTcpBinding();
            ChannelFactory<IService2> channelFactory = new ChannelFactory<IService2>(binding, addresstemp);
            IService2 channeltemp = channelFactory.CreateChannel();
            return channeltemp;
        }

Supongo que te gusta

Origin blog.csdn.net/wangyue4/article/details/132120326
Recomendado
Clasificación