WCF Service - coexist as Windows and as Web Service?

Last post 10-19-2009 6:48 AM by dvd.ribeiro. 2 replies.

Sort Posts:

  • WCF Service - coexist as Windows and as Web Service?

    10-14-2009, 7:06 AM
    • Member
      383 point Member
    • dvd.ribeiro
    • Member since 03-28-2006, 11:00 AM
    • Portugal
    • Posts 234

    Hi, I have a WCF simple service (calculator), and I created the code for it as a Windows Service. It has the interface, the service, and a windowsservice.cs class like this:

    public class CalculatorWindowsService : ServiceBase
        {
            public ServiceHost serviceHost = null;
            public CalculatorWindowsService()
            {
                // Name the Windows Service
                ServiceName = "WCFWindowsCalculatorService";
            }
            public static void Main()
            {
                ServiceBase.Run(new CalculatorWindowsService());
            }
            // Start the Windows service.
            protected override void OnStart(string[] args)
            {
                if (serviceHost != null)
                {
                    serviceHost.Close();
                }
                // Create a ServiceHost for the CalculatorService type and 
                // provide the base address.
                serviceHost = new ServiceHost(typeof(CalculatorService));
                // Open the ServiceHostBase to create listeners and start 
                // listening for messages.
                serviceHost.Open();
            }
            protected override void OnStop()
            {
                if (serviceHost != null)
                {
                    serviceHost.Close();
                    serviceHost = null;
                }
            }
        }

    public class CalculatorWindowsService : ServiceBase

        {

            public ServiceHost serviceHost = null;


            public CalculatorWindowsService()

            {

                // Name the Windows Service

                ServiceName = "WCFWindowsCalculatorService";

            }


            public static void Main()

            {

                ServiceBase.Run(new CalculatorWindowsService());

            }



            // Start the Windows service.

            protected override void OnStart(string[] args)

            {

                if (serviceHost != null)

                {

                    serviceHost.Close();

                }


                // Create a ServiceHost for the CalculatorService type and 

                // provide the base address.

                serviceHost = new ServiceHost(typeof(CalculatorService));


                // Open the ServiceHostBase to create listeners and start 

                // listening for messages.

                serviceHost.Open();

            }


            protected override void OnStop()

            {

                if (serviceHost != null)

                {

                    serviceHost.Close();

                    serviceHost = null;

                }

            }

        }


    The thing is, that this code is mainly made for Windows Services.


    How do I also make code to use this same service both as Windows service and as Web Service?

    I'm new at WCF, trying to figure out some differences in technical terms...

    Thank you a lot!


    Best regards,
    David Ribeiro
  • Re: WCF Service - coexist as Windows and as Web Service?

    10-14-2009, 12:14 PM
    Answer
    • Member
      90 point Member
    • randallt
    • Member since 02-11-2009, 4:53 PM
    • Posts 30

    In WCF, a service can be hosted in a number of different ways: in IIS, as aWindows service, in console apps or even in Windows Forms and WPF apps.  The service contract and implementation code can all be re-used, only the hosting code changes.  The code you have posted above is simply for hosting the service--so it won't be useful to you when you try to create a web service--but the CalculatorService code itself can still be reused.  For how to host a WCF service in IIS, see: http://msdn.microsoft.com/en-us/library/ms733766.aspx.

    Also note that this isn't actually a WCF forum, it focuses on the REST Starter Kit.
    General WCF questions are better suited for the WCF forum on MSDN, here's the link:
    http://social.msdn.microsoft.com/Forums/en-US/wcf/threads/

    thanks

  • Re: WCF Service - coexist as Windows and as Web Service?

    10-19-2009, 6:48 AM
    • Member
      383 point Member
    • dvd.ribeiro
    • Member since 03-28-2006, 11:00 AM
    • Portugal
    • Posts 234

    Thank you a lot.


    Best regards,

    Best regards,
    David Ribeiro
Page 1 of 1 (3 items)