WCF 第四章 绑定 选择一个合适的绑定

WCF中有9个预设绑定。这些绑定中的每一个都满足一个特殊分布式计算的需求。很多因素决定了为一个特殊应用选择哪一个绑定,包括安全,互通性,可 信赖,性能和事务需求。表4.2 通过显示9种预设绑定支持的公共特性来进行比较。这张表可以用来为一个特定需求选择最好的绑定。
 
最 常见的用来选择一个绑定的方法是检查你的应用程序需要的特性并由此确定一个满足那些需求的绑定。表4.2 比较了每一个预设绑定的特性以便于你可以基于自己的需求选择绑定。有很多特性,包括互操作,间隔,可信赖和事务。比如,如果你的应用程序需要在一个不可信 赖的网络中通信,例如在一个无线网络连接中,你可能想要一个支持可靠会话(RS)的绑定。图片4.2 显示了你可能用来选择一个绑定的过程。
 
Table 4.2 Supported Features of Each Binding

 

  RS*=由WS-Reliable 通信(WS-RM)定义的在一个已实现的SOAP可信赖消息中的WCF可靠会话。
  当你在选择一个绑定时你需要考虑很多特性。表4.2 不能把它们全部列出来;因此,你可能需要做更进一步的研究来选择一个合适的绑定。
  每一个绑定都支持一种特殊的通信场景,比如跨机器,本机和使用Web Services的互通通信。我们将检查与每个绑定一起的那些场景。也有其他的场景,比如集成安全和对等通信。这些主题应该做更深入的探讨并且会在第八章 “安全”和第十二章“对等网络”中详细讨论。
 
示例程序
我们现在讲检查WCF中每一个预设绑定。为了说明每个 绑定我们将使用一个基于stock quotes 的示例程序。示例请求基于一个票价并返回stock 价格。这个内容要在不同绑定中暴露并使用同样的服务,同时要注意代码和配置文件中的任意改动。列表4.4显示了stock quote 服务。
 
列表4.4 StockQuoteService 服务

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace EssentialWCF
{
[ServiceContract]
public interface IStockQuoteService
{
[OperationContract]
double GetQuote(string symbol);
}

public class StockQuoteService : IStockQuoteService
{
public double GetQuote(string symbol)
{
double value;
if (symbol == "MSFT")
value
= 31.15;
else if (symbol == "YHOO")
value
= 28.10;
else if (symbol == "GOOG")
value
= 450.75;
else
value
= double.NaN;
return value;
}
}
}

列表4.5 显示了在Visual Studio 中通过添加服务引用生成的客户端代理。我们手工编辑代理代码来移除任何注释同时为了格式化目的将共同使用的命名空间添加using语句。不考虑这些微小的 改动,这些代码与你通过添加服务引用或者svcutil.exe生成的代理代码是一样的。我们的目的是使用有这不同绑定的同样客户端代码并观察在代码或配 置文件中的任何改动。

列表4.5 StockQuoteService 客户端代理

//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:2.0.50727.4952
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace Client.ServiceReference {


[System.CodeDom.Compiler.GeneratedCodeAttribute(
"System.ServiceModel", "3.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(
ConfigurationName="ServiceReference.IStockQuoteService")]
public interface IStockQuoteService {

[System.ServiceModel.OperationContractAttribute(
ReplyAction="http://tempuri.org/IStockQuoteService/GetQuoteResponse")]
double GetQuote(string symbol);
}

[System.CodeDom.Compiler.GeneratedCodeAttribute(
"System.ServiceModel", "3.0.0.0")]
public interface IStockQuoteServiceChannel :
Client.ServiceReference.IStockQuoteService, System.ServiceModel.IClientChannel {
}

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute(
"System.ServiceModel", "3.0.0.0")]
public partial class StockQuoteServiceClient :
System.ServiceModel.ClientBase<Client.ServiceReference.IStockQuoteService>,
Client.ServiceReference.IStockQuoteService {

public StockQuoteServiceClient() {
}

public StockQuoteServiceClient(string endpointConfigurationName) :
base(endpointConfigurationName) {
}

public StockQuoteServiceClient(string endpointConfigurationName, string remoteAddress) :
base(endpointConfigurationName, remoteAddress) {
}

public StockQuoteServiceClient(string endpointConfigurationName,
System.ServiceModel.EndpointAddress remoteAddress) :
base(endpointConfigurationName, remoteAddress) {
}

public StockQuoteServiceClient(System.ServiceModel.Channels.Binding binding,
System.ServiceModel.EndpointAddress remoteAddress) :
base(binding, remoteAddress) {
}

public double GetQuote(string symbol) {
return base.Channel.GetQuote(symbol);
}
}
}
 

示例应用程序使用自我寄宿的方法来寄宿服务。列表4.6显示了自我寄宿StockQuoteService的代码。查看第七章“寄宿”来获取更多关于自我寄宿的内容。

列表4.6 StockQuoteService ServiceHost

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace EssentialWCF
{
class Program
{
static void Main(string[] args)
{
MyServiceHost.StartService();
Console.WriteLine(
"Press <Enter> to terminate service!");
Console.ReadLine();
MyServiceHost.StopService();
}
}
internal class MyServiceHost
{
internal static ServiceHost myServiceHost = null;
internal static void StartService()
{
myServiceHost
= new ServiceHost(typeof(StockQuoteService));
myServiceHost.Open();
}
internal static void StopService()
{
if (myServiceHost.State != CommunicationState.Closed)
{
myServiceHost.Close();
}
}
}
}

=========

转载自

 

posted @ 2011-06-29 10:11  Gavin Liu  阅读(236)  评论(0编辑  收藏  举报

Right people get the right information at the right time.
以技术求生存,以市场求发展;学以至用,开拓创新;达技术之颠峰,至市场之广阔!