[原创]我的WCF之旅(2):Endpoint Overview

WCF实际上是构建了一个框架,这个框架实现了在互联系统中各个Application之间如何通信。使得Developers和Architect在构建分布式系统中,无需在考虑如何去实现通信相关的问题,更加关注与系统的业务逻辑本身。而在WCF Infrastructure中,各个Application之间的通信是由Endpoint来实现的。

Endpoint的结构

Endpoint包含以下4个对象:

  • Address: Address通过一个URI唯一地标识一个Endpoint,并告诉潜在的WCF service的调用者如何找到这个Endpoint。所以Address解决了Where to locate the WCF Service?
  • Binding: Binding实现在Client和Service通信的所有底层细节。比如Client与Service之间传递的Message是如何编码的——text/XML, binary,MTOM;这种Message的传递是采用的哪种Transport——TCP, Http, Named Pipe, MSMQ; 以及采用怎样的机制解决Secure Messaging的问题——SSL,Message Level Security。所以Binding解决的是How to communicate with service?
  • Contract: Contract的主要的作用是暴露某个WCF Service所提供的所有有效的Functionality。从Message Exchange的层面上讲,Contract实际上是抱每个Operation转化成为相对应的Message Exchange Pattern——MEP(Request/Response; One-way; Duplex)。所以Contract解决的是What functionalities do the Service provide?
  • Behavior: Behavior的主要作用是定制Endpoint在运行时的一些必要的Behavior。比如Service 回调Client的Timeout;Client采用的Credential type;以及是否支持Transaction等。

当我们Host一个WCF Service的时候,我们必须给他定义一个或多个Endpoint,然后service通过这个定义的Endpoint进行监听来自Client端的请求。当我们的Application需要调用这个Service的时候,因为Client 和Service是通过Endpoint的进行通信的, 所以我们必须为我们的Application定义Client端的Endpoint。只有当Client的Endpoint和Service端某个Endpoint相互匹配(Service端可以为一个Service定义多个Endpoint),Client端的请求才能被Service端监听到。也就是说,我们只有在Client具有一个与Service端完全匹配的Endpoint,我们才能调用这个Service。而这种匹配是比较严格的,比如从匹配Address方面,Client端和Service端的Endpoint Address不仅仅在URI上要完全匹配Service, 他们的Headers也需要相互匹配。对于Binding, 一般地,Client需要有一个与Service端完全一样的Binding,他们之间才能通信。

Sample

首先给一个Sample,以便我们对在WCF Service Aplication中如何定义Endpoint有一个感性的认识。整个Solution的结构参照下图,我的上一篇Blog([原创]我的WCF之旅(1):创建一个简单的WCF程序 )中有详细的介绍。你也可以通过后面的Link下载相应的Source Code(http://www.cnblogs.com/files/artech/Artech.WCFService.zip


1.Service Contract:Artech..WCfService.Contract/ServiceContract/IGeneralCalculator.cs

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

namespace Artech.WCFService.Contract
ExpandedBlockStart.gifContractedBlock.gif
{
[ServiceContract]
publicinterfaceIGeneralCalculator
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
[OperationContract]
doubleAdd(doublex,doubley);
}

}

2. Service: Artech.WCFSerice.Service/GeneralCalculatorService.cs

using System;
using System.Collections.Generic;
using System.Text;

using Artech.WCFService.Contract;

namespace Artech.WCFService.Service
ExpandedBlockStart.gifContractedBlock.gif
{
publicclassGeneralCalculatorService:IGeneralCalculator
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
ContractedSubBlock.gifExpandedSubBlockStart.gif
IGeneralCalculatorMembers#regionIGeneralCalculatorMembers

publicdoubleAdd(doublex,doubley)
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
returnx+y;
}


#endregion

}

}


3. Hosting: Artech.WCFService.Hosting/Program.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using Artech.WCFService.Contract;
using Artech.WCFService.Service;
using System.ServiceModel.Description;

namespace Artech.WCFService.Hosting
ExpandedBlockStart.gifContractedBlock.gif
{
classProgram
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
staticvoidMain(string[]args)
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
//HostCalculatorServiceViaCode();
HostCalculatorSerivceViaConfiguration();
}


ExpandedSubBlockStart.gifContractedSubBlock.gif
/**////<summary>
///Hostingaserviceusingmanagedcodewithoutanyconfiguraitoninformation.
///Pleasenotethattherelatedconfigurationdatashouldberemovedbeforecallingthemethod.
///</summary>

staticvoidHostCalculatorServiceViaCode()
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
UrihttpBaseAddress
=newUri("http://localhost:8888/generalCalculator");
UritcpBaseAddress
=newUri("net.tcp://localhost:9999/generalCalculator");

using(ServiceHostcalculatorSerivceHost=newServiceHost(typeof(GeneralCalculatorService),httpBaseAddress,tcpBaseAddress))
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
BasicHttpBindinghttpBinding
=newBasicHttpBinding();
NetTcpBindingtcpBinding
=newNetTcpBinding();

calculatorSerivceHost.AddServiceEndpoint(
typeof(IGeneralCalculator),httpBinding,string.Empty);
calculatorSerivceHost.AddServiceEndpoint(
typeof(IGeneralCalculator),tcpBinding,string.Empty);

ServiceMetadataBehaviorbehavior
=calculatorSerivceHost.Description.Behaviors.Find<ServiceMetadataBehavior>();
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
if(behavior==null)
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
behavior
=newServiceMetadataBehavior();
behavior.HttpGetEnabled
=true;
calculatorSerivceHost.Description.Behaviors.Add(behavior);
}

else
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
behavior.HttpGetEnabled
=true;
}

}


calculatorSerivceHost.Opened
+=delegate
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
Console.WriteLine(
"CalculatorServicehasbeguntolisten");
}
;

calculatorSerivceHost.Open();

Console.Read();
}

}


staticvoidHostCalculatorSerivceViaConfiguration()
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
using(ServiceHostcalculatorSerivceHost=newServiceHost(typeof(GeneralCalculatorService)))
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
calculatorSerivceHost.Opened
+=delegate
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
Console.WriteLine(
"CalculatorServicehasbeguntolisten");
}
;

calculatorSerivceHost.Open();

Console.Read();
}

}

}

}

4. Service.svc: http://localhost/WCFService/ GeneralCalculatorService.svc

< %@ServiceHost Language ="C#" Debug ="true" Service ="Artech.WCFService.Service.GeneralCalculatorService" % >

5. Client: Artech.WCFService.Client/ GeneralCalculatorClient.cs & Program.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using Artech.WCFService.Contract;

namespace Artech.WCFService.Client
ExpandedBlockStart.gifContractedBlock.gif
{
classGeneralCalculatorClient:ClientBase<IGeneralCalculator>,IGeneralCalculator
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
publicGeneralCalculatorClient()
:
base()
ExpandedSubBlockStart.gifContractedSubBlock.gif
{}

publicGeneralCalculatorClient(stringendpointConfigurationName)
:
base(endpointConfigurationName)
ExpandedSubBlockStart.gifContractedSubBlock.gif
{}

publicGeneralCalculatorClient(Bindingbinding,EndpointAddressaddress)
:
base(binding,address)
ExpandedSubBlockStart.gifContractedSubBlock.gif
{}

ContractedSubBlock.gifExpandedSubBlockStart.gif
IGeneralCalculatorMembers#regionIGeneralCalculatorMembers

publicdoubleAdd(doublex,doubley)
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
returnthis.Channel.Add(x,y);
}


#endregion

}

}


using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using Artech.WCFService.Contract;

namespace Artech.WCFService.Client
ExpandedBlockStart.gifContractedBlock.gif
{
classProgram
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
staticvoidMain()
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
try
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
//InvocateCalclatorServiceViaCode();

InvocateCalclatorServiceViaConfiguration();
}

catch(Exceptionex)
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
Console.WriteLine(ex.Message);
}


Console.Read();
}


staticvoidInvocateCalclatorServiceViaCode()
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
BindinghttpBinding
=newBasicHttpBinding();
BindingtcpBinding
=newNetTcpBinding();

EndpointAddresshttpAddress
=newEndpointAddress("http://localhost:8888/generalCalculator");
EndpointAddresstcpAddress
=newEndpointAddress("net.tcp://localhost:9999/generalCalculator");
EndpointAddresshttpAddress_iisHost
=newEndpointAddress("http://localhost/wcfservice/GeneralCalculatorService.svc");

Console.WriteLine(
"Invocateself-hostcalculatorservice");

ContractedSubBlock.gifExpandedSubBlockStart.gif
InvocateSelf-hostservice#regionInvocateSelf-hostservice
using(GeneralCalculatorClientcalculator_http=newGeneralCalculatorClient(httpBinding,httpAddress))
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
using(GeneralCalculatorClientcalculator_tcp=newGeneralCalculatorClient(tcpBinding,tcpAddress))
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
try
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
Console.WriteLine(
"Begintoinvocatecalculatorserviceviahttptransport");
Console.WriteLine(
"x+y={2}wherex={0}andy={1}",1,2,calculator_http.Add(1,2));

Console.WriteLine(
"Begintoinvocatecalculatorserviceviatcptransport");
Console.WriteLine(
"x+y={2}wherex={0}andy={1}",1,2,calculator_tcp.Add(1,2));
}

catch(Exceptionex)
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
Console.WriteLine(ex.Message);
}

}

}

#endregion


Console.WriteLine(
"\n\nInvocateIIS-hostcalculatorservice");

ContractedSubBlock.gifExpandedSubBlockStart.gif
InvocateIIS-hostservice#regionInvocateIIS-hostservice
using(GeneralCalculatorClientcalculator=newGeneralCalculatorClient(httpBinding,httpAddress_iisHost))
ExpandedSubBlockStart.gif
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 、2项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 、资5源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值