您的位置:知识库 » .NET技术

使用WCF实现SOA面向服务编程——简单的WCF开发实例

作者: 风尘浪子  来源: 博客园  发布时间: 2011-04-02 10:19  阅读: 4206 次  推荐: 2   原文链接   [收藏]  

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

2
0
 
标签:.NET WCF SOA

.NET技术热门文章

    .NET技术最新文章

      最新新闻

        热门新闻