【问题标题】:C# - How do I hide a web method from a derived classC# - 如何从派生类中隐藏 Web 方法
【发布时间】:2009-11-27 15:47:19
【问题描述】:

我有一个 .NET 网络服务,以及它的派生版本。

我的问题是,在派生版本中,我有一个要隐藏的方法(来自 WSDL 和首页)。我尝试过覆盖,将其标记为过时,将其设置为私有和覆盖,但仍然,webservice 属性仍然“有它的方式”。

有什么方法可以删除派生方法上的方法属性。或者有什么方法可以隐藏 WSDL 中的方法?

//罗宾

【问题讨论】:

    标签: .net web-services inheritance methods wsdl


    【解决方案1】:

    您很难实现这一点,因为 .NET(以及任何其他面向对象框架)的继承系统并非为此而设计:请查看 the Liskov substitution principle

    也许您应该选择其他课程来实现您想要的。例如,不使用继承,而是创建一个全新的服务,并使其方法简单地调用原始服务类上的等效方法;这样您就可以只在新服务中包含您想要的方法。

    【讨论】:

    • 组合优于继承!
    【解决方案2】:

    我认为您无法启用任何选项来获得此行为。如果你真的需要隐藏它并且你不能简单地删除它,我会创建一个新的 web 服务,它只公开你想要公开的方法。

    【讨论】:

      【解决方案3】:

      此方法适用于 .Net 4.0:

      [WebService(Namespace = "http://tempuri.org/")]
      [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
      public class Service1 : System.Web.Services.WebService
      {
          [WebMethod]
          public virtual string HelloWorld()
          {
              return "Hello World";
          }
      }
      
      [WebService(Namespace = "http://tempuri.org/")]
      [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
      public class Service2 : Service1
      {
          public override string HelloWorld()
          {
              throw new NotImplementedException();
          }
      }
      

      Service2 派生自 Service1 并在不指定 WebMethodAttribute 的情况下覆盖 HelloWorld WebMethod

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-28
        • 2010-11-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多