xaria
xaria

Reputation: 842

Hosting WCF as Windows service " Service was started and Stopped"

I am hosting WCF as Windows Service using net.tcp. After installing the windows service when I start the service, I get that service was started and stopped.

The error says in order to In order to add an endpoint to the service 'MYService', a non-empty contract name must be specified. at System.ServiceModel.Description.ConfigLoader.LookupContract(String contractName, String serviceName)

My OnStart function is as follows

 protected override void OnStart(string[] args)
        {
            try
            {
                if (myServiceHost != null)
                {
                    myServiceHost.Close();
                }
                myServiceHost = new ServiceHost(typeof(MYservice));
                myServiceHost.Open();

            }
            catch (Exception ex)
            {
                log.Error("ONStart", ex);
                throw;
            }

        }

The config file is as follows:

<serviceHostingEnvironment minFreeMemoryPercentageToActivateService="10" />
<services>
  <service behaviorConfiguration="myServiceBehavior"
    name="myNamespace.myTestService">
    <endpoint binding="netTcpBinding" bindingConfiguration="netTCPBindingConfig" contract="myNamespace.ServiceInterface.ImyTestService" />
    <endpoint binding="mexTcpBinding" bindingConfiguration="" />
    <host>
      <baseAddresses>
        <add baseAddress="net.tcp://10.1.3.69:8523/TestService" />
      </baseAddresses>
      <timeouts closeTimeout="10:00:10" openTimeout="10:01:00" />
    </host>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="myServiceBehavior">
      <serviceMetadata httpGetEnabled="false" httpsGetEnabled="false" />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
  </serviceBehaviors>
</behaviors>

Upvotes: 1

Views: 2471

Answers (2)

Dejan Janjušević
Dejan Janjušević

Reputation: 3230

In your config file, there is:

<endpoint binding="netTcpBinding" bindingConfiguration="netTCPBindingConfig" contract="myNamespace.ServiceInterface.ISomeService" /> `

Instead of ISomeService, you must specify whichever interface is implemented by MYService.

EDIT

Additionally, mex binding must have a contract specified, i.e. contract="IMetadataExchange"

EDIT AGAIN

For your convenience, this is how your mex binding should look like:

<endpoint binding="mexTcpBinding" address="mex" bindingConfiguration="" contract="IMetadataExchange" />

Upvotes: 5

user1312242
user1312242

Reputation: 337

Please try this:

        protected override void OnStart(string[] args)
        {
            try
            {
                myServiceHost = new ServiceHost(typeof(MYservice));
                myServiceHost.Open();
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                log.Error("ONStart", ex); throw;
            }
            finally
            {
                myServiceHost.Close();

            }
        }

Upvotes: -2

Related Questions