コード例 #1
0
ファイル: Templates.cs プロジェクト: jiejohn/WCFQuickSamples
        public static void Test()
        {
            string      baseAddress = "http://" + Environment.MachineName + ":8000/Service";
            ServiceHost host        = new ServiceHost(typeof(Service), new Uri(baseAddress));

            host.AddServiceEndpoint(typeof(ITest), GetBinding(), "");
            host.Open();
            Console.WriteLine("Host opened");

            Thread[] threads = new Thread[5];
            for (var i = 0; i < threads.Length; i++)
            {
                threads[i] = new Thread(new ThreadStart(delegate
                {
                    ChannelFactory <ITest> factory = new ChannelFactory <ITest>(GetBinding(), new EndpointAddress(baseAddress));
                    Trace("{0} - Client before call", Thread.CurrentThread.ManagedThreadId);
                    ITest proxy = factory.CreateChannel();
                    proxy.BeginAdd(5, 8, (result) =>
                    {
                        Trace("{0} - Client result: {1}", Thread.CurrentThread.ManagedThreadId, proxy.EndAdd(result));
                        ((IClientChannel)proxy).Close();
                        factory.Close();
                    }, null);
                }));
            }

            Trace("Starting threads...");
            for (var i = 0; i < threads.Length; i++)
            {
                threads[i].Start();
            }

            Console.Write("Press ENTER to close the host");

            Console.ReadLine();
            host.Close();
        }