托管WCF服务作为WindowsService?

时间:2013-11-22 11:35:32

标签: wpf wcf windows-services endpoint-address

有人能告诉我将WCF服务库作为Windows服务托管的方法吗? 我试图遵循各种链接,但总是得到一些或其他错误。 服务启动或立即停止或客户端无法访问Windows上托管的服务..我使用简单的WPF应用程序作为客户端。

也可以有人告诉我端点地址和基地址之间的区别以及托管WCF作为Windows服务时应该设置的内容

WCF服务的App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="mysqlconnection" connectionString="Initial catalog=calculator; data source=10.2.108.251; User Id=sa; Password=abc@123"/>
  </connectionStrings>
  <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="Calculator1.CalculatorService1" behaviorConfiguration="Calculator1.BasicCalculator">
        <endpoint address="" binding="wsHttpBinding" contract="Calculator1.ICalculator1">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8732/Design_Time_Addresses/Calculator1/Service1/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="Calculator1.BasicCalculator">
          <!-- To avoid disclosing metadata information, 
          set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="True"/>
          <!-- To receive exception details in faults for debugging purposes, 
          set the value below to true.  Set to false before deployment 
          to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

</configuration>

我使用SvcUtil.exe生成的app.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_ICalculator1" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                    useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <security mode="None">
                        <transport clientCredentialType="None" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
                    </security>
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:9001/CalculatorService1"
                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ICalculator1"
                contract="ICalculator1" name="BasicHttpBinding_ICalculator1" />
        </client>
    </system.serviceModel>
</configuration>

Windows sErvices文件

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 System.ServiceModel.Description;

namespace WindowsSErviceCalculator1
{
    public partial class CalculatorWindowsService1 : ServiceBase
    {
        ServiceHost m_svcHost = null;
        public CalculatorWindowsService1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            if (m_svcHost != null)
            {
                m_svcHost.Close();
            }
            string strAdrHTTP = "http://localhost:9001/CalculatorService1";
            Uri[] adrbase = { new Uri(strAdrHTTP) };
            m_svcHost = new ServiceHost(typeof(Calculator1.CalculatorService1), adrbase);

            ServiceMetadataBehavior mBehave = new ServiceMetadataBehavior();
            m_svcHost.Description.Behaviors.Add(mBehave);

            BasicHttpBinding httpb = new BasicHttpBinding();
            m_svcHost.AddServiceEndpoint(typeof(Calculator1.ICalculator1), httpb, strAdrHTTP);
            m_svcHost.AddServiceEndpoint(typeof(IMetadataExchange),
            MetadataExchangeBindings.CreateMexHttpBinding(), "mex");

            m_svcHost.Open();
        }

        protected override void OnStop()
        {
            if (m_svcHost != null)
            {
                m_svcHost.Close();
                m_svcHost = null;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

例如,如果您可以将服务作为控制台应用程序托管,则从该应用程序app.config文件中获取服务配置,并将其复制到服务的app.config文件中。

从应用程序或服务托管WCF服务之间没有任何区别。

话虽这么说,服务不会继续运行,除非你告诉它做某事。从技术上讲:必须有一些循环(理想情况下是计时器或线程)才能使服务保持活跃状态​​。如果您没有,服务将立即开始和停止。


啊哈!从你的问题的编辑中,有一件事变得很明显:你用app.config覆盖了代码中完成的东西的配置。您需要决定一种方式:通过app.config文件(我推荐)或通过代码执行所有操作。