C#如何动态更新运行时的接口? [英] C# how to update interfaces in runtime dynamically?

查看:78
本文介绍了C#如何动态更新运行时的接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有此界面的WCF服务:

I have a WCF Service with this interface:

[ServiceContract]
interface IServiceA
{
     [OperationContract]
     int Add(int value1, int value2);

     [OperationContract]
     int Subtract(int value1, int value2);
}





然后,在另一项服务上,我将通过ChannelFactory与它沟通



Then, on another service, I will be communicating with it through ChannelFactory

class ControllerService
{
     static string address = String.Format("net.tcp://localhost:8733/TestingService/Service");

     static void Main(string[] args)
     {
          while (true)
          {
               try
               {
                    ChannelFactory<IServiceA> factory = new ChannelFactory<IServiceA>(new NetTcpBinding());
                    IServiceA proxy = factory.CreateChannel(new EndpointAddress(address));
                    Console.WriteLine(proxy.Add(1 , 2));
               }
               catch (Exception ex)
               {
                    Console.WriteLine("Was not possible to run the service: " + ex);
               }
          }
     }
}





这两项服务是独立运行的。问题是在ControllerService上我必须事先知道接口与IServiceA进行通信。

如果IServiceA脱机,使用接口中的新方法进行更新,如何更新我的ControllerService以识别新方法并动态调用它们而无需关闭ControllerService?



我尝试过的方法:



我通过ExpandoObject尝试使用动态对象,我尝试在运行时使用反射,我确实可以创建类和对象,并在运行时定义所有这些新方法。我只是无法对界面做同样的事情...



我已经阅读了多个网站,问题和答案仍然看不到任何界面在运行时创建或更新...



我发现的另一个解决方案是在服务之间始终保持相同的接口,通信将是IServiceA通知它有可用的方法,而ControllerService基本上与之通信,我希望你执行方法X,它会以同样的方式返回响应。



These two services are running independently. The problem is that on ControllerService I have to know the interface beforehand to communicate with the IServiceA.
If IServiceA goes offline, gets updated with new methods in the Interface, how can I update my ControllerService to recognize the new methods and call them dynamically without having to turn ControllerService off?

What I have tried:

I tried with dynamic objects through ExpandoObject, I tried using reflection in runtime which I can indeed create classes and objects and define new methods all this in runtime. I just haven't been able to do the same for interfaces...

I've read through multiple sites and questions and answers still couldn't see any interface creation or update in runtime...

The other "solution" I found was to have ALWAYS the same interface to communicate between services, that communication would be IServiceA informs which methods it has available, and ControllerService basically communicates back with I want you to execute the method "X" and it would return the response the same way.

推荐答案

一个接口是合同,如果它可以动态变化那么它不是合同。你怎么可能动态调用方法?假设我添加了一个名为AddPersonToDivision的merthod,它接受带有Firstname,Lastname等的Person对象和一个包含DivisionName,DivisionManager等字段的Division对象,它是一个Manager实例,它是一个派生自Person的类。你打算如何动态调用它?



如果IServiceA发生变化,你需要重新编写\re-compile并重新发布客户端。如果您希望在没有服务停止的情况下发生这种情况,那么您需要查看负载平衡,您可以在其中依次更新服务,以便至少有一个服务始终在运行。
An interface is a contract, if it could change dynamically then it's not a contract. And how can you possibly call methods dynamically? Let's say I add a merthod called "AddPersonToDivision" that accepts a Person object with Firstname, Lastname etc and a Division object that contains fields like "DivisionName", "DivisionManager" that is an instance of "Manager" which is a class derived from Person. How are you going to call that dynamically?

If IServiceA changes then you need to re-write\re-compile and re-release the client. If you want that happening without the service going down then you'll need to look into load balancing where you can have services that you update in-turn so that at least one is always running.


这篇关于C#如何动态更新运行时的接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆