How to: Host a WCF Service in a Managed Windows Service

This topic outlines the basic steps required to create a Windows Communication Foundation (WCF) service that is hosted by a Windows Service. The scenario is enabled by the managed Windows service hosting option that is a long-running WCF service hosted outside of Internet Information Services (IIS) in a secure environment that is not message activated. The lifetime of the service is controlled instead by the operating system. This hosting option is available in all versions of Windows.

Windows services can be managed with the Microsoft.ManagementConsole.SnapIn in Microsoft Management Console (MMC) and can be configured to start up automatically when the system boots up. This hosting option consists of registering the application domain (AppDomain) that hosts a WCF service as a managed Windows service so that the process lifetime of the service is controlled by the Service Control Manager (SCM) for Windows services.

The service code includes a service implementation of the service contract, a Windows Service class, and an installer class. The service implementation class, CalculatorService, is a WCF service. The CalculatorWindowsService is a Windows service. To qualify as a Windows service, the class inherits from ServiceBase and implements the OnStart and OnStop methods. In OnStart, a ServiceHost is created for the CalculatorService type and opened. In OnStop, the service is stopped and disposed. The host is also responsible for providing a base address to the service host, which has been configured in application settings. The installer class, which inherits from Installer, allows the program to be installed as a Windows service by the Installutil.exe tool.

Construct the service and provide the hosting code

  1. Create a new Visual Studio Console Application project called "Service".

  2. Rename Program.cs to Service.cs.

  3. Change the namespace to Microsoft.ServiceModel.Samples

  4. Add a references to the following assemblies.

    • System.ServiceModel.dll

    • System.ServiceProcess.dll

    • System.Configuration.Install.dll

  5. Add the following using statements to Service.cs.

    Visual Basic
    Imports System.ComponentModel
    Imports System.ServiceModel
    Imports System.ServiceProcess
    Imports System.Configuration
    Imports System.Configuration.Install
    C#
    using System.ComponentModel;
    using System.ServiceModel;
    using System.ServiceProcess;
    using System.Configuration;
    using System.Configuration.Install;
  6. Define the ICalculator service contract as shown in the following code.

    Visual Basic
    ' Define a service contract.
    <ServiceContract(Namespace:="http://Microsoft.ServiceModel.Samples")> _
    Public Interface ICalculator
        <OperationContract()> _
        Function Add(ByVal n1 As Double, ByVal n2 As Double) As Double
        <OperationContract()> _
        Function Subtract(ByVal n1 As Double, ByVal n2 As Double) As Double
        <OperationContract()> _
        Function Multiply(ByVal n1 As Double, ByVal n2 As Double) As Double
        <OperationContract()> _
        Function Divide(ByVal n1 As Double, ByVal n2 As Double) As Double
    End Interface
    C#
    // Define a service contract.
    [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
    public interface ICalculator
    {
        [OperationContract]
        double Add(double n1, double n2);
        [OperationContract]
        double Subtract(double n1, double n2);
        [OperationContract]
        double Multiply(double n1, double n2);
        [OperationContract]
        double Divide(double n1, double n2);
    }
  7. Implement the service contract in a class called CalculatorService as shown in the following code.

    Visual Basic
    ' Implement the ICalculator service contract in a service class.
    Public Class CalculatorService
        Implements ICalculator
        ' Implement the ICalculator methods.
        Public Function Add(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Add
            Return n1 + n2
    
        End Function
    
        Public Function Subtract(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Subtract
            Return n1 - n2
    
        End Function
    
        Public Function Multiply(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Multiply
            Return n1 * n2
        End Function
    
        Public Function Divide(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Divide
            Return n1 / n2
    
        End Function
    End Class
    C#
    // Implement the ICalculator service contract in a service class.
    public class CalculatorService : ICalculator
    {
        // Implement the ICalculator methods.
        public double Add(double n1, double n2)
        {
            double result = n1 + n2;
            return result;
        }
    
        public double Subtract(double n1, double n2)
        {
            double result = n1 - n2;
            return result;
        }
    
        public double Multiply(double n1, double n2)
        {
            double result = n1 * n2;
            return result;
        }
    
        public double Divide(double n1, double n2)
        {
            double result = n1 / n2;
            return result;
        }
    }
  8. Create a new class called CalculatorWindowsService that inherits from the ServiceBase class. Add a local variable called serviceHost to reference the ServiceHost instance. And define the Main method which calls ServiceBase.Run(new CalculatorWindowsService)

    Visual Basic
    Public Class CalculatorWindowsService
        Inherits ServiceBase
        Public serviceHost As ServiceHost = Nothing
        Public Sub New()
            ' Name the Windows Service
            ServiceName = "WCFWindowsServiceSample"
        End Sub
    
        Public Shared Sub Main()
            ServiceBase.Run(New CalculatorWindowsService())
        End Sub
    C#
    public class CalculatorWindowsService : ServiceBase
    {
        public ServiceHost serviceHost = null;
        public CalculatorWindowsService()
        {
            // Name the Windows Service
            ServiceName = "WCFWindowsServiceSample";
        }
    
        public static void Main()
        {
            ServiceBase.Run(new CalculatorWindowsService());
        }
  9. Override the OnStart method by creating and opening a new ServiceHost instance as shown in the following code.

    Visual Basic
    ' Start the Windows service.
    Protected Overrides Sub OnStart(ByVal args() As String)
        If serviceHost IsNot Nothing Then
            serviceHost.Close()
        End If
    
        ' Create a ServiceHost for the CalculatorService type and 
        ' provide the base address.
        serviceHost = New ServiceHost(GetType(CalculatorService))
    
        ' Open the ServiceHostBase to create listeners and start 
        ' listening for messages.
        serviceHost.Open()
    End Sub
    C#
    // 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();
    }
  10. Override the OnStop method closing the ServiceHost as shown in the following code.

    Visual Basic
    Protected Overrides Sub OnStop()
        If serviceHost IsNot Nothing Then
            serviceHost.Close()
            serviceHost = Nothing
        End If
    End Sub
    C#
    protected override void OnStop()
    {
        if (serviceHost != null)
        {
            serviceHost.Close();
            serviceHost = null;
        }
    }
  11. Create a new class called ProjectInstaller that inherits from Installer and that is marked with the RunInstallerAttribute set to true. This allows the Windows service to be installed by the Installutil,exe tool.

    Visual Basic
       ' Provide the ProjectInstaller class which allows 
       ' the service to be installed by the Installutil.exe tool
       <RunInstaller(True)> _
    Public Class ProjectInstaller
           Inherits Installer
           Private process As ServiceProcessInstaller
           Private service As ServiceInstaller
    
           Public Sub New()
               process = New ServiceProcessInstaller()
               process.Account = ServiceAccount.LocalSystem
               service = New ServiceInstaller()
               service.ServiceName = "WCFWindowsServiceSample"
               Installers.Add(process)
               Installers.Add(service)
           End Sub
       End Class
    C#
    // Provide the ProjectInstaller class which allows 
    // the service to be installed by the Installutil.exe tool
    [RunInstaller(true)]
    public class ProjectInstaller : Installer
    {
        private ServiceProcessInstaller process;
        private ServiceInstaller service;
    
        public ProjectInstaller()
        {
            process = new ServiceProcessInstaller();
            process.Account = ServiceAccount.LocalSystem;
            service = new ServiceInstaller();
            service.ServiceName = "WCFWindowsServiceSample";
            Installers.Add(process);
            Installers.Add(service);
        }
    }
  12. Remove the Service class that was generated when you created the project.

  13. Add an application configuration file to the project. Replace the contents of the file with the following configuration XML.

    Xml
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.serviceModel>
        <services>
          <service name="Microsoft.ServiceModel.Samples.CalculatorService"
                   behaviorConfiguration="CalculatorServiceBehavior">
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:8000/ServiceModelSamples/service"/>
              </baseAddresses>
            </host>
            <!-- this endpoint is exposed at the base address provided by host: http://localhost:8000/ServiceModelSamples/service  -->
            <endpoint address=""
                      binding="wsHttpBinding"
                      contract="Microsoft.ServiceModel.Samples.ICalculator" />
            <!-- the mex endpoint is explosed at http://localhost:8000/ServiceModelSamples/service/mex -->
            <endpoint address="mex"
                      binding="mexHttpBinding"
                      contract="IMetadataExchange" />
          </service>
        </services>
        <behaviors>
          <serviceBehaviors>
            <behavior name="CalculatorServiceBehavior">
              <serviceMetadata httpGetEnabled="true"/>
              <serviceDebug includeExceptionDetailInFaults="False"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
      </system.serviceModel>
    </configuration>

    Right click on the app.config file in the and select Properties. Under Copy to Output Directory select Copy if Newer.

Install and run the service

  1. Build the solution to create the Service.exe executable.

  2. Open the Visual Studio 2008 Command Prompt and navigate to the project directory. Type installutil bin\service.exe at the command prompt to install the Windows service.

    ms733069.note(en-us,VS.90).gifNote:
    If you don't use the Visual Studio 2008 Command Prompt make sure that the %WinDir%\Microsoft.NET\Framework\v2.0.50727 directory is in the system path.

    Type services.msc at the command prompt to access the Service Control Manager (SCM). The Windows service should appear in Services as "WCFWindowsServiceSample". The WCF service can only respond to clients if the Windows service is running. To start the service, right-click it in the SCM and select "Start", or type net start WCFWindowsServiceSample at the command prompt.

  3. If you make changes to the service, you must first stop it and uninstall it. To stop the service, right-click the service in the SCM and select "Stop", or type net stop WCFWindowsServiceSample at the command prompt. Note that if you stop the Windows service and then run a client, an EndpointNotFoundException exception occurs when a client attempts to access the service. To uninstall the Windows service type installutil /u bin\service.exe at the command prompt.

Example

The following is a complete listing of the code used by this topic.

Visual Basic
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text

Imports System.ComponentModel
Imports System.ServiceModel
Imports System.ServiceProcess
Imports System.Configuration
Imports System.Configuration.Install

Namespace Microsoft.ServiceModel.Samples
    ' Define a service contract.
    <ServiceContract(Namespace:="http://Microsoft.ServiceModel.Samples")> _
    Public Interface ICalculator
        <OperationContract()> _
        Function Add(ByVal n1 As Double, ByVal n2 As Double) As Double
        <OperationContract()> _
        Function Subtract(ByVal n1 As Double, ByVal n2 As Double) As Double
        <OperationContract()> _
        Function Multiply(ByVal n1 As Double, ByVal n2 As Double) As Double
        <OperationContract()> _
        Function Divide(ByVal n1 As Double, ByVal n2 As Double) As Double
    End Interface

    ' Implement the ICalculator service contract in a service class.
    Public Class CalculatorService
        Implements ICalculator
        ' Implement the ICalculator methods.
        Public Function Add(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Add
            Return n1 + n2

        End Function

        Public Function Subtract(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Subtract
            Return n1 - n2

        End Function

        Public Function Multiply(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Multiply
            Return n1 * n2
        End Function

        Public Function Divide(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Divide
            Return n1 / n2

        End Function
    End Class

    Public Class CalculatorWindowsService
        Inherits ServiceBase
        Public serviceHost As ServiceHost = Nothing
        Public Sub New()
            ' Name the Windows Service
            ServiceName = "WCFWindowsServiceSample"
        End Sub

        Public Shared Sub Main()
            ServiceBase.Run(New CalculatorWindowsService())
        End Sub

        ' Start the Windows service.
        Protected Overrides Sub OnStart(ByVal args() As String)
            If serviceHost IsNot Nothing Then
                serviceHost.Close()
            End If

            ' Create a ServiceHost for the CalculatorService type and 
            ' provide the base address.
            serviceHost = New ServiceHost(GetType(CalculatorService))

            ' Open the ServiceHostBase to create listeners and start 
            ' listening for messages.
            serviceHost.Open()
        End Sub

        Protected Overrides Sub OnStop()
            If serviceHost IsNot Nothing Then
                serviceHost.Close()
                serviceHost = Nothing
            End If
        End Sub
    End Class
    ' Provide the ProjectInstaller class which allows 
    ' the service to be installed by the Installutil.exe tool
    <RunInstaller(True)> _
 Public Class ProjectInstaller
        Inherits Installer
        Private process As ServiceProcessInstaller
        Private service As ServiceInstaller

        Public Sub New()
            process = New ServiceProcessInstaller()
            process.Account = ServiceAccount.LocalSystem
            service = New ServiceInstaller()
            service.ServiceName = "WCFWindowsServiceSample"
            Installers.Add(process)
            Installers.Add(service)
        End Sub
    End Class
End Namespace
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ComponentModel;
using System.ServiceModel;
using System.ServiceProcess;
using System.Configuration;
using System.Configuration.Install;

namespace Microsoft.ServiceModel.Samples
{
    // Define a service contract.
    [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
    public interface ICalculator
    {
        [OperationContract]
        double Add(double n1, double n2);
        [OperationContract]
        double Subtract(double n1, double n2);
        [OperationContract]
        double Multiply(double n1, double n2);
        [OperationContract]
        double Divide(double n1, double n2);
    }

    // Implement the ICalculator service contract in a service class.
    public class CalculatorService : ICalculator
    {
        // Implement the ICalculator methods.
        public double Add(double n1, double n2)
        {
            double result = n1 + n2;
            return result;
        }

        public double Subtract(double n1, double n2)
        {
            double result = n1 - n2;
            return result;
        }

        public double Multiply(double n1, double n2)
        {
            double result = n1 * n2;
            return result;
        }

        public double Divide(double n1, double n2)
        {
            double result = n1 / n2;
            return result;
        }
    }

    public class CalculatorWindowsService : ServiceBase
    {
        public ServiceHost serviceHost = null;
        public CalculatorWindowsService()
        {
            // Name the Windows Service
            ServiceName = "WCFWindowsServiceSample";
        }

        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;
            }
        }
    }

    // Provide the ProjectInstaller class which allows 
    // the service to be installed by the Installutil.exe tool
    [RunInstaller(true)]
    public class ProjectInstaller : Installer
    {
        private ServiceProcessInstaller process;
        private ServiceInstaller service;

        public ProjectInstaller()
        {
            process = new ServiceProcessInstaller();
            process.Account = ServiceAccount.LocalSystem;
            service = new ServiceInstaller();
            service.ServiceName = "WCFWindowsServiceSample";
            Installers.Add(process);
            Installers.Add(service);
        }
    }
}

Like the "Self-Hosting" option, the Windows service hosting environment requires that some hosting code be written as part of the application. The service is implemented as an console application and contains its own hosting code. In other hosting environments, such as Windows Process Activation Service (WAS) hosting in Internet Information Services (IIS), it is not necessary for developers to write hosting code.

See Also

>
© 2007 Microsoft Corporation. All rights reserved.
Build Date: 2009-10-13
Tags : wcf


Community Content

Eric Crammer
They forgot to include the behaviors section of the app.config!!!
<system.serviceModel>
<services>
<service
name="Microsoft.ServiceModel.Samples.CalculatorService"
behaviorConfiguration="CalculatorServiceBehavior">
...
<!-- the mex endpoint is explosed at http://localhost/servicemodelsamples/service.svc/mex -->
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>

<!--For debugging purposes set the includeExceptionDetailInFaults attribute to true-->
<behaviors>
<serviceBehaviors>
<behavior name="CalculatorServiceBehavior">
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>


Not sure where all you are looking, but the behaviors section is in the provided sample.
Tags : contentbug

Sudheer Yarashi
Error

I get following error:


Service cannot be started. System.InvalidOperationException: The contract name 'IMetadataExchange' could not be found in the list of contracts implemented by the service ContractService. Add a ServiceMetadataBehavior to the configuration file or to the ServiceHost directly to enable support for this contract.

Solution to the above problem by Sudheer Yarashi:-

The value for the "contract" should be same for both <endpoint address="" and <endpoint address="mex". I have highlighted in bold letters below.

<endpoint address=""
binding="wsHttpBinding"
contract="WcfCalculatorService.ICalculator" />

<!-- the mex endpoint is explosed at http://localhost:8000/ServiceModelSamples/service/mex -->
<endpoint address="mex"
binding="mexHttpBinding"
contract="WcfCalculatorService.ICalculator" />

Tags : answer

BruceMe
Details
This is a really great example of some of the best features of WCF/WebServices... Can someone content review and cleanup the details that don't work. Create a sample project that just works?
Tags :

Thomas Lee
Unable to start the windows service

When I try to start the windows service, I am getting the following error :-

"The WCFWindowsServiceSample service on local computer started and then stopped. Some services stop automatically, if they have no work to do, for example, the Performance logs and Alerts Service".

Can anybody provide help on this ?

Solution to the above problem by Sudheer Yarashi:-

The value for the "contract" should be same for both <endpoint address="" and <endpoint address="mex". I have highlighted in bold letters below.

<endpoint address=""
binding="wsHttpBinding"
contract="WcfCalculatorService.ICalculator" />


<!-- the mex endpoint is explosed at http://localhost:8000/ServiceModelSamples/service/mex -->
<endpoint address="mex"
binding="mexHttpBinding"
contract="WcfCalculatorService.ICalculator" />

<Svix says...>
I faced the same problem and it drove me crazy... i was too lazy to enable tracing... after i did, i found that the base address i had provided (http://localhost/myservice) could not be used as the default port it takes is 80... i mentioned the port number as 8080 and voila! the service ran as good as ever..


sloughin
.NET 3.5 InstallUtil.exe?
Having built my service using .NET 3.5, it seems there's no InstallUtil.exe shipped with the .NET 3.5 Framework, so I tried using the latest version I had and got this error:C:\AppServiceHost>InstallUtil bin\Release\AppService.exe
Microsoft (R) .NET Framework Installation utility Version 1.1.4322.573
Copyright (C) Microsoft Corporation 1998-2002. All rights reserved.

Exception occurred while initializing the installation:
System.BadImageFormatException: The format of the file 'AppService.exe' is invalid..

This is probably due to the mismatch in versions, but where is the InstallUtil hidden for .NET 3.5?

ANSWER: .NET 3 and 3.5 are just extensions of 2.0 so look here c:\Windows\Microsoft.NET\Framework\v2.0.50727 (or whatever v2.x you have) for the InstallUtil.exe that works with .NET3.5.


Tags :

Sudheer Yarashi
Unable to start the windows service? Ensure consistent ServiceName and contract names

When I try to start the windows service, I am getting the following error :-

"The WCFWindowsServiceSample service on local computer started and then stopped. Some services stop automatically, if they have no work to do, for example, the Performance logs and Alerts Service".

Can anybody provide help on this ?

If you get the above error, check if the Service Name and Contract Name are specified correctly through out the project. Check in the App.config if all the configurations are correct.

Solution to the above problem by Sudheer Yarashi:-

The value for the "contract" should be same for both <endpoint address="" and <endpoint address="mex". I have highlighted in bold letters below.

<endpoint address=""
binding="wsHttpBinding"
contract="WcfCalculatorService.ICalculator" />

<!-- the mex endpoint is explosed at http://localhost:8000/ServiceModelSamples/service/mex -->
<endpoint address="mex"
binding="mexHttpBinding"
contract="WcfCalculatorService.ICalculator" />

Tags :

Jeltz
More reasons for start followed by immediate stop

A good way to find these errors is also to look in the system event log (obvious but it is often overlooked because it is not an in your face error), and the service start/immediate stop message is not obviously a serious error message.

A windowsservice class compiled with "AnyCPU" target, and a wcf service compiled with x86 target (dumb but my oversight) and then installed on an x64 machine will not generate any errors on service install or service start except to say service stops as soon as it starts. It does not even reach the OnStart override function so exception trapping outlined above does not apply (but it does reach the windowsservice class initialise).

It does of course fail because CLR loader causes bad image format exception trying to load x86 WCF assembly from x64 serviceclass assembly.

I suspect you could trap and log these sorts of problems with a custom assembly resolver but i have not tried this, as I found the right error message in the system event log.

Regards, jeltz

Tags :

Stanley Roark
Making it work: pulling it all together

How to make this sample work:

1. Create a Console Application project. There is actually a "WCF Service Application" project type in Visual Studio 2008 - do not use it for this example. Remove Program.cs from the project.

2. Add references to System.Configuration.Install, System.ServiceModel and System.ServiceProcess.

3. App.config is missing a closing tag:

</system.ServiceModel>

4. In App.config, ensure that the service name and the endpoint contract for wsHttpBinding reflects the namespace that you're using. The given App.config assumes that the namespace is Microsoft.ServiceModel.Samples. If, for instance, the ICalculator interface and the CalculatorService class are in the namespace Joe.Samples, your App.config should be as follows:

...
  <system.serviceModel>
    <services>
      <service name="Joe.Samples.CalculatorService">
...
        <endpoint address=""
                  binding="wsHttpBinding"
                  contract="Joe.Samples.ICalculator" />

5. As pointed out by DWDragen, the behaviors section in App.config is missing:

...
  <system.serviceModel>
    <services>
      <service name="Microsoft.ServiceModel.Samples.CalculatorService" behaviorConfiguration="CalculatorServiceBehavior">
...
    <behaviors>
      <serviceBehaviors>
        <behavior name="CalculatorServiceBehavior">
          <serviceMetadata httpGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

Note that you need to add the behaviorConfiguration attribute.

6. The CalculatorService() method is supposed to be a constructor (but this is not a showstopper):

public void CalculatorService()
{
    ServiceName = "WCFWindowsServiceSample";
}

should be

public CalculatorWindowsService()
{
    ServiceName = "WCFWindowsServiceSample";
}

Tags : wcf

Thomas Lee
How do I generate App.Config for this example?
Sorry, bit of a noob here. Trying to get my head around WCF for an upcoming project.

There is obviously some prior assumed knowledge because I followed this example step by step and yet I don't have an App.Config file.

Can someone please outline the generation of the configuration file for a Managed Windows Service project?

Thanks.


[tfl - 18 06 09] Hi - and thanks for your post. You should post questions like this to the MSDN Forums at http://forums.microsoft.com/msdn or the MSDN Newsgroups at http://www.microsoft.com/communities/newsgroups/en-us/. You are much more likely get a quicker response using the forums than through the Community Content. For specific help about:
Visual Studio : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.vstudio%2C&
SQL Server : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.sqlserver%2C&
.NET Framework : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.dotnet.framework
All Public : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public%2C&

skbardo
A better example
There is a better example here:

http://msdn.microsoft.com/en-us/library/cc949080.aspx#

The only thing is, one of the example codes is incorrect on the link above. Step 5 #8 Should be:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.ServiceModel;
using WcfServiceLibrary1;
namespace WindowsService1
{
public partial class Service1 : ServiceBase
{
internal static ServiceHost myServiceHost = null;
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
if (myServiceHost != null)
{
myServiceHost.Close();
}
myServiceHost = new ServiceHost(typeof(WcfServiceLibrary1.Service1));
myServiceHost.Open();
}
protected override void OnStop()
{
if (myServiceHost != null)
{
myServiceHost.Close();
myServiceHost = null;
}
}
}
}

If you follow that link and make sure to watch for this one error, you should have a TCP WCF service running as a windows service in about 15 mintues.
Tags :

ChristianProgrammer2
O M f G started and then stopped
OK I addressed the ODD contract="WcfCalculatorService.ICalculator" but isnt the Mex Contract supposed to be ,, yea never mind... whatever
I made them both the bolded value as instructed and still start stop

Its a bit crappy that Microsoft cant even put out a descent example of WCF as hosted in a Windows Service ...

maybe they should ask Juval to whip up one ?! :-)


Yea Man seriously the Mex Contract is ALWAYS IMetadataExchange - Rizal7321 - looks to have it figured out I'm going to try his fixes
Tags :

ChristianProgrammer2
Thanks to Rizal7321
This is my working
config file...... if you took the time to set the default namespace to Microsoft.ServiceModel.Samples like the tutorial instructed....

Rizal7321's work allowed me to get this working perfectly ..Thank you Sir !!

I've been studying WCF HARD all week so the fact that I got this to pop makes me happy and Im going to lunch!! It seriously shouldn't be this difficult,,
when you step back a bit the steps really are not that intense ..... its just alot of stuff to remember and implement at once...
the crappy examples we are provided assuredly do not help

Good weekend all .. 9/04/09 Charles Taylor MCSD Milwaukee


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="Microsoft.ServiceModel.Samples.CalculatorService"
behaviorConfiguration="CalculatorServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/ServiceModelSamples/CalculatorService"/>
</baseAddresses>
</host>
<!-- this endpoint is exposed at the base address provided by host: http://localhost:8000/ServiceModelSamples/service -->
<endpoint address=""
binding="wsHttpBinding"
contract ="Microsoft.ServiceModel.Samples.ICalculator"/>
<!--contract="Microsoft.ServiceModel.Samples.ICalculator" /-->
<!-- the mex endpoint is explosed at http://localhost:8000/ServiceModelSamples/service/mex -->
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="CalculatorServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="False"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>

Tags :

Page view tracker