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

非 Y 不嫁゛ 提交于 2019-11-28 09:59:10

问题


I have 3 project in my solution.

  • Test Client => for add reference and access via tcp ip
  • WcfServiceLibruary1 => for executing my methots
  • WindowsService1 => for installing&running as windows service(Account:Network Service , StartType:Automatic)

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:


回答1:


Read This Article

In this Construct the service and provide the hosting code

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



来源:https://stackoverflow.com/questions/18542654/how-to-host-a-wcf-service-in-a-managed-windows-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!