using System; using System.Collections.Generic; using System.Text; using System.ServiceModel; using System.ServiceModel.Dispatcher; using System.ServiceModel.Channels; using System.ServiceModel.Description; namespace WCFSample { class Program { static void Main(string[] args) { //Hidden singleton hosting // ServiceHost host = new ServiceHost(typeof(MyService), new Uri("http://localhost:8080/MyService")); //Wellknown singleton hosting ServiceHost host = new ServiceHost(new MyService(), new Uri("http://localhost:8080/MyService")); host.AddServiceEndpoint(typeof(IService), new BasicHttpBinding(), ""); host.Open(); IService client = ChannelFactory.CreateChannel(new BasicHttpBinding(), new EndpointAddress("http://localhost:8080/MyService")); client.SayHello(); client.SayHello(); client.SayHello(); client.ReleaseInstance(); client.SayHello(); client.SayHello(); client.SayHello(); client.ReleaseInstance(); client.SayHello(); client.SayHello(); client.SayHello(); ((IClientChannel)client).Close(); Console.WriteLine("Calling ServiceHost.Close()"); host.Close(); } } [ServiceContract] public interface IService { [OperationContract] void SayHello(); [OperationContract] void ReleaseInstance(); } [ServiceBehavior(InstanceContextMode=InstanceContextMode.Single, ConcurrencyMode=ConcurrencyMode.Single)] public class MyService : IService, IDisposable { int sayHelloCount; String id; public MyService() { id = Guid.NewGuid().ToString(); Console.WriteLine("MyService Ctor called. Id = "+ id); sayHelloCount = 0; } #region IService Members public void SayHello() { sayHelloCount++; Console.WriteLine("SayHello count : " + sayHelloCount); } public void ReleaseInstance() { Console.WriteLine("Calling ReleaseServiceInstance"); OperationContext.Current.InstanceContext.ReleaseServiceInstance(); Console.WriteLine("Calling GetServiceInstance"); OperationContext.Current.InstanceContext.GetServiceInstance(); } #endregion #region IDisposable Members public void Dispose() { Console.WriteLine("Destructor called for Id : " + id); } #endregion } }