创建我的WCF演示程序

WCF的服务不能孤立地存在,需要寄宿于一个运行着的进程中,我们把承载WCF服务的进程称为宿主,为服务指定宿主的过程称为服务寄宿(Service Hosting)。

寄宿方式有两种

第一种:通过自我寄宿(Self-Hosting)的方式创建一个控制台应用作为服务的宿主(寄宿进程为Hosting.exe)。

第二种:通过IIS寄宿方式将服务寄宿于IIS中(寄宿进程为IIS的工作进行W3wp.exe)。客户端通过另一个控制台应用模拟(进程为Client.exe)。

第一步:创建一个WCF的解决方案

通过VS2008创建一个空白的解决方案。然后再添加四个空白的类库,分别是:

ContractsWCF作为分布式开发的一种,具有事先定义后的数据交换规则,而定制的规则被称为契约(Contract)。引用System.ServiceMode程序集。

Services提供对WCF服务的实现。定义在该项目中的所有WCF服务实现了定义在Contracts中相应的服务契约,所以Services具有对Contracts项目的引用。

Hosting一个控制台应用程序。实现对定义在Services项目中的服务的寄宿,该项目须要同时引用Contracts和Services两个项目和System.ServiceMode程序集。

Client一个控制台应用模拟服务的客户端,该项目引用System.ServiceMode程序集。

在Contracts类库中添加System.ServiceMode应用,如图:

第二步:创建服务契约

Contracts类库中创建Contracts接口类,命名空间名为Artech.WcfServices.Contracts

View Code
using System.ServiceModel;

namespace Artech.WcfServices.Contracts
{
[ServiceContract(Name
= "CalculatorService", Namespace = "http://www.artech.com/")]
public interface ICalculator
{
//
[OperationContract]
double Add(double x, double y);

//
[OperationContract]
double Subtract(double x, double y);

//
[OperationContract]
double Multiply(double x, double y);

//
[OperationContract]
double Divide(double x, double y);
}
}

第三步:创建服务

Services类库中创建Services类,命名空间名为Artech.WcfServices.Services。添加Artech.WcfServices.Contracts引用。

View Code

using Artech.WcfServices.Contracts;

namespace Artech.WcfServices.Services
{
public class CalculatorService:ICalculator
{
public double Add(double x, double y)
{
return x + y;
}

public double Subtract(double x, double y)
{
return x - y;
}

public double Multiply(double x, double y)
{
return x * y;
}

public double Divide(double x, double y)
{
return x / y;
}
}
}

继承接口类关系图

第四步:通过自我寄宿的方式寄宿服务

WCF服务需要依存一个运行着的进程(宿主),服务寄宿就是为服务指定一个宿主的过程。通过为服务添加一个或多个终结点,使之暴露给潜给的服务消费者。Hosting类库的代码体现了通过一个控制台应用对CalculatorService的寄宿:

首先添加三个引用,分别为:System.ServiceModel、Artech.WcfServices.Contracts、Artech.WcfServices.Services

实现自我寄宿的两种方式:

第一种:完全通过代码的方式完成所有的服务寄宿工作

Hosting类库的代码:

View Code
using System.ServiceModel;
using System.ServiceModel.Description;
using Artech.WcfServices.Contracts;
using Artech.WcfServices.Services;

namespace Artech.WcfServices.Hosting
{
class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(CalculatorService)))
{
host.AddServiceEndpoint(
typeof(ICalculator), new WSHttpBinding(), "http://127.0.0.1:9999/calculatorservice");//添加一个终结点。指定了服务契约的类型ICalculator

//WCF服务的描述通过元数据(Metadata)的形式发布出来。通过一个特殊的服务行为ServiceMetadataBehavior实现
if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)
{
ServiceMetadataBehavior behavior
= new ServiceMetadataBehavior();
behavior.HttpGetEnabled
= true;//基于HTTP-GET的元数据获取方式.
behavior.HttpGetUrl = new Uri("http://127.0.0.1:9999/calculatorservice/metadata");//元数据的发布地址
host.Description.Behaviors.Add(behavior);
}
host.Opened
+= delegate
{
Console.WriteLine(
"CalculaorService已经启动,按任意键终止服务!");
};

host.Open();
Console.Read();
}
}
}
}

第二种:通过配置的方式实现服务寄宿工作

在Hosting类库中添加配置文件App.config配置代码如下:

启动Hosting.exe。在IE地址栏上键入http://127.0.0.1:9999/calculatorservice/metadata,你将会得到以WSDL形式体现的服务元数据

View Code
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="metadataBehavior">
<serviceMetadata httpGetEnabled="true" httpGetUrl="http://127.0.0.1:9999/calculatorservice/metadata"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="metadataBehavior" name="Artech.WcfServices.Services.CalculatorService">
<endpoint address="http://127.0.0.1:9999/calculatorservice" binding="wsHttpBinding" contract="Artech.WcfServices.Contracts.ICalculator" />
</service>
</services>
</system.serviceModel>
</configuration>

在配置之后Hosting控制台的Program.cs文件代码精简为:

View Code
using System.ServiceModel;
using System.ServiceModel.Description;
using Artech.WcfServices.Contracts;
using Artech.WcfServices.Services;

namespace Artech.WcfServices.Hosting
{
class Program
{
static void Main(string[] args)
{
using(ServiceHost host=new ServiceHost(typeof(CalculatorService)))
{
host.Opened
+= delegate
{
Console.WriteLine(
"CalculaorService已经启动,按任意键终止服务!");
};

host.Open();
Console.Read();
}
}
}
}



第五步:创建客户端调用服务

在运行服务寄宿程序(Hosting.exe)的情况下,右键点击Client项目,在弹出的上下文菜单中选择“添加服务引用。地址栏上键入服务元数据发布的源地址:http://127.0.0.1:9999/calculatorservice/metadata,并指定一个命名空间,点击OK按钮,VS为为你生成一系列用于服务调用的代码和配置。

创建CalculatorServiceClient对象,执行相应方法调用服务操作。客户端进行服务调用的代码如下:



View Code
using Client.CalculatorServices;

namespace Artech.WcfServices.Client
{
class Program
{
static void Main(string[] args)
{
using (CalculatorServiceClient proxy = new CalculatorServiceClient())
{
Console.WriteLine(
"x + y = {2} when x = {0} and y = {1}", 1, 2, proxy.Add(1, 2));
Console.WriteLine(
"x - y = {2} when x = {0} and y = {1}", 1, 2, proxy.Subtract(1, 2));
Console.WriteLine(
"x * y = {2} when x = {0} and y = {1}", 1, 2, proxy.Multiply(1, 2));
Console.WriteLine(
"x / y = {2} when x = {0} and y = {1}", 1, 2, proxy.Divide(1, 2));
}
}
}
}

 运行结果:

演示程序源码:WcfServices.rar

原文地址:https://www.cnblogs.com/sjllef/p/1965884.html