I am using an windows service to host the WCF Pooling Duplex Service, whenever wcf service enters the faulted state I want to restart the WCF Duplex Service. - Restart of normal WCF Service works fine but when I try to restart the WCF Duplex Service it throws an following exception.
The ChannelDispatcher at 'http://localhost:8731/TestService' with contract(s)
"ITestService"' is unable to open its IChannelListener.
This is the inner exception and its stack trace
Innerexception
**A registration already exists for URI 'http://localhost:8731/TestService'.**
StackTrace
at System.ServiceModel.Channels.UriPrefixTable`1.RegisterUri(Uri uri, HostNameComparisonMode hostNameComparisonMode, TItem item)
at System.ServiceModel.Channels.HttpTransportManager.Register(TransportChannelListener channelListener)
at System.ServiceModel.Channels.TransportManager.Open(TransportChannelListener channelListener)
at System.ServiceModel.Channels.TransportManagerContainer.Open(SelectTransportManagersCallback selectTransportManagerCallback)
at System.ServiceModel.Channels.TransportChannelListener.OnOpen(TimeSpan timeout)
at System.ServiceModel.Channels.HttpChannelListener`1.OnOpen(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
at System.ServiceModel.Channels.PollingDuplexChannelListener.OnOpen(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
at System.ServiceModel.Dispatcher.ChannelDispatcher.OnOpen(TimeSpan timeout)
I tried to create a small sample code which is as follows.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceModel;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
namespace WindowsHostedWcfService
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
try
{
ServiceHost TestService = new ServiceHost(typeof(TestService));
TestService.Open();
TestService.Faulted += TestService_Faulted;
AsyncCallback AsyncCallback = new System.AsyncCallback(CloseCompleted);
TestService.BeginClose(AsyncCallback, null);
}
catch (Exception Ex)
{
}
}
private void CloseCompleted(IAsyncResult i)
{
TestService ts = new TestService();
ServiceHost TestService1 = new ServiceHost(ts);
TestService1.Open();
TestService1.Faulted += TestService_Faulted;
}
void TestService_Faulted(object sender, EventArgs e)
{
}
protected override void OnStop()
{
}
public void Run()
{
OnStart(new string[] { });
}
}
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single, InstanceContextMode = InstanceContextMode.Single)]
public class TestService : ITestService
{
public TestService()
{
}
public void TestMethod()
{
throw new Exception();
}
}
[ServiceContract(CallbackContract = typeof(ICallBack))]
public interface ITestService
{
[OperationContract]
void TestMethod();
}
[ServiceContract]
public interface ICallBack
{
[OperationContract(IsOneWay = true)]
void TestCallbackMethod();
}
}
So because of this every time the WCf service goes into the faulted state I have to restart my windows service so that WCF service starts running,
thanks!