Can WCF service create its own host?

I have a client / server application type and I would like the server object to create its 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 multiple messages happen. But when it starts to speed up (my application requires data to be transferred every 50ms), the server freezes and the transfer stops after a few seconds without throwing an exception.

So, is it possible to create an object for your master? Or do I really need to create it in main () or do something else?

EDIT: I think the problem in this case is that I want the object that implements the service itself to create its own ServiceHost.

0


a source to share


3 answers


Nothing stops any object from instantiating the ServiceHost.

The big question then is: can you guarantee that your object containing the service host is "alive"? Or was it rubbish collected by accident?



We are using Windows (NT) Services to host our own service host classes to provide 24/7 availability for WCF services - works great.

Mark

0


a source


To be a WCF service, you just need to fulfill the service contract. There is nothing to prevent you from adding more methods to open and close an instance of yourself as a service.



0


a source


Look ServiceBehaviorAttribute , which allows you to specify the behavior of your service ....;) property ConcurrencyMode determined support for multithreading and the default single-threaded mode, and InstanceContextMode determines whether a service object for the session, per call or monochrome.

Quoting from ConcurrencyMode:

Setting the ConcurrencyMode to Single tells the system to restrict service instances to one thread of execution at a time, which saves you threading issues. Multiple means that service objects can be executed by multiple threads at any given time. In this case, you must ensure thread safety.

Quoting from InstanceContextMode:

If InstanceContextMode is set to Single, the result is that your service can only process one message at a time, unless you also set the ConcurrencyMode to Multiple.

We could use some of your service code examples to further debug the behavior you described. For example, is your expensive service object (assuming it is not a singleton), or is it slowing down? Do you know where the time is spent, is it a code, or is there some kind of firewall that restricts the connection? What protocol are you using?

0


a source







All Articles