473,387 Members | 1,903 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

WCF -Inproc: Client and Server on main thread -> Client call hangs...

I'm currently running some tests with WCF in In-proc environment.

I have created a small winform application that starts the host in the main
thread with an named pipe endpoint.

If I call a method exposed by the server using the proxy from a separate
thread, all is fine. However, if I do the same from the main thread, the
call hangs and I get a timeoutException after 1min (default call timeout).

Can anyone explain me why this hangs in the second situation?

Thanks a lot!
- José

Dec 19 '07 #1
9 6114
Jose,

How did you enable thread affinity with the server on the main thread?
From what I understand, WCF services will process incoming requests on other
threads, not necessarily the thread that the ServiceHost is created on.

Could you post a complete example?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"José Joye" <jo*************************************@bluewin.c hwrote in
message news:00**********************************@microsof t.com...
I'm currently running some tests with WCF in In-proc environment.

I have created a small winform application that starts the host in the
main thread with an named pipe endpoint.

If I call a method exposed by the server using the proxy from a separate
thread, all is fine. However, if I do the same from the main thread, the
call hangs and I get a timeoutException after 1min (default call timeout).

Can anyone explain me why this hangs in the second situation?

Thanks a lot!
- José

Dec 19 '07 #2
When you say "in-proc" - do you mean that you are hosting both the
service and client in the same winform process? If so, is the server
code /also/ talking to the form? If so, and you are using
Control.Invoke in the server code (to access UI elements), then you
have probably deadlocked yourself - as your UI code is probably
blocking the UI thread waiting for its method to complete.
This would also explain why calling the proxy method on a background
thread doesn't cause the issue: the UI thread isn't blocked, so your
server code can successfully use Control.Invoke.

Have I joined the dots? or missed by a mile?

Marc
Dec 19 '07 #3
And for info - in this scenario your best bet is probably to either
design around it (so your server doesn't need to talk to the UI), or
do what you're already doing: call the proxy from another (pool?)
thread, then use Control.BeginInvoke to marshal the result back to the
UI.

Marc
Dec 19 '07 #4
This is because you are creating a ServiceHost on the UI form. There is
a SynchronizationContext associated with the UI thread, and WCF picks up on
that. Because of that, all calls are routed to the UI thread. Because you
make the proxy call on the UI thread, you block yourself.

There is some more information on this situation here:

http://www.softinsight.com/bnoyes/Pe...2-b0f0e9d11d0a

To get around this, on the implementation of your service, apply the
ServiceBehavior attribute, setting the UseSynchronizationContext to false
(it defaults to true). Either that, or set it in the config file.

Or, you can host the service, or make the call from the proxy on another
thread.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"José Joye" <jo*************************************@bluewin.c hwrote in
message news:uH**************@TK2MSFTNGP06.phx.gbl...
Nicholas,

I built a small sample.
See attached

- José
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.coma écrit
dans le message de news:%2****************@TK2MSFTNGP03.phx.gbl...
>Jose,

How did you enable thread affinity with the server on the main thread?
From what I understand, WCF services will process incoming requests on
other threads, not necessarily the thread that the ServiceHost is created
on.

Could you post a complete example?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"José Joye" <jo*************************************@bluewin.c hwrote in
message news:00**********************************@microsof t.com...
>>I'm currently running some tests with WCF in In-proc environment.

I have created a small winform application that starts the host in the
main thread with an named pipe endpoint.

If I call a method exposed by the server using the proxy from a separate
thread, all is fine. However, if I do the same from the main thread, the
call hangs and I get a timeoutException after 1min (default call
timeout).

Can anyone explain me why this hangs in the second situation?

Thanks a lot!
- José


Dec 19 '07 #5
Marc,

Thanks for your remark. This really makes sense. However, my server method
does not access the UI.

I have posted a sample of my problem within this thread (by the way, I have
just seen that the "catch section" for the method running in the 2nd thread
is buggy in my posted sample)

Thanks,
- José

"Marc Gravell" <ma**********@gmail.coma écrit dans le message de
news:89**********************************@l32g2000 hsh.googlegroups.com...
When you say "in-proc" - do you mean that you are hosting both the
service and client in the same winform process? If so, is the server
code /also/ talking to the form? If so, and you are using
Control.Invoke in the server code (to access UI elements), then you
have probably deadlocked yourself - as your UI code is probably
blocking the UI thread waiting for its method to complete.
This would also explain why calling the proxy method on a background
thread doesn't cause the issue: the UI thread isn't blocked, so your
server code can successfully use Control.Invoke.

Have I joined the dots? or missed by a mile?

Marc
Dec 19 '07 #6
That's the thing, your server method does access the UI, just
unknowingly. Because you create the ServiceHost on a thread that has a
SynchronizationContext associated with it, the ServiceHost marshals all
calls through that SynchronizationContext to the UI thread.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"José Joye" <jo*************************************@bluewin.c hwrote in
message news:75**********************************@microsof t.com...
Marc,

Thanks for your remark. This really makes sense. However, my server method
does not access the UI.

I have posted a sample of my problem within this thread (by the way, I
have just seen that the "catch section" for the method running in the 2nd
thread is buggy in my posted sample)

Thanks,
- José

"Marc Gravell" <ma**********@gmail.coma écrit dans le message de
news:89**********************************@l32g2000 hsh.googlegroups.com...
>When you say "in-proc" - do you mean that you are hosting both the
service and client in the same winform process? If so, is the server
code /also/ talking to the form? If so, and you are using
Control.Invoke in the server code (to access UI elements), then you
have probably deadlocked yourself - as your UI code is probably
blocking the UI thread waiting for its method to complete.
This would also explain why calling the proxy method on a background
thread doesn't cause the issue: the UI thread isn't blocked, so your
server code can successfully use Control.Invoke.

Have I joined the dots? or missed by a mile?

Marc

Dec 19 '07 #7
There is a SynchronizationContext associated with the UI thread, and WCF picks up on
that. Because of that, all calls are routed to the UI thread.
Interesting; I hadn't thought of that (but then, I tend to stick to
simple service apps); I assumesd it would still be using its own
private threads. Thanks Nicholas; as a WCF user I'll squirrel that
knowledge away - it may be useful.

Always a pleasure,

Marc
Dec 19 '07 #8
Marc,

To clarify, the ServiceHost does use its own threads to listen for the
incoming requests. However, when it comes time to actually dispatch the
call, if there was a SynchronizationContext associated with the creation of
the ServiceHost, the runtime will call the Send method on the
SynchronizationContext, dispatching the call.

In this case, it is a Windows Forms app, which marshals the call to the
UI thread. However, because the client of the service is blocking on the
call in the UI thread already, nothing gets processed. Classic deadlock
situation.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Marc Gravell" <ma**********@gmail.comwrote in message
news:5b**********************************@w56g2000 hsf.googlegroups.com...
>There is a SynchronizationContext associated with the UI thread, and WCF
picks up on
that. Because of that, all calls are routed to the UI thread.

Interesting; I hadn't thought of that (but then, I tend to stick to
simple service apps); I assumesd it would still be using its own
private threads. Thanks Nicholas; as a WCF user I'll squirrel that
knowledge away - it may be useful.

Always a pleasure,

Marc

Dec 19 '07 #9
Thanks a lot!

"Nicholas Paldino [.NET/C# MVP]" wrote:
Marc,

To clarify, the ServiceHost does use its own threads to listen for the
incoming requests. However, when it comes time to actually dispatch the
call, if there was a SynchronizationContext associated with the creation of
the ServiceHost, the runtime will call the Send method on the
SynchronizationContext, dispatching the call.

In this case, it is a Windows Forms app, which marshals the call to the
UI thread. However, because the client of the service is blocking on the
call in the UI thread already, nothing gets processed. Classic deadlock
situation.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Marc Gravell" <ma**********@gmail.comwrote in message
news:5b**********************************@w56g2000 hsf.googlegroups.com...
There is a SynchronizationContext associated with the UI thread, and WCF
picks up on
that. Because of that, all calls are routed to the UI thread.
Interesting; I hadn't thought of that (but then, I tend to stick to
simple service apps); I assumesd it would still be using its own
private threads. Thanks Nicholas; as a WCF user I'll squirrel that
knowledge away - it may be useful.

Always a pleasure,

Marc


Dec 20 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Sidd | last post by:
Hi, I tried finding and example of multithreaded client-serve program in python. Can any one please tell me how to write a multithreaded client-server programn in python such that 1.It can handle...
4
by: jas | last post by:
I have a basic client/server socket situation setup....where the server accepts a connection and then waits for commands. On the client side, I create a socket, connect to the server...then I...
0
by: Kapil Shah | last post by:
Hello Everybody, I have created a console multithreaded client server application where many client connects to the server. I have created a timer in the server which asks the client every 30...
1
by: Alexandre (www.pointnetsolutions.com) | last post by:
I am looking at building a distributed system, and i have been looking at many different options, in some cases i was thinking of a client server relation where a client would send a string to...
6
by: Joe Jax | last post by:
I have an object that spawns a worker thread to process one of its methods. That method processes methods on a collection of other objects. During this processing, a user may request to cancel the...
3
by: asadikhan | last post by:
Hi, I have a client server application where the client sends a request to the server with a filename. The server needs to read the file contents, massage the data, and then add it to the...
9
by: =?iso-8859-1?Q?Jos=E9_Joye?= | last post by:
I'm currently running some tests with WCF in In-proc environment. I have created a small winform application that starts the host in the main thread with an named pipe endpoint. If I call a...
0
by: bushramemon | last post by:
hello,,now i m working on multi-threaded client server socket program in which there is one server and multiple clients.........these both are just giving acknowledge to each other that server...
1
by: Emek Taydas | last post by:
You cannot vote on your own post 0 Helloes good people of Bytes.com, I have a .NET client-server application running with a few hundred of clients. The project was migrated from VB6 to .NET about...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.