- 浏览: 79672 次
- 性别:
- 来自: 苏州
文章分类
最新评论
(WCF) 示例1:Magic Eight Ball.
- 博客分类:
- C#/.NET
实例一: 构建一个简单的WCF Service: MagicEightBall (问问题,自动回答)。
首先回顾一下WCF程序集之间的关系,理解WCF Host和Client之间的通信架构
┌────────────────┐ ┌────────────────┐
│ Client App │ (SOAP) │ WCF Host │
│ [ Proxy ]<─────────────────>[WCF Service] │
└────────────────┘ └────────────────┘
│ │
Config File Config File
实现WCF通信的思路就是:
创建WCF Service(包括定义Contract和Service,配置Config文件) -> 承载(寄宿)宿主(Host) -> 客户端程序创建Proxy,实现和WCF Service进行通信。
具体步骤如下:
1. 创建Contracts类库,创建WCF Service的接口 IEightBall 。
1)添加引用(.NET)System.ServiceModel.dll程序集,并引用System.ServiceModel命名空间。
2)将WCF Service接口标记 [ServiceContract]特性,每个接口成员标记[OperaionContract]特性。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace Contracts
{
//为了使接口称为WCF Services接口,必须用[ServiceContract]特性加以标记
[ServiceContract]
public interface IEightBall
{
//Ask one question ,and then get an answer.
//WCF框架中使用的方法必须用[OperationContract]特性来修饰。
[OperationContract]
string ObtainAnswerToQuestion(string userQuestion);
}
}
2. 创建Services类库,对Service接口IEightBall进行实现。
1)添加引用Contracts Project,并在代码中引用Contracts命名空间。
2)定义对接口实现的具体的内容。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Contracts;
namespace Services
{
public class EightBallService : IEightBall
{
//Constractor , just display message in Host.
public EightBallService()
{
Console.WriteLine("The 8-ball awaits your question... ");
}
//Services method for interface
public string ObtainAnswerToQuestion(string userQuestion)
{
string[] answers = { "Future Uncertain", "Yes", "No", "Hazy", "Ask again later", "Definitely" };
//Return random answer.
Random r = new Random();
return answers[r.Next(answers.Length)];
}
}
}
3. 创建Host控制台程序,承载(寄宿)WCF Service (产品级的服务一般用Windows Service 或 IIS 虚拟目录承载)
在创建WCF Service的Host时,需要决定在代码中定义必要的承载逻辑,还是配置相关的Config文件。 配置Config文件的好处在于Host可以改变底层的运行方式而不用重新编译和部署可执行文件。
构建WCF Service的Host的一般步骤为:
(I)在Host的配置文件中,定义所承载的WCF Service 的 终结点(Endpoint)。
(II)通过编程使用ServiceHost类型去提供 Endpoint所提供的Service类型。
(III)确保Host保持运行状态,以处理所受到的客户端请求。(显然,Windows Service 或 IIS 不需要这一步 )
具体实现如下:
1)向当前项目中添加一个Application Configuration File (App.conifg). 在App.config文件中创建ABC ,
在WCF的世界中,终结点(Endpoint)是表示A(地址),B(绑定),C(契约)的一个包装。
在XML中,终结点使用<endpoint>元素,和address,binding,contract 元素进行表达。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="Services.EightBallService">
<endpoint address ="http://localhost:8080/EightBallService"
binding ="basicHttpBinding"
contract ="Contracts.IEightBall"/>
</service>
</services>
</system.serviceModel>
</configuration>
2)针对ServiceHost类型进行编程。创建ServiceHost实例,在运行时,这个对象会自动读取Host 中*.config文件中<system.serviceModel>元素中的数据来检测正确的地址,绑定类型和契约,然后创建必要的管道。
2.1)添加对System.ServiceModel.dll 和 本例中 Services project (即Services.dll) 的引用。
2.2)代码中引用System.ServiceModel , Services 命名空间
2.3)启用元数据交换
- 元数据交换(MEX)是一个WCF Service Behaviors,可以指定它微调WCF运行库如何处理我们的Service
- MEX行为会通过HTTP-GET拦截任何元数据请求。如果希望用svcutil.exe或者VS2010来自动创建客户端代理的*.config文件,必须启用MEX。
修改Host的App.config文件如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="Services.EightBallService"
behaviorConfiguration ="MEXBehavior">
<endpoint address=""
binding="basicHttpBinding"
contract="Contracts.IEightBall"/>
<!-- Enable MEX endpoint-->
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange"/>
<!-- Let MEX know the address of Service-->
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080/Services/EightBallService"/>
</baseAddresses>
</host>
</service>
</services>
<!-- Define behavior of MEX-->
<behaviors>
<serviceBehaviors>
<behavior name="MEXBehavior">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
Host代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using Services;
namespace TestServiceHost
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(" Console Based WCF Host ");
using (ServiceHost serviceHost = new ServiceHost(typeof(EightBallService)))
{
//Open host and keep listenning from client.
serviceHost.Open();
//Display host control application windows, and don't exist unless Enter key is pressed.
Console.WriteLine(" The service is ready. Press the Enter key to terminate service. ");
Console.ReadLine();
}
}
}
}
运行这个应用程序后(run as administrator),宿主在内存中随时准备接受从远程客户端发来的请求。
可以用Web浏览器来查看元数据描述。http://localhost:8080/Services/EightBallService
补充1: <system.serviceModel>元素的细节
<system.serviceModel>
<behaviors></behaviors> <!-- 进一步限定Host,Service或Client端的功能。-->
<client></client> <!-- 包含客户端来连接到Service的endpoint列表。-->
<commonBehaviors></commonBehaviors> <!-- 定义了允许WCF和COM互操作的COM契约-->
<diagnostics></diagnostics> <!-- 包含WCF诊断特新的设置,可以启用或禁用跟踪,性能计数器,和WMI提供程序,并且可以增加自定义消息过滤器-->
<comContracts></comContracts> <!-- 这个元素只能在machine.config文件中进行设置,可以用于定义某个机器所有的WCF的行为-->
<services></services> <!-- 包含了从Host公开的WCF Service的集合 -->
<bindings></bindings> <!-- 允许我们微调WCF提供的绑定如basicHttpBinding和netMsmqBing等-->
</system.serviceModel>
4. 创建一个控制台项目,构建WCF Client端应用程序。
1)首先需要创建客户端代理(proxy) (注:之前需要使Host程序保持运行,即使WCF Service保持运行状态)
方法一: 使用svcutil.exe 生成代理代码
svcutil http://localhost:8080/EightBallService/out:myProxy.cs /config:app.cofig
方法二: 使用VS2010生成代理代码。
1.1) 在Client控制台项目中,Add Service Reference。然后在Address里面输入Service的Uri: http://localhost:8080/EightBallService
1.2) 点击go查看Service 描述。命名为 EightBallServiceReference 后保存退出。
- 工具会自动生成客户端的代理类EightBallClient代码。
2)编写客户端控制台代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// Proxy location
using Client.EightBallServiceReference;
namespace Client
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(" Ask the Magic 8 Ball . \n");
using (EightBallClient ball = new EightBallClient())
{
Console.Write("Your questoin:");
string question = Console.ReadLine();
string answer = ball.ObtainAnswerToQuestion(question);
Console.WriteLine("8-ball syas:{0}", answer);
}
Console.ReadLine();
}
}
}
至此,我们在Client端运行Client.exe程序,就可以通过Service代理类来运行WCF Service 中的方法了。 实现了Client端和Server端的通信。
发表评论
-
(WF) 基础1.创建一个WF4.0项目
2012-12-31 16:52 1068(WF) 基础1.创建一个WF4.0项目。 从WinF ... -
(WCF) 基础知识
2012-12-29 14:07 800SOA(Service-Oriented ... -
Visual Studio “类视图”和“对象浏览器”图标
2012-12-29 12:52 881转载: http://blog.sina.com.cn/s/b ... -
WCF的编程模型-实例: 构建一个简单的WCF应用.
2012-12-28 14:45 3171WCF的编程模型-实例: 构 ... -
(C#)属性(property)
2012-12-25 22:39 787(C#)属性(property) 属性是字段和方法的一个交集 ... -
(C#)事件(Event)
2012-12-20 22:06 1073(C#)事件(Event) 事件好像是被简化的针 ... -
(C#)异步编程(进程,线程,计时器)
2012-12-12 12:09 1832(C#)异步编程(进程,线程,计时器) 进程 - 启动 ... -
(C#)接口(Interface)
2012-12-11 15:04 916(C#)接口(Interface)接口是 ... -
(C#)委托(delegate)
2012-12-11 11:42 679(C#)委托(delegate)委托是包含具有相同签名和返回值 ... -
(C#)枚举.
2012-12-11 10:32 636(C#)枚举. 枚举是由程序员定义的类型。- 与结构一样,枚 ... -
(C#)异常及处理.
2012-12-11 09:42 587(C#)异常及处理. 异常时程序运行时发生的错误,它违反了一 ... -
C # 基本概念 (面试题)
2012-11-22 10:37 827Interview Problem 1.Does C# s ... -
C sharp 基本概念(面试题)
2012-11-22 10:35 0Interview Problem 1.Does C# ... -
C# 基本概念(问题)
2012-11-22 10:11 1123传入某个属性的set方 ...
相关推荐
WCF示例项目,c#...................................................................................................................................
做了一个特别简单的wcf 的dome,包含客户端和服务端, 另外我发现了几个比较好的学习wcf 的博客,也分享一下 https://www.cnblogs.com/jiekzou/p/5325310.html https://www.cnblogs.com/llguanli/p/6782774.html ...
构建WCF面向服务的应用程序系列课程(2):WCF契约设计.zip
WCF全面解析:上册.part2.rar
1.找到如下地址“C:\Windows\System32\cmd.exe” 命令行工具,右键以管理员身份运行(视系统是否为win7 而定) 2.输入如下命令: C:\>cd C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin C:\Program Files\...
使用技术:IIS+ASP.NET+WCF+ORACLE+C# 打开方式:Visual Studio 2017以上版本,以管理员方式打开 软件说明:一个使用WCF制作的WEB服务器系统,用于支持驾驶员考试。 数据库结构在:HldWcfModel.HldSysTableClass.cs...
NULL 博文链接:https://alecrichard009.iteye.com/blog/1266327
构建WCF面向服务的应用程序系列课程(12):可靠性消息.zip
WCF4高级编程Professional.WCF.4.rar
上册第一部分http://download.csdn.net/detail/keen1991/5432745 上册第二部分http://download.csdn.net/detail/keen1991/5432815
Wrox.Professional.WCF.4.Windows.Communication.Foundation.with.NET.4.
跟我一起学Visual.Studio.2005(4):VS.2005.IDE针对C#编程的改进 共14篇
微软官方提供的WCF示例源代码,是全部.
动态配置Known Types,解决WCF动态扩展属性类型的问题: Unhandled Exception: System.Runtime.Serialization.SerializationException: Type '--------' with data contract name 'Dog:...
参数1: “C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\SvcUtil.exe” ,是指 SvcUtil.exe 工具的地址 参数2: “C:\WCFClint\ClientCode.cs ” ,是指生成的代理类输出存放到的地址 参数3: “C:\...
WCF全面解析:上册.part3.rar
动态配置Known Types,解决WCF动态扩展属性类型的问题: Unhandled Exception: System.Runtime.Serialization.SerializationException: Type '--------' with data contract name 'Dog:...
[WCF服务编程].(Programming.WCF.Services).Juval.Lowy.文字版.pdf.7z
WCF服务编程全本[10].part1WCF服务编程全本[10].part1
讲解WCF技术的,适合各种阶段的学习。从入门到进阶。