Mennan
Mennan

Reputation: 4497

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

I have 3 project in my solution.

enter image description here

I used all same codes on msdn sample

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

I use a wcf service that has 2 methots.I want to use this wcf service in managed windows service.I added a windows service to my solution and set references stuff.

I use this address referance on my wcf - app.config:

net.tcp://localhost:2023/Service1

NOW PROBLEM IS:

I success to add reference to my test client project using

net.tcp://localhost:2023/Service1:

But this referance address is not be used on install as windows service !!! When i install it as windows service,i cant access this address, And i got this error: No connection could be made because the target machine actively refused it

WcfServiceLibruary app.config:

<?xml version="1.0"?>
  <configuration>
    <system.web>
    <compilation debug="true"/>
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <services>
      <service name="WcfServiceLibrary1.Service1">
        <endpoint address="" binding="netTcpBinding" bindingConfiguration="" contract="WcfServiceLibrary1.IService1">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration="" contract="IMetadataExchange"/>
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:2023/Service1"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="false"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>

My WindowsService :

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

Everything works well when i start on visualstudio service host: enter image description here

Upvotes: 2

Views: 10294

Answers (1)

Haidar Abbas
Haidar Abbas

Reputation: 192

Read This Article

In this Construct the service and provide the hosting code

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

Upvotes: 2

Related Questions