使用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。

目录
相关文章
|
4月前
|
Go
在golang中发起http请求以获取访问域名的ip地址实例(使用net, httptrace库)
这只是追踪我们的行程的简单方法,不过希望你跟着探险家的脚步,即使是在互联网的隧道中,也可以找到你想去的地方。接下来就是你的探险之旅了,祝你好运!
157 26
|
5月前
|
中间件 Go
Golang | Gin:net/http与Gin启动web服务的简单比较
总的来说,`net/http`和 `Gin`都是优秀的库,它们各有优缺点。你应该根据你的需求和经验来选择最适合你的工具。希望这个比较可以帮助你做出决策。
188 35
|
3月前
|
存储 缓存
.NET 6中Startup.cs文件注入本地缓存策略与服务生命周期管理实践:AddTransient, AddScoped, AddSingleton。
记住,选择正确的服务生命周期并妥善管理它们是至关重要的,因为它们直接影响你的应用程序的性能和行为。就像一个成功的建筑工地,工具箱如果整理得当,工具选择和使用得当,工地的整体效率将会大大提高。
126 0
|
7月前
|
人工智能 芯片
D1net阅闻|OpenAI员工疯狂暗示,内部已成功开发ASI?被曝训出GPT-5但雪藏
D1net阅闻|OpenAI员工疯狂暗示,内部已成功开发ASI?被曝训出GPT-5但雪藏
|
5月前
|
SQL 小程序 API
如何运用C#.NET技术快速开发一套掌上医院系统?
本方案基于C#.NET技术快速构建掌上医院系统,结合模块化开发理念与医院信息化需求。核心功能涵盖用户端的预约挂号、在线问诊、报告查询等,以及管理端的排班管理和数据统计。采用.NET Core Web API与uni-app实现前后端分离,支持跨平台小程序开发。数据库选用SQL Server 2012,并通过读写分离与索引优化提升性能。部署方案包括Windows Server与负载均衡设计,确保高可用性。同时针对API差异、数据库老化及高并发等问题制定应对措施,保障系统稳定运行。推荐使用Postman、Redgate等工具辅助开发,提升效率与质量。
169 0
|
9月前
|
Linux API C#
基于 .NET 开发的多功能流媒体管理控制平台
基于 .NET 开发的多功能流媒体管理控制平台
139 9
|
9月前
|
Web App开发 前端开发 调度
一款基于 .NET + Blazor 开发的智能访客管理系统
一款基于 .NET + Blazor 开发的智能访客管理系统
117 8
|
9月前
|
前端开发 JavaScript C#
基于.NET8+Vue3开发的权限管理&个人博客系统
基于.NET8+Vue3开发的权限管理&个人博客系统
117 7
|
前端开发
WCF更新服务引用报错的原因之一
WCF更新服务引用报错的原因之一
|
C# 数据安全/隐私保护
c#如何创建WCF服务到发布(SqlServer版已经验证)
c#如何创建WCF服务到发布(SqlServer版已经验证)
123 0

热门文章

最新文章