简体   繁体   English

如何在本地计算机上安装的多个WCF服务之间共享属性?

[英]How to share a property between multiple WCF services installed on a local machine?

I have a Windows service hosting multiple WCF services. 我有一个托管多个WCF服务的Windows服务。 All of it running on a local machine (using NamePipe). 它全部在本地计算机上运行(使用NamePipe)。
Is there a simple way to have these WCF services share a property? 是否有一种简单的方法可以使这些WCF服务共享属性? (I would rather not have them sharing a file). (我希望他们不要共享文件)。
I need this as each session instantiated within each WCF service will be getting a hold on a given hardware and the other WCF services need to know what is still available in order to be able to instantiate another session. 我需要这个,因为每个WCF服务中实例化的每个会话都将保留给定的硬件,而其他WCF服务则需要知道仍有哪些可用,以便能够实例化另一个会话。
Each WCF service implements a different protocol, which is why I did not merge the lot. 每个WCF服务都实现一个不同的协议,这就是为什么我没有合并很多东西的原因。 There will be one proxy for each host. 每个主机都有一个代理。

I don't know of a super simple way of getting service instances to share a property, but you could create a custom host that derives from ServiceHost and have it implement a particular interface, say something like: 我不知道让服务实例共享属性的超级简单方法,但是您可以创建一个派生自ServiceHost的自定义主机,并使其实现特定的接口,例如:

public interface ISharedStateContainer
{
  SharedState State { get; set; }
}

This interface would have to be known your services. 必须知道此接口的服务。 Then, in your windows service project, you could make a custom service host: 然后,在Windows服务项目中,您可以创建一个自定义服务主机:

public class CustomServiceHost: ServiceHost, ISharedStateContainer
{
   SharedState state;
   public SharedState State{ get{ return state; } set{ state=value; } }       
}

...and then when the windows service creates the wcf service host instances, it could inject the shared state: ...然后Windows服务创建wcf服务主机实例时,它可以注入共享状态:

var sharedState = new SharedState();

myServiceHost = new CustomServiceHost( typeof( MyService ) );
((ISharedStateContainer) myServiceHost).State = sharedState;

myOtherHost = new CustomServiceHost( typeof( OtherService ) );
((ISharedStateContainer) myOtherHost).State = sharedState;

myServiceHost.Open();
myOtherHost.Open();

...and then, in a running instance of a service, you could get to shared state like this: ...然后在正在运行的服务实例中,您可以进入共享状态,如下所示:

var sharedState = ((ISharedStateContainer)OperationContext.Current.Host).State

Where I've got SharedState , you could make it any type you want...but making it a reference type that itself has properties means you can use it to share as many properties as you need. 在拥有SharedState ,可以将其设置为所需的任何类型...但是将其设置为本身具有属性的引用类型意味着您可以使用它来共享所需的多个属性。 Note that with any shared state, you'll have race conditions to protect against. 请注意,在任何共享状态下,您都将具有防止竞争的竞争条件。

I've done a windows service the same way (multiple different wcf service types)...and this is more-or-less how they share state. 我已经以相同的方式(多种不同的WCF服务类型)完成了Windows服务...这差不多就是它们共享状态的方式。

EDIT: 编辑:

I don't know why I didn't think about this sooner, but another nice way to share state is using a singleton. 我不知道为什么我不早考虑这一点,但是共享状态的另一种好方法是使用单例。 This is probably more straightforward than the earlier approach. 这可能比以前的方法更直接。 I have this pattern going on, too...for a somewhat different reason, but it would serve for shared state, too: 我也正在继续这个模式...出于某种不同的原因,但它也适用于共享状态:

public class SharedState
{
  //--> singleton instance...
  static readonly SharedState current;

  //--> use static initializer to create the current instance...
  static SharedState( )
  {
    current = new SharedState();
  }

  //--> hide ctor...
  private SharedState(){}

  public static SharedState Current
  {
    get { return current; }
  }

  //--> all your shared state instance methods and properties go here...

  public string SomeString
  {
    get
    {
      return //...
    }
  }

}

...and then you can get to this object from anywhere in your service , even from code not running in the context of a client operation. ...然后您可以从服务中的任何位置访问此对象,甚至可以从不在客户端操作的上下文中运行的代码访问。 I use this for long running background task that the service needs to perform periodically, but shared properties are super easy: 我将其用于服务需要定期执行的长期运行的后台任务,但是共享属性非常简单:

var someValue = SharedState.Current.SomeString;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM