使用W“.NET技术”CF实现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
{

// 在服务器端定义在一个服务契约
[ServiceContract(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 >
<上海企业网站设计与制作n style="color: #800000;">serviceBehaviors >
< behavior 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。

目录
相关文章
|
监控 Cloud Native 测试技术
.NET技术深度解析:现代企业级开发指南
每日激励:“不要一直责怪过去的自己,他曾经站在雾里也很迷茫”。我是蒋星熠Jaxonic,一名在代码宇宙中探索的极客旅人。从.NET Framework到.NET 8,我深耕跨平台、高性能、云原生开发,践行领域驱动设计与微服务架构,用代码书写技术诗篇。分享架构演进、性能优化与AI融合前沿,助力开发者在二进制星河中逐光前行。关注我,共探技术无限可能!
.NET技术深度解析:现代企业级开发指南
|
Go
在golang中发起http请求以获取访问域名的ip地址实例(使用net, httptrace库)
这只是追踪我们的行程的简单方法,不过希望你跟着探险家的脚步,即使是在互联网的隧道中,也可以找到你想去的地方。接下来就是你的探险之旅了,祝你好运!
815 26
|
SQL 小程序 API
如何运用C#.NET技术快速开发一套掌上医院系统?
本方案基于C#.NET技术快速构建掌上医院系统,结合模块化开发理念与医院信息化需求。核心功能涵盖用户端的预约挂号、在线问诊、报告查询等,以及管理端的排班管理和数据统计。采用.NET Core Web API与uni-app实现前后端分离,支持跨平台小程序开发。数据库选用SQL Server 2012,并通过读写分离与索引优化提升性能。部署方案包括Windows Server与负载均衡设计,确保高可用性。同时针对API差异、数据库老化及高并发等问题制定应对措施,保障系统稳定运行。推荐使用Postman、Redgate等工具辅助开发,提升效率与质量。
808 0
|
开发框架 算法 .NET
C#/.NET/.NET Core技术前沿周刊 | 第 15 期(2024年11.25-11.30)
C#/.NET/.NET Core技术前沿周刊 | 第 15 期(2024年11.25-11.30)
426 6
|
开发框架 Cloud Native .NET
C#/.NET/.NET Core技术前沿周刊 | 第 16 期(2024年12.01-12.08)
C#/.NET/.NET Core技术前沿周刊 | 第 16 期(2024年12.01-12.08)
500 6
|
前端开发
WCF更新服务引用报错的原因之一
WCF更新服务引用报错的原因之一
|
C# 数据安全/隐私保护
c#如何创建WCF服务到发布(SqlServer版已经验证)
c#如何创建WCF服务到发布(SqlServer版已经验证)
311 0
|
C++
WCF基础教程(二)——解析iis8和iis8.5+VS2013发布wcf服务问题
WCF基础教程(二)——解析iis8和iis8.5+VS2013发布wcf服务问题
399 0
WCF基础教程(二)——解析iis8和iis8.5+VS2013发布wcf服务问题
|
安全 数据库连接 数据库
WCF服务创建到发布(SqlServer版)
在本示例开始之前,让我们先来了解一下什么是wcf? wcf有哪些特点? wcf是一个面向服务编程的综合分层架构。该架构的项层为服务模型层。 使用户用最少的时间和精力建立自己的软件产品和外界通信的模型。它使得开发者能够建立一个跨平台的安全、可信赖、事务性的解决方案。且能与已有系统兼容写作。 简单概括就是:一组数据通信的应用程序开发接口。
518 0