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 )
![[原创]我的WCF之旅(2):Endpoint Overview](http://img.e-com-net.com/image/product/19bdeae2cdab4982870331489c983602.jpg)
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

{
[ServiceContract]
publicinterfaceIGeneralCalculator


{
[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

{
publicclassGeneralCalculatorService:IGeneralCalculator


{

IGeneralCalculatorMembers#regionIGeneralCalculatorMembers

publicdoubleAdd(doublex,doubley)


{
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

{
classProgram


{
staticvoidMain(string[]args)


{
//HostCalculatorServiceViaCode();
HostCalculatorSerivceViaConfiguration();
}


/**////<summary>
///Hostingaserviceusingmanagedcodewithoutanyconfiguraitoninformation.
///Pleasenotethattherelatedconfigurationdatashouldberemovedbeforecallingthemethod.
///</summary>
staticvoidHostCalculatorServiceViaCode()


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

using(ServiceHostcalculatorSerivceHost=newServiceHost(typeof(GeneralCalculatorService),httpBaseAddress,tcpBaseAddress))


{
BasicHttpBindinghttpBinding=newBasicHttpBinding();
NetTcpBindingtcpBinding=newNetTcpBinding();

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

ServiceMetadataBehaviorbehavior=calculatorSerivceHost.Description.Behaviors.Find<ServiceMetadataBehavior>();


{
if(behavior==null)


{
behavior=newServiceMetadataBehavior();
behavior.HttpGetEnabled=true;
calculatorSerivceHost.Description.Behaviors.Add(behavior);
}
else


{
behavior.HttpGetEnabled=true;
}
}

calculatorSerivceHost.Opened+=delegate


{
Console.WriteLine("CalculatorServicehasbeguntolisten
");
};

calculatorSerivceHost.Open();

Console.Read();
}
}

staticvoidHostCalculatorSerivceViaConfiguration()


{
using(ServiceHostcalculatorSerivceHost=newServiceHost(typeof(GeneralCalculatorService)))


{
calculatorSerivceHost.Opened+=delegate


{
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

{
classGeneralCalculatorClient:ClientBase<IGeneralCalculator>,IGeneralCalculator


{
publicGeneralCalculatorClient()
:base()


{}

publicGeneralCalculatorClient(stringendpointConfigurationName)
:base(endpointConfigurationName)


{}

publicGeneralCalculatorClient(Bindingbinding,EndpointAddressaddress)
:base(binding,address)


{}


IGeneralCalculatorMembers#regionIGeneralCalculatorMembers

publicdoubleAdd(doublex,doubley)


{
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

{
classProgram


{
staticvoidMain()


{
try


{
//InvocateCalclatorServiceViaCode();

InvocateCalclatorServiceViaConfiguration();
}
catch(Exceptionex)


{
Console.WriteLine(ex.Message);
}

Console.Read();
}

staticvoidInvocateCalclatorServiceViaCode()


{
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");


InvocateSelf-hostservice#regionInvocateSelf-hostservice
using(GeneralCalculatorClientcalculator_http=newGeneralCalculatorClient(httpBinding,httpAddress))


{
using(GeneralCalculatorClientcalculator_tcp=newGeneralCalculatorClient(tcpBinding,tcpAddress))


{
try


{
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)


{
Console.WriteLine(ex.Message);
}
}
}
#endregion

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


InvocateIIS-hostservice#regionInvocateIIS-hostservice
using(GeneralCalculatorClientcalculator=newGeneralCalculatorClient(httpBinding,httpAddress_iisHost))

display:
分享到:
评论