Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
ServiceHostBase Class
Collapse the table of content
Expand the table of content

ServiceHostBase Class

Extend the ServiceHostBase class to implement hosts that expose custom programming models.

Namespace: System.ServiceModel
Assembly: System.ServiceModel (in system.servicemodel.dll)

public abstract class ServiceHostBase : CommunicationObject, IExtensibleObject<ServiceHostBase>, IDisposable
public abstract class ServiceHostBase extends CommunicationObject implements IExtensibleObject<ServiceHostBase>, 
public abstract class ServiceHostBase extends CommunicationObject implements IExtensibleObject<ServiceHostBase>, 
Not applicable.

Use the ServiceHostBase class to create hosts that provide a custom programming model. The Windows Communication Foundation (WCF) service programming model uses the ServiceHost class.

Special note for Managed C++ users deriving from this class:

  • Put your cleanup code in (On)(Begin)Close (and/or OnAbort), not in a destructor.

  • Avoid destructors: they cause the compiler to auto-generate IDisposable

  • Avoid non-reference members: they can cause the compiler to auto-generate IDisposable

  • Avoid finalizers; but if you include one, you should suppress the build warning and call SuppressFinalize and the finalizer itself from (On)(Begin)Close (and/or OnAbort) in order to emulate what would have been the auto-generated IDisposable behavior.

This sample uses the ServiceHost class which is derived from ServiceHostBase

// Host the service within this EXE console application.
public static void Main()
  // Create a ServiceHost for the CalculatorService type and use
  // the base address from config.
  ServiceHost hostDefault = new 
  TimeSpan closeTimeout = hostDefault.CloseTimeout;
  TimeSpan openTimeout = hostDefault.OpenTimeout;

  ServiceAuthorizationBehavior authorization =

  ServiceCredentials credentials =

  ServiceDescription description = 

  int manualFlowControlLimit = 

  NetTcpBinding portsharingBinding = new NetTcpBinding();
typeof( CalculatorService ),
"net.tcp://localhost/MyService" );

  int newLimit = hostDefault.IncrementManualFlowControlLimit(100);
  using (ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService)))
      // Open the ServiceHost to start listening for messages.
      // The service can now be accessed.
      Console.WriteLine("The service is ready.");
      Console.WriteLine("Press <ENTER> to terminate service.");

      // Close the ServiceHost.
    catch (TimeoutException timeProblem)
    catch (CommunicationException commProblem)

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0

Community Additions

© 2015 Microsoft