使用WC“.NET研究”F实现SOA面向服务编程——简单的WCF开发实例

简介:   前面为大家介绍过WCF的特点,现在再讲解一下WCF基础概念。  在WCF里,各个Application之间的通信是由EndPoint来实现的,EndPoint是WCF实现通信的核心要素。一个WCF Service可由多个EndPoint集合组成,每个EndPoint只能有一种绑定,就是说EndPoint就是通信的入口,客户端和服务端通过EndPoint交换信息。

  前面为大家介绍过WCF的特点,现在再讲解一下WCF基础概念

  在WCF里,各个Application之间的通信是由EndPoint来实现的,EndPoint是WCF实现通信的核心要素。一个WCF Service可由多个EndPoint集合组成,每个EndPoint只能有一种绑定,就是说EndPoint就是通信的入口,客户端和服务端通过 EndPoint交换信息。

 
 
< service name = " " >
< endpoint address = "" binding = " wsHttpBinding " contract = " myNamespace.IService " >
</ endpoint >
</ service >

  Endpoint由三部分组成:(A) Address 地址,(B)Binding 绑定,(C)Contract 契约。

  • A(Address): 通过一个URI唯一地标识一个Endpoint,并告诉WCF service的调用者如何找到这个Endpoint。
  • B(Binding): 定义了与数据传输相关的传输协议,消息编码,通信模式,可靠性,安全性,事务,互操作性等信息。Framewrok3.5里已经包括以下几种绑定:

  • C(Contract):它是有关服务响应的操作及进出消息的格式的语法描述,系统就是通过Contract实现操作的。

  下面为大家讲解一下Hello World的开发例子。

  服务器端:

 
 
using System;
using System.ServiceModel;
namespace myNamespace
{

// 在服务器端定义在一个服务契约
[Servi上海网站建设ceContract(Namespace = " myNamespace " )]
public interface IService
{
[OperationContract]
String HelloWorld();
}
// 实现契约
public class MyService:IService
{
public String HelloWorld( string name)
{
return " Hello World " + Name;
}
}
}

  最后上海闵行企业网站制作,服务既可以在代码中实现,也可以在配置文件中实现:

 
 
< services >
< service behaviorConfiguration ="ServiceBehavior" name ="Service" >
//行为可以影响运行是操作的WCF类,它不公在客户端和服务器启动WCF运行时被执行,还可以在二者之间流动消息时被执行。
< endpoint address ="" binding ="wsHttpBinding" contract ="IService" >
< identity >
< dns value ="localhost" />
</ identity >
</ endpoint >
< endpoint address ="mex" binding ="mexHttpBinding" contract ="IMetadataExchange" />
//mexHttpBinding定义了WCF的元数据。当没有定义元数据时,服务依然能够执行,但不能在HTTP中被发现。
</ service >
</ services >
< behaviors >
< serviceBehaviors >
< behavior 上海企业网站制作pan>name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>

  客户端:

  通过Add Service Reference引用服务地址:

  添加配置文件:

 
 
< system.serviceModel >
< bindings >
上海闵行企业网站设计与制作 />
< basicHttpBinding >
< binding name ="wsHttpBinding_IService" 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/myNamespace.IService.svc" binding ="wsHttpBinding"

上海徐汇企业网站制作 bindingConfiguration
="wsHttpBinding_IService"

contract
="myNamespace.IService" name ="wsHttpBinding_IService" />
</ client >
</ system.serviceModel >

    最后通过代理直接调用:

 
 
static void Main( string [] args)
{
ServiceClient client
= new ServiceClient();
string data = client.HelloWorld( " Leslie " );
Console.Writeline(data);
Console.Read();
}

  朋友,恭喜你,一个最简单Hello World的WCF已经实现。

  然而,如果你要开发一个SOA系统,你不可能将每一个类都编写成一个*.svc文件,那应该怎么做才能真正实现SOA?

  下一章将为你详细介绍如何使用WCF实现真正的SOA。

目录
相关文章
|
1月前
|
SQL 开发框架 数据可视化
企业应用开发中.NET EF常用哪种模式?
企业应用开发中.NET EF常用哪种模式?
|
2月前
|
开发框架 JavaScript 前端开发
5个.NET开源且强大的快速开发框架(帮助你提高生产效率)
5个.NET开源且强大的快速开发框架(帮助你提高生产效率)
|
4月前
|
SQL 开发框架 数据可视化
企业应用开发中.NET EF常用哪种模式?
企业应用开发中.NET EF常用哪种模式?
|
4月前
|
开发框架 缓存 Cloud Native
微软发布 .NET 云原生开发框架—— .NET Aspire
微软于 2023-11-14日 发布了 .NET 8 的正式版。伴随着这个重要 .NET 版本的发布,微软也发布了一个全新的 .NET云原生开发框架 —— .NET Aspire,它提供了如下 3 个方面的能力,来帮助我们使用 .NET 开发分层、云就绪的可观测、本地与生产环境一致的分布式云原生应用程序。
197 0
|
4天前
|
开发框架 前端开发 JavaScript
采用C#.Net +JavaScript 开发的云LIS系统源码 二级医院应用案例有演示
技术架构:Asp.NET CORE 3.1 MVC + SQLserver + Redis等 开发语言:C# 6.0、JavaScript 前端框架:JQuery、EasyUI、Bootstrap 后端框架:MVC、SQLSugar等 数 据 库:SQLserver 2012
|
30天前
|
数据安全/隐私保护 Windows
.net三层架构开发步骤
.net三层架构开发步骤
11 0
|
30天前
深入.net平台的分层开发
深入.net平台的分层开发
48 0
|
2月前
|
开发框架 前端开发 .NET
福利来袭,.NET Core开发5大案例,30w字PDF文档大放送!!!
为了便于大家查找,特将之前开发的.Net Core相关的五大案例整理成文,共计440页,32w字,免费提供给大家,文章底部有PDF下载链接。
32 1
福利来袭,.NET Core开发5大案例,30w字PDF文档大放送!!!
|
3月前
|
C#
.NET开发中合理使用对象映射库,简化和提高工作效率
.NET开发中合理使用对象映射库,简化和提高工作效率
|
3月前
|
开发框架 前端开发 JavaScript
一款基于.NET Core的快速开发框架、支持多种前端UI、内置代码生成器
一款基于.NET Core的快速开发框架、支持多种前端UI、内置代码生成器