【问题标题】:Hosting a WCF service as a WindowsService?将 WCF 服务托管为 WindowsService?
【发布时间】:2013-11-22 11:35:32
【问题描述】:

谁能告诉我将 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 服务文件

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

【问题讨论】:

  • 您不需要使用svcutil.exe。右键单击 Visual Studio 中的“参考”部分并选择 Add Service reference。为此需要启动服务。
  • 我以前使用“添加服务引用”来执行此操作..但曾经得到相同的错误..所以这次我使用了 svcutil.exe

标签: wpf wcf windows-services endpoint-address


【解决方案1】:

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

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

话虽如此,除非您告诉它做某事,否则服务将不会继续运行。从技术上讲:必须有一些循环(最好是计时器或线程)来保持服务处于活动状态。如果您没有,该服务将立即启动和停止。


啊哈!从您的问题的编辑中,一件事变得显而易见:您正在用代码中完成的东西覆盖来自app.config 的配置。您需要决定一种方式:要么通过app.config 文件(我推荐)处理所有事情,要么在代码中完成所有事情。

【讨论】:

  • 即使我这样做了,并且每当我尝试执行我的 WPF 应用程序时,我都能启动并运行该服务,但它会因无法访问该特定服务的错误而停止。在这种情况下可能是什么错误?这个错误是否可能是因为 Windows 服务的配置文件中的一些错误?
  • 我点击了这个链接..codeproject.com/Articles/38160/…
  • 好吧,windows 服务的配置文件包含了 WCF 服务的服务配置,所以是的,你很可能有问题。您可以从另一个应用程序(如控制台应用程序)托管 WCF 服务吗?您是否在服务代码(在 OnStart 中)中正确创建了服务主机?我怀疑这与服务有什么关系,但由于您没有发布任何代码或配置信息,我无法判断。
  • WCF 服务的 App.Config
  • 不要发表评论!编辑您的问题!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-18
相关资源
最近更新 更多