About Host Test Project Type

Topics: General Discussion Forum, July and December Releases Forum, Service Factory Modeling Edition Forum
Mar 29, 2008 at 2:25 AM
When we use wssf ,it creates WCF host test project for IIS automatically,how can we change the project type,for example as window service project,Console project...
Mar 30, 2008 at 7:25 PM
I think the key part to this is WCF host test project. As the name states it is a test host for your service that can be hosted in the ASP.NET Development Server (the one included in Visual Studio). For testing a web service, you set both the test host and test client as startup projects. Although it can be, I do not think this WCF host project is designed your actually deployed host. It is merely a sample host for you to test. It should be simple enough for you to create console application to host your solution. Since I like to give complete examples, I have included a sample console application source below. Note that most of the sample code relates to handling ctrl-c etc in the console appliation.

using System;
using System.Runtime.InteropServices;
using System.ServiceModel;
using System.Threading;
 
namespace WcfHostConsoleApplication
{
    [ServiceContract]
    public interface ISampleContract
    {
        [OperationContract]
        bool Hello(string name);
    }
 
    public class SampleService : ISampleContract
    {
        public bool Hello(string name)
        {
            throw new NotImplementedException();
        }
    }
 
 
    class Program
    {
        #region Console Control Hanlder example by Willy Denoyette [MVP] (http://www.thescripts.com/forum/thread273227.html)
        enum CtrlType
        {
            CTRL_C_EVENT = 0,
            CTRL_BREAK_EVENT = 1,
            CTRL_CLOSE_EVENT = 2,
            CTRL_LOGOFF_EVENT = 5,
            CTRL_SHUTDOWN_EVENT = 6
        }
 
        private delegate bool ConsoleCtrlEventHandler(CtrlType sig);
 
        private bool ConsoleCtrlHandler(CtrlType sig)
        {
            bool handled = false;
            switch (sig)
            {
                case CtrlType.CTRL_C_EVENT:
                case CtrlType.CTRL_LOGOFF_EVENT:
                case CtrlType.CTRL_SHUTDOWN_EVENT:
                case CtrlType.CTRL_CLOSE_EVENT:
                    waitEvent.Set();
                    handled = true;
                    break;
                default:
                    return handled;
            }
            return handled;
        }
 
        [DllImport("Kernel32")]
        private static extern bool SetConsoleCtrlHandler(ConsoleCtrlEventHandler handler, bool add);
 
        private ConsoleCtrlEventHandler eventHandler;
 
        private event ConsoleCtrlEventHandler CtrlHandler
        {
            add
            {
                if (eventHandler != null)
                    eventHandler = (ConsoleCtrlEventHandler)Delegate.Combine(eventHandler, value);
                else
                    eventHandler = value;
 
                SetConsoleCtrlHandler(value, true);
            }
 
            remove
            {
                if (eventHandler != null)
                    eventHandler = (ConsoleCtrlEventHandler)Delegate.Remove(eventHandler, value);
 
                SetConsoleCtrlHandler(value, false);
            }
        }
        #endregion
 
        private ManualResetEvent waitEvent; // will be set when the program is to exit
 
        public Program()
        {
            waitEvent = new ManualResetEvent(false);
            CtrlHandler += ConsoleCtrlHandler;
        }
 
        public void Run()
        {
            Uri baseAddress = new Uri("http://localhost:8000");
            ServiceHost host = new ServiceHost(typeof(SampleService), baseAddress);
 
            try
            {
                host.Open();
                WaitHandle.WaitAny(new WaitHandle[] { waitEvent });
            }
            finally
            {
                host.Close();
            }
        }
 
        static void Main(string[] args)
        {
            Program program = new Program();
            program.Run();
        }
    }
}


baston wrote:
When we use wssf ,it creates WCF host test project for IIS automatically,how can we change the project type,for example as window service project,Console project...

Mar 31, 2008 at 5:36 PM
Nice exmample pbolduc!.
As you mentioned, the host test project is basically a quick sample project to show how you can expose and call a service using the factory. In case you want to expose the service in a different host project (other than ASP.NET web project) you can simply point to a console or other project and generate code using that target project.
Apr 1, 2008 at 10:44 AM
Thanks pbolduc and charlyfriend.It seems the test host is not used for deploy but test ,when I finish the service designd with WSSF,I must create deploy project (Web service,Console...) manually, then add the generated service code or .dll to the project .
Apr 1, 2008 at 4:05 PM
You can point to those projects in the Host designer (where you select the client project in the Client Application node) and the generated code and config should end up in the selected project (IIS, console, etc.).