WCF Service hangs after client crashes

  • Thread starter Thread starter Elvandar
  • Start date Start date
E

Elvandar

I've a problem with a WCF application. It has 2 parts:

- a Windows Service which exposes a WCF interface on port 15000, channel
type Net TCP
- a WPF client application which connects to the service.

Here is the example interface of the server:

SERVER

public class Program
{
static void Main (string[] args)
{
ServiceHost myInterface = new ServiceHost (typeof (MyService));
myInterface.Open ();
}
}

public interface IMyService
{
[OperationContract]
bool Login (string Username, string Password);
}

public class MyService : IMyService
{
[OperationBehavior]
public bool Login (string Username, string Password)
{
//...autenticazione...
return true;
}
}



And here is an example of the client side:

App.xaml:

Startup="Application_Startup"
ShutdownMode="OnLastWindowClose"
Exit="Application_Exit"


public partial class App : Application
{
public static MyServiceClient server;

private void Application_Startup (object sender, StartupEventArgs e)
{
server = new MyServiceClient ();
server.Open ();
}

private void Application_Exit (object sender, ExitEventArgs e)
{
server.Close ();
}
}


public partial class LoginWindow : Window
{
public void btnLogin_Click (object sender, RoutedEventArgs e)
{
bool result = App.server.Login (txtUsername.Text,
txtPassword.Password);

if (result)
{
string s = null;
s.Split (' '); // Here an exception is thrown
}
}
}


The problem occurs in a situation like this:

1. Server start (Net TCP channel on port 15000)
2. Client start. It correctly connects to server
3. Login on client
4. After Login() returns with true, the client executes some code and,
due to a bug, it crashes
5. I start a new client. It correctyle connects to server (Open() method
doesn't throw any exception)
6. Try to call Login()
7. The client hangs and then returns with a TimeoutException
8. Must restart server (or at least the WCF ServiceHost)

So, sometimes, after the client crashes, the server must be restarted
because its WCF interface doesn't accept any method call...it seems to
be correctly exposed (both via telnet and opening a new client), but if
I try to call any method, it never arrives to the server side (tested in
debug mode in Visual Studio with breakpoints), it seems the call is
being "lost".

Observing the ServiceHost on the server, it never goes to Faulted state,
no Exception is thrown, nothing. Simply, WCF interface is opened but
calls are lost. And this behaviour isn't predictable, because sometimes
it occurs after just 1 client crash, sometimes the client must crash 4
or 5 times before server "hangs"...

Any idea? Thanks to all, and Merry Christmas!
 
This is more of a "in general" tip.

I read your post, and I don't have any tips for your exact issue with the
hanging.

~~maybe this might offer a tidbit of help.


But I usually find the issue via:

http://msdn.microsoft.com/en-us/library/ms732023.aspx

Service Trace Viewer Tool (SvcTraceViewer.exe)



Then you "wire up" your service to record issues to a log file.

Below is what I use for the server side.
You can wire one up for the client side as well.

But this is my "go to" method for finding out "what the heck happened with
my service call??"




<system.diagnostics>

<trace autoflush="true" />

<sources>

<source name="System.ServiceModel"

switchValue="Information, ActivityTracing"

<listeners>

<add name="sdt"

type="System.Diagnostics.XmlWriterTraceListener"

initializeData= "c:\wutemp\Host1.svclog" />

</listeners>

</source>

</sources>

</system.diagnostics>






Elvandar said:
I've a problem with a WCF application. It has 2 parts:

- a Windows Service which exposes a WCF interface on port 15000, channel
type Net TCP
- a WPF client application which connects to the service.

Here is the example interface of the server:

SERVER

public class Program
{
static void Main (string[] args)
{
ServiceHost myInterface = new ServiceHost (typeof (MyService));
myInterface.Open ();
}
}

public interface IMyService
{
[OperationContract]
bool Login (string Username, string Password);
}

public class MyService : IMyService
{
[OperationBehavior]
public bool Login (string Username, string Password)
{
//...autenticazione...
return true;
}
}



And here is an example of the client side:

App.xaml:

Startup="Application_Startup"
ShutdownMode="OnLastWindowClose"
Exit="Application_Exit"


public partial class App : Application
{
public static MyServiceClient server;

private void Application_Startup (object sender, StartupEventArgs e)
{
server = new MyServiceClient ();
server.Open ();
}

private void Application_Exit (object sender, ExitEventArgs e)
{
server.Close ();
}
}


public partial class LoginWindow : Window
{
public void btnLogin_Click (object sender, RoutedEventArgs e)
{
bool result = App.server.Login (txtUsername.Text,
txtPassword.Password);

if (result)
{
string s = null;
s.Split (' '); // Here an exception is thrown
}
}
}


The problem occurs in a situation like this:

1. Server start (Net TCP channel on port 15000)
2. Client start. It correctly connects to server
3. Login on client
4. After Login() returns with true, the client executes some code and, due
to a bug, it crashes
5. I start a new client. It correctyle connects to server (Open() method
doesn't throw any exception)
6. Try to call Login()
7. The client hangs and then returns with a TimeoutException
8. Must restart server (or at least the WCF ServiceHost)

So, sometimes, after the client crashes, the server must be restarted
because its WCF interface doesn't accept any method call...it seems to be
correctly exposed (both via telnet and opening a new client), but if I try
to call any method, it never arrives to the server side (tested in debug
mode in Visual Studio with breakpoints), it seems the call is being
"lost".

Observing the ServiceHost on the server, it never goes to Faulted state,
no Exception is thrown, nothing. Simply, WCF interface is opened but calls
are lost. And this behaviour isn't predictable, because sometimes it
occurs after just 1 client crash, sometimes the client must crash 4 or 5
times before server "hangs"...

Any idea? Thanks to all, and Merry Christmas!
 
Back
Top