博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用W“.NET技术”CF实现SOA面向服务编程——简单的WCF开发实例
阅读量:7068 次
发布时间:2019-06-28

本文共 3224 字,大约阅读时间需要 10 分钟。

  前面为大家介绍过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。

转载于:https://www.cnblogs.com/waw/archive/2011/10/13/2210692.html

你可能感兴趣的文章
简单的转义字符
查看>>
RabbitMQ入门-Topic模式
查看>>
poj 2777 Count Color(线段树区间更新)
查看>>
Java数据结构与算法(5) - ch05链表(LinkList)
查看>>
CLR Via CSharp读书笔记(21):自动内存管理(垃圾回收)
查看>>
刚刚接触python的感想
查看>>
modelsim使用常见问题及解决办法集锦 ②
查看>>
常用的第三方库
查看>>
java 操作elasticsearch之搭建测试项目环境
查看>>
iOS-图文表并茂,手把手教你GCD
查看>>
python之logging模块
查看>>
让Android Studio支持系统签名
查看>>
3.5 Templates -- Binding Element Attributes(绑定元素属性)
查看>>
jquery常用技巧及常用方法列表集合
查看>>
Microsoft Dynamics AX 2012 正式版虚拟机
查看>>
iOS-CALayer图片淡入淡出动画
查看>>
Python学习笔记
查看>>
Oracle
查看>>
MAT(MemoryAnalyzerTool)
查看>>
分享实用的JavaScript代码库
查看>>