2

我正在开发的程序使用 wcf 公开回调和服务。基本上,服务所做的只是返回一些变量值。至于回调,他们只是更新那些变量。

我希望能够公开一个只包含服务的类和一个包含服务和回调的类。

例如 :

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

  protected static int mValue;

};

[ServiceContract]
[ServiceBehavior(InstanceContextMode=InstanceContextMode::Single, ConcurrencyMode=ConcurrencyMode::Multiple)]
public ServiceAndCallbackClass : ServiceClass
{
  [OperationContract]
  public bool subscribe()
  {
    // some subscribing stuff
  }

  public void MyCallback()
  {
    ++mValue;

    // Notify every subscriber with the new value
  }

};

如果我只想要服务,我可以使用基类。但是,如果我想订阅回调并使用服务,我可以使用 ServiceAndCallbackClass。

这可能吗 ?

4

1 回答 1

2

我发现的一种解决方案:

制作2个接口。第一个仅包含服务,第二个从第一个继承并添加回调。

一个实现类将实现这两​​个接口。

例子 :

[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.

于 2012-04-24T13:54:08.623 回答