标签:
This is the third of six tasks required to create a Windows Communication Foundation (WCF) application. For an overview of all six of the tasks, see the Getting Started Tutorial topic.
This topic describes how to host a Windows Communication Foundation (WCF) service in a console application. This procedure consists of the following steps:
这个主题描述了如何在控制台应用程序中托管一个wcf服务。包含以下几个步骤
Create a console application project to host the service. 创建一个控制台应用项目来托管服务
Create a service host for the service. //为服务创建一个servicehost
Enable metadata exchange. 启用元数据的交换
Open the service host. 打开servicehost
A complete listing of the code written in this task is provided in the example following the procedure.
右键解决方案,创建一个新的控制台应用程序。选择添加,新建项目。新建一个名为GettingStartedHost。
Setting the target framework will cause Visual Studio 2012 to reload the solution, press OK when prompted.
右键解决方案资源管理器中GettingStartedHost项目属性,设置项目为.net4.5框架。
添加GettingStartedLib 项目作为GettingStartedHost 的引用。
给GettingStartedHost 项目添加System.ServiceModel的引用。
Open the Program.cs or Module.vb file and enter the following code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.ServiceModel;//Uri和ServiceHost using System.ServiceModel.Description;//ServiceMetadataBehavior using GettingStartedLib; namespace GettingStartedHost { class Program { static void Main(string[] args) { // Step 1 Create a URI to serve as the base address. Uri baseAddress = new Uri("http://localhost:8000/GettingStarted/"); // Step 2 Create a ServiceHost instance ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), baseAddress); try { // Step 3 Add a service endpoint. selfHost.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), "CalculatorService"); // Step 4 Enable metadata exchange. ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); smb.HttpGetEnabled = true; selfHost.Description.Behaviors.Add(smb); // Step 5 Start the service. selfHost.Open(); Console.WriteLine("The service is ready."); Console.WriteLine("Press <ENTER> to terminate service."); Console.WriteLine(); Console.ReadLine(); // Close the ServiceHostBase to shutdown the service. selfHost.Close(); } catch (CommunicationException ce) { Console.WriteLine("An exception occurred: {0}", ce.Message); selfHost.Abort(); } } } }
创建一个Uri类的实例,来保存服务的基础地址。服务通过url来进行识别,url包含了基础地址以及可选的uri。基础地址的格式如下:[transport]://[machine-name or domain][:optional port#]/[optional URI segment]
calculator 服务的基地址,使用的是http传输,localhost,端口8000。URI部分为GettingStarted
创建一个ServicHost 类的实例来托管服务。构造函数接受2个参数,实现了服务契约的类的类型,服务的基地址。
创建ServiceEndpoint 的实例。服务终结点由地址,绑定,服务契约构成。服务终结点的构造函数需要服务契约的接口类型,绑定以及地址。服务契约是ICalculator,此接口定义了,并且在服务类型中实现。在这个示例中使用的绑定是WSHttpBinding,用来连接符合WS-*规范的终结点。地址附加到基地址上来识别终结点。这个代码示例中的地址指定为Calculator,所以这个终结点的完全限定地址为http://localhost:8000/GettingStartedService/Calculator
Important |
---|
Adding a service endpoint is optional when using .NET Framework 4 or later. In these versions, if no endpoints are added in code or configuration, WCF adds one default endpoint for each combination of base address and contract implemented by the service. For more information about default endpoints see Specifying an Endpoint Address. For more information about default endpoints, bindings, and behaviors, see Simplified Configuration and Simplified Configuration for WCF Services. |
重点:在.net4.0以及更新的版本中,添加服务终结点是可选的。如果在代码或配置中没有添加终结点,那么wcf会为每一个基地址和服务实现的契约添加默认的终结点。
启用元数据交互。客户端使用元数据生成代理来调用服务操作。为了确保元数据交换,代码中需要创建一个ServiceMetadataBehavior对象,然后设置HttpGetEnabled属性为true。并且把这个行为添加到ServiceHost实例的Behaviors集合中。
打开ServiceHost来监听进来的消息。注意代码在等待用户enter。如果你没有做这个的话,app会立即关闭并且服务也会被关闭。
还需要注意的是,使用了try catch进行异常的捕获。在ServiceHost实例化之后,所以的代码都放在了try catch块中。
Run the GettingStartedHost console application from inside Visual Studio 2012. When running on Windows Vista and later operating systems, the service must be run with administrator privileges. Because Visual Studio was run with Administrator privileges, GettingStartedHost is also run with Administrator privileges. You can also start a new command prompt running it with Administrator privileges and run service.exe within it.
Open Internet Explorer and browse to the service‘s debug page at http://localhost:8000/GettingStarted/CalculatorService.
1.在VS2012中运行GettingStartedHost控制台应用程序,vista之后的操作系统,必须以管理员权限来运行服务。因为VS2012是以管理员权限运行的,所以GettingStartedHost也是以管理员权限运行的。
你也可以启动cmd命令提示符,使用管理员权限运行。然后调用service.exe
2.打开浏览器,在http://localhost:8000/GettingStarted/CalculatorService浏览服务的调试页面
Notice:
Services such as this one require permission to register HTTP addresses on the machine for listening. Administrator accounts have this permission, but non-administrator accounts must be granted permission for HTTP namespaces. For more information about how to configure namespace reservations, see Configuring HTTP and HTTPS. When running under Visual Studio, the service.exe must be run with administrator privileges.
像这样的服务需要有权限在机器上注册Http地址来监听。管理员账号拥有这个权限,但是非管理员用户必须获得HTTP命名空间的授权。
How to: Host and Run a Basic Windows Communication Foundation Service
标签:
原文地址:http://www.cnblogs.com/chucklu/p/4631184.html