Export (0) Print
Expand All

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>, 
	IDisposable
public abstract class ServiceHostBase extends CommunicationObject implements IExtensibleObject<ServiceHostBase>, 
	IDisposable
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 
   ServiceHost(typeof(CalculatorService));
  
  TimeSpan closeTimeout = hostDefault.CloseTimeout;
  
  TimeSpan openTimeout = hostDefault.OpenTimeout;


  ServiceAuthorizationBehavior authorization =
      hostDefault.Authorization;

  ServiceCredentials credentials =
                  hostDefault.Credentials;

  ServiceDescription description = 
          hostDefault.Description;


  int manualFlowControlLimit = 
          hostDefault.ManualFlowControlLimit;


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


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

      // Close the ServiceHost.
      serviceHost.Close();
    }
    catch (TimeoutException timeProblem)
    {
      Console.WriteLine(timeProblem.Message);
      Console.ReadLine();
    }
    catch (CommunicationException commProblem)
    {
      Console.WriteLine(commProblem.Message);
      Console.ReadLine();
    }
  }
}

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

ADD
Show:
© 2014 Microsoft