0

谁能告诉我将 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;
            }
        }
    }
}
4

1 回答 1

0

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

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

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


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

于 2013-11-22T11:37:53.060 回答