
在现代企业级应用开发中,Windows Communication Foundation (WCF) 是一个强大的框架,用于构建安全、可靠和可扩展的分布式系统,本文将详细讨论如何在服务器端进行WCF服务的host配置,包括基本概念、具体步骤和相关代码示例。
一、WCF服务的基本概念
WCF简介
WCF是微软提供的一种用于构建分布式应用程序的框架,它允许开发者在不同的传输协议(如HTTP、TCP等)上创建服务,并支持多种消息格式(如SOAP、JSON、XML等)。
Host概念
在WCF中,Host是指承载和控制WCF服务运行的环境,常见的Hosting方式包括:
自托管:通过编程方式在应用程序内部启动和控制WCF服务。
IIS托管:利用Internet Information Services (IIS) 来承载WCF服务。
WAS托管:使用Windows Process Activation Services (WAS) 来承载WCF服务。

二、自托管WCF服务的配置
ServiceHost类
ServiceHost类是WCF自托管的核心类,负责启动和管理WCF服务,以下是一个简单的例子:
using System;
using System.ServiceModel;
namespace WcfServiceLibrary
{
[ServiceContract]
public interface ICalculator
{
[OperationContract]
int Add(int a, int b);
}
public class CalculatorService : ICalculator
{
public int Add(int a, int b)
{
return a + b;
}
}
class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(CalculatorService)))
{
host.Open();
Console.WriteLine("Service is running...");
Console.ReadLine();
host.Close();
}
}
}
}配置文件App.config
在自托管的WCF服务中,通常会使用App.config文件来配置服务的各种属性。
<configuration>
<system.serviceModel>
<services>
<service name="WcfServiceLibrary.CalculatorService">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/Calculator"/>
</baseAddresses>
</host>
<endpoint address="" binding="wsHttpBinding" contract="WcfServiceLibrary.ICalculator"/>
</service>
</services>
</system.serviceModel>
</configuration>多服务与多终结点配置
可以在一个ServiceHost实例中添加多个服务和终结点,如下所示:
using System;
using System.ServiceModel;
namespace WcfMultiServiceLibrary
{
[ServiceContract]
public interface ICalculator
{
[OperationContract]
int Add(int a, int b);
}
public class CalculatorService : ICalculator
{
public int Add(int a, int b)
{
return a + b;
}
}
[ServiceContract]
public interface IGreeter
{
[OperationContract]
string SayHello(string name);
}
public class GreeterService : IGreeter
{
public string SayHello(string name)
{
return "Hello, " + name;
}
}
class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(CalculatorService), typeof(GreeterService)))
{
host.Open();
Console.WriteLine("Services are running...");
Console.ReadLine();
host.Close();
}
}
}
}对应的App.config文件:
<configuration>
<system.serviceModel>
<services>
<service name="WcfMultiServiceLibrary.CalculatorService">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/Calculator"/>
</baseAddresses>
<endpoint address="" binding="wsHttpBinding" contract="WcfMultiServiceLibrary.ICalculator"/>
</host>
</service>
<service name="WcfMultiServiceLibrary.GreeterService">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/Greeter"/>
</baseAddresses>
<endpoint address="" binding="wsHttpBinding" contract="WcfMultiServiceLibrary.IGreeter"/>
</host>
</service>
</services>
</system.serviceModel>
</configuration>三、IIS托管WCF服务的配置
1. SVC文件与Web.config文件

在IIS托管模式下,需要创建一个.svc文件来定义服务,并在Web.config文件中进行相应的配置。
CalculatorService.svc
<% @ServiceHost Service="WcfServiceLibrary.CalculatorService" %>
Web.config
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.7.2" />
<services>
<service name="WcfServiceLibrary.CalculatorService">
<endpoint address="" binding="wsHttpBinding" contract="WcfServiceLibrary.ICalculator"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>部署到IIS
1、打开IIS管理器,选择“默认网站”或创建一个新的网站。
2、右键点击网站,选择“添加应用程序”。
3、在弹出的对话框中,设置应用程序名称和物理路径(指向包含.svc文件的目录)。
4、点击“确定”,完成部署。
四、常见问题与解答栏目
问题1:如何在WCF服务中启用HTTPS?
解答:要在WCF服务中启用HTTPS,需要在Web.config或App.config文件中添加相应的绑定配置。
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="SecureBinding">
<security mode="Transport">
<transport clientCredentialType="None"/>
</security>
</binding>
</basicHttpBinding>
</bindings>
<services>
<service name="WcfServiceLibrary.CalculatorService">
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="SecureBinding" contract="WcfServiceLibrary.ICalculator"/>
</service>
</services>
</system.serviceModel>然后在IIS中为站点绑定一个SSL证书。
问题2:如何更改WCF服务的端口号?
解答:可以通过修改配置文件中的baseAddress元素来更改WCF服务的端口号。
<baseAddresses> <add baseAddress="http://localhost:9000/Calculator"/> </baseAddresses>
这样就会将服务端口号更改为9000,确保防火墙规则允许新的端口号通过。
五、归纳
本文详细介绍了如何在服务器端进行WCF服务的host配置,包括自托管和IIS托管两种方式的具体步骤和示例代码,通过合理配置WCF服务,可以确保其在生产环境中稳定高效地运行,还提供了一些常见问题的解决方案,帮助开发者更好地应对实际开发中的挑战。
小伙伴们,上文介绍了“服务器端给wcf做了host配置”的内容,你了解清楚吗?希望对你有所帮助,任何问题可以给我留言,让我们下期再见吧。
文章来源网络,作者:运维,如若转载,请注明出处:https://shuyeidc.com/wp/30676.html<
