简体   繁体   English

WCF服务可以创建自己的主机吗?

[英]Can an WCF Service create his own host?

I have a client / server type of application and I'd like the server object to create his own host. 我有一个客户端/服务器类型的应用程序,我希望服务器对象创建自己的主机。 It looks something like this: 看起来像这样:

public class Server : IServer {
  private ServiceHost m_Host;
  public Server() {
    m_Host = new ServiceHost(this);
    m_Host.Open();
  }
}

It seems to work fine when there are few message transfers occurring. 几乎没有邮件传输发生时,它似乎工作正常。 But when it starts to speed up (my application requires that data is transfered every 50 ms), the server hangs and and the transfers stop after a few seconds without throwing an exception. 但是,当它开始加速时(我的应用程序要求每50毫秒传输一次数据),服务器挂起,并且传输在几秒钟后停止,而不会引发异常。

So, is it possible for an object to create his own host? 那么,对象可以创建自己的主机吗? Or do I really have to create it in the main() or do something else? 还是真的必须在main()中创建它或做其他事情?

EDIT: I think the problem in this case is that I want the object that implements the service itself to create his own ServiceHost. 编辑:我认为在这种情况下的问题是我希望实现服务本身的对象创建自己的ServiceHost。

There's nothing really stopping any object to create an instance of ServiceHost. 没有什么真正停止任何对象来创建ServiceHost实例的。

The big question then is - can you guarantee that your object containing the service host is "alive"? 那么,最大的问题是-您可以保证包含服务主机的对象是“活动的”吗? Or was it garbage collected by any chance? 还是有可能是垃圾收集?

We use Windows (NT) Services to host our own custom service host classes to provide around-the-clock availability for WCF services - works just fine. 我们使用Windows(NT)服务来托管我们自己的自定义服务宿主类,以提供WCF服务的全天候可用性-正常工作。

Marc

To be a WCF service it simply needs to implement the service contract. 要成为WCF服务,只需执行服务合同即可。 There's nothing to stop you adding more methods to open and close an instance of itself as a service. 没有什么可以阻止您添加更多方法来打开和关闭自身作为服务的实例的。

Check out the ServiceBehaviorAttribute which allows you to specify how your service ... behaves. 查看ServiceBehaviorAttribute ,它可以让您指定服务的行为。 ;) The ConcurrencyMode property defined the support for multithreading and defaults to single threaded mode, and the InstanceContextMode defines if the service object is per session, per call or singleton. ;) ConcurrencyMode属性定义了对多线程的支持,并且默认为单线程模式,而InstanceContextMode定义了服务对象是按会话,按调用还是按单例。

Quote from ConcurrencyMode: 从ConcurrencyMode引用:

Setting ConcurrencyMode to Single instructs the system to restrict instances of the service to one thread of execution at a time, which frees you from dealing with threading issues. 将ConcurrencyMode设置为Single可以指示系统一次将服务实例限制为一个执行线程,这使您无需处理线程问题。 A value of Multiple means that service objects can be executed by multiple threads at any one time. 值Multiple表示可以同时由多个线程执行服务对象。 In this case, you must ensure thread safety. 在这种情况下,必须确保线程安全。

Quote from InstanceContextMode: 引用InstanceContextMode:

If the InstanceContextMode value is set to Single the result is that your service can only process one message at a time unless you also set the ConcurrencyMode value to Multiple. 如果InstanceContextMode值设置为Single,则结果是您的服务一次只能处理一条消息,除非您还将ConcurrencyMode值也设置为Multiple。

We could really use some code examples of your service to further debug the behavior you're describing. 我们确实可以使用您的服务的一些代码示例来进一步调试您正在描述的行为。 For example, is your service object expensive to construct (assuming non singleton implementation), or do the operation slow down? 例如,您的服务对象构造昂贵(假定非单例实现),还是操作变慢? Do you know where the time is spent, is it code, or could it as well be some firewall that limits connection? 您是否知道时间在哪里,是代码还是在限制连接的防火墙? What protocol do you use? 您使用什么协议?

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

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