简体   繁体   中英

Sometime WCF Service stop serving when I press Enter button it allow client apps to communicate

using MyLibrary.Utilities;
using ServerApp.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ServerApp.Managers
{
    internal class HostServicesManager
    {
        private Thread hostServicesThread;
        private static ServiceHost examService;
        private static ServiceHost authenticationService, timeService;
        private static ServiceHost registerService;
        private static ServiceHost invigilatorService;

        private HostServicesManager() { }
        private static HostServicesManager instance = null;
        public static HostServicesManager Instance // Singleton Pattern 
        {
            set { instance = value; }
            get { if (instance == null) { instance = new HostServicesManager(); } return instance; }
        }

        public void StartHostServices()
        {
            hostServicesThread = new Thread(HostServices);
            hostServicesThread.Start();
        }//end of function

        public void StopHostServices()
        {
            if (hostServicesThread != null)
            {
                hostServicesThread.Abort();
            }
        }//end of function

        private  void HostServices()
        {

            string myip = Utilities.myIP();
            string baseAddress = "http://" + myip + ":" + MyConfiguration.Server_PORT;
            if (myip == "")
            {
                Console.WriteLine("<----------- No network is running on this machine. Connect Registrar with network ----------->");
                Console.ReadKey();
                return;
            }

            authenticationService = new ServiceHost(typeof(StudentAuthenticationService), new Uri(baseAddress + "/StudentAuthentication/"));
            authenticationService.Open();

            registerService = new ServiceHost(typeof(RegisterService), new Uri(baseAddress + "/RegisterService/"));
            registerService.Open();

            timeService = new ServiceHost(typeof(TimeService), new Uri(baseAddress + "/ServerTimeService/"));
            timeService.Open();

            examService = new ServiceHost(typeof(PaperService), new Uri(baseAddress + "/ExamService/"));
            examService.Open();

            invigilatorService = new ServiceHost(typeof(InvigilatorAuthenticationService), new Uri(baseAddress + "/InvigilatorService/"));
            invigilatorService.Open();

            DisplayServicesStatus();
        }


        public  void DisplayServicesStatus()
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("|------------------Services--------------------|");
            Console.WriteLine("| My IP: " + Utilities.myIP() + " Open Port: " + MyConfiguration.Server_PORT + "         |"); ;
            Console.WriteLine("| 1- Student Authentication Server is running..|");
            Console.WriteLine("| 2- Time Service is Running...................|");
            Console.WriteLine("| 3- Paper Service is Running..................|");
            Console.WriteLine("| 4- Invigilator Service Running...............|");
            Console.WriteLine("|______________________________________________|");
            Console.WriteLine();
            Console.WriteLine("-----------------Services Log-------------------");
            Console.BackgroundColor = ConsoleColor.Green;
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine(Program.uploadPapersInfoList.Count() + " Papers Loaded............");
            Console.BackgroundColor = ConsoleColor.Black;
            Console.ForegroundColor = ConsoleColor.White;
        }

    }
}

WCF Service Stuck

Here is my code. This class manages the different WCF services.

When I run this application, in some cases or on a slow machine the application is stuck and when client apps try to connect with these services, these services do not respond to them. But when I press the Enter button on the console it allows the services to communicate even though it also sends the old responses that it received while it was stuck.

Assuming you don't have a call to Console.ReadLine() pausing something (which you can check by hitting pause in the debugger and checking the call stack), I would suspect the console is in “quick edit” mode.

When you click or highlight text in quick edit mode, the console will stop, the cursor will change, and the title bar will change until you hit enter (and the selected text will be copied to your clipboard).

See https://superuser.com/questions/1442941/windows-10-console-stops-running-if-i-click-in-the-console-window for a similar scenario

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM