Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1087259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:54:56+00:00 2026-05-16T22:54:56+00:00

I am creating a WCF service, and it is timing out when it is

  • 0

I am creating a WCF service, and it is timing out when it is used in some of my integration tests. I have been able to narrow it down and it is demonstrated by the following code. It appears that creating a Form before you create the host causes the problem, but I’m not sure why.

class Program
{
    [ServiceContract]
    public interface IMyContract
    {
        [OperationContract]
        void Ping();
    }
    public class MyContract : IMyContract
    {
        public void Ping()
        {
            Console.WriteLine("Ping");
        }
    }
    public class MyContractProxy : ClientBase<IMyContract>, IMyContract
    {
        public MyContractProxy(int port)
            : base(new NetTcpBinding { SendTimeout = TimeSpan.FromSeconds(5) }, new EndpointAddress(string.Format("net.tcp://localhost:{0}", port)))
        {
        }

        public void Ping()
        {
            Channel.Ping();
        }
    }

    static void Main(string[] args)
    {
        try
        {
            new Form();
            var host = new ServiceHost(typeof(MyContract));
            host.AddServiceEndpoint(typeof(IMyContract), new NetTcpBinding(), "net.tcp://localhost:12345");
            host.Open();

            var proxy = new MyContractProxy(12345);
            proxy.Open();
            proxy.Ping();
        }
        catch (Exception e)
        {
            Console.Error.WriteLine(e);
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T22:54:56+00:00Added an answer on May 16, 2026 at 10:54 pm

    The Form changes behavior of your service – your proxy and service runs in the same thread.

    There is difference in hosting service in UI thread and non UI thread. When you host the service in non UI thread, service create new thread for handling the request. If you host the service in UI thread (it happens if you run any WinForm or WPF form there) it does not use new threads by default. Instead it process incomming calls as part of standard windows message loop.

    Add ServiceBehavior to avoid this behavior:

    [ServiceBehavior(UseSynchronizationContext=false)]
    public class MyContract : IMyContract
    { ... }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have quite some trouble creating a WCF service that supports downloads of very
I am creating solution and inside I have three projects: A WCF Service Library
I'm just getting into creating some WCF services, but I have a requirement to
This relates somewhat to: InvalidOperationException while creating wcf web service instance I have a
I have been following along with the Aaron Skonnard videos on creating a WCF
I'm creating a WCF service that may be used either locally or remotely, and
I want my WCF service to be able to return some known faults. ie
I've been tasked with creating a WCF service that will query a db and
I'm creating a wcf service that I test by Ajax. I have a method
I'm creating a WCF Service Library and I have a question regarding thread-safety consuming

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.