Wcf服务继承(扩展服务) [英] Wcf service inheritance (Extend a service)

查看:153
本文介绍了Wcf服务继承(扩展服务)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我想能够暴露一个只包含服务的类和一个包含服务和回调的类。 / p>

例如:

  [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()
{
//一些订阅的东西
}

public void MyCallback()
{
++ mValue;

//通知每个订阅者新值
}

};

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



这是否可能?

解决方案

我发现一个解决方案:



创建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()
{
//一些订阅的东西
}

public void myCallback
{
++ mValue;

//向每个订阅者通知新值
}

protected static int;
};

[ServiceBehavior(InstanceContextMode = InstanceContextMode :: Single,ConcurrencyMode = ConcurrencyMode :: Multiple)]
public ServiceAndCallbackClass:ServiceClass
{
// Dummy实现用于创建第二服务
};

从那里,我们可以创建2个服务。一个基于实现类,一个基于Dummy类。每个服务将从不同的接口创建,从而暴露不同的方法。


The program I am working on exposes both callbacks and services using wcf. Basically, what the services do is simply return some variables value. As for the callback, they simply update those variables.

I want to be able to expose one class containing only the services and one class containing the services and the callbacks.

For example :

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

};

If I want only the services, I can use the base class. However, if I want to subscribe to the callback and use the service, I can use ServiceAndCallbackClass.

Is this possible ?

解决方案

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.

这篇关于Wcf服务继承(扩展服务)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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