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