Wcf service inheritance (Extend a service)

假如想象 提交于 2019-12-06 15:09:51

One solution I found :

Make 2 interfaces. The first one containing only the services and the second one inheriting from the first one and adding the callbacks.

An implementation class would implement the 2 interfaces.

Example :

[ServiceContract]
[ServiceKnownType(typeof(ICallback))]
public interface IService
{
  [OperationContract]
  int GetData();
}

[ServiceContract]
public interface ICallback : IService
{
  [OperationContract]
  public bool subscribe();
}

[ServiceBehavior(InstanceContextMode=InstanceContextMode::Single, ConcurrencyMode=ConcurrencyMode::Multiple)]
public ServiceClass : IService, ICallback
{
  public int getValue()
  {
    return mValue;
  }

  public bool subscribe()
  {
    // some subscribing stuff
  }

  public void myCallback()
  {
    ++mValue;

    // Notify every subscriber with the new value
  }    

  protected static int;
};

[ServiceBehavior(InstanceContextMode=InstanceContextMode::Single, ConcurrencyMode=ConcurrencyMode::Multiple)]
public ServiceAndCallbackClass : ServiceClass
{
  // Dummy implementation used to create second service
};

From there, we can create 2 services. One based on the implementation class and one based on the "Dummy" class. Each service would be created from a different interface and thus exposing different methods.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!