1、地址( address )
WCF中地址以统一资源标识符(URI)的形式指定,它用来标识消息发送和接受的目的地,并且它由 通讯协议 和 位置路径 两部分组成。例如: http://192.168.1.1:8000/ 表明通讯协议为http,位置路径是 192.168.1.1 的8000端口。位置路径很好理解,就是IP加端口号。对于初学者往往忽略了其中包含的通讯协议,除了http之外,还可以指定为:
TCP地址
使用TCP协议进行传输,其形式为:net.tcp://localhost:8000/
IPC地址
使用net.pipe进行传输,其形式为:net.pipe://localhost/
MSMQ地址
使用Microsoft Message Queue
机制
进行传输,其形式为:net.msmq://localhost/
对等网地址
使用net.p2p进行传输,其形式为:net.p2p://localhost/
下面看看配置文件中的实际例子:
<
host
>
<
baseAddresses
>
<
add
baseAddress
= "http://localhost:8731/"
/>
</
baseAddresses
>
</
host
>
<
endpoint
address
="http://localhost:8731/Service"
binding
="basicHttpBinding"
contract
="Wcf_Address_Config.IService1"
>
</
endpoint
>
也可以写成相对路径:
<
host
>
<
baseAddresses
>
<
add
baseAddress
= "http://localhost:8731/"
/>
</
baseAddresses
>
</
host
>
<
endpoint
address
="Service1"
binding
="basicHttpBinding"
contract
="Wcf_Address_Config.IService1"
></
endpoint
>
那么在编程中如何设置地址呢?
EndpointAddress address
=
new
EndpointAddress(
"
http://127.0.0.1:2136/Service1
"
);
Binding binding
=
new
BasicHttpBinding();
wcf.IService1 service
=
new
wcf.Service1Client(binding, address);
2、绑定( binding )
绑定元素表示绑定的特定部分,如传输协议、编码、基础结构级协议(如 WS-ReliableMessaging)的实现以及通信堆栈的其他任何要素。绑定也是组成服务终结点的三要素之一。WCF基库中提供了很多可选择的预绑定:
basicHttpBinding
一个绑定,适用于与符合 WS-Basic Profile 的 Web 服务(例如基于 ASP.NET Web 服务 (ASMX) 的服务)进行的通信。 此绑定使用 HTTP 作为传输协议,并使用文本/XML 作为默认的消息编码。传输协议为:HTTP/HTTPS 编码格式为:Text,MTOM
wsHttpBinding
一个安全且可互操作的绑定,适合于非双工服务约定。传输协议为:HTTP/HTTPS 编码格式为:Text,MTOM
wsDualHttpBinding
一个安全且可互操作的绑定,适用于双工服务协定或通过 SOAP 媒介进行的通信。传输协议为:HTTP 编码格式为:Text,MTOM
netTcpBinding
一个安全且经过优化的绑定,适用于 WCF 应用程序之间跨计算机的通信。传输协议为:TCP 编码格式为:Binary
netPeerTcpBinding
一个支持多计算机安全通信的绑定。传输协议为:P2P 编码格式为:Binary
3、契约( contract )
在WCF中一共包含四种契约,1)服务契约:服务契约将多个相干的操作联系在一起,组成单个功能元素,契约可以定义服务级设置,如果服务的命名空间,对应的回调契约及其其他此类设置。2)数据契约,服务使用的数据类型必须在元数据中进行描述,以使其他各方可以与该服务进行交互操作,数据类型的说明称谓数据契约,而这些类型可以在消息的任何部分使用,如果服务是用简单类型,没有必要显示使用数据契约。4)数据契约:可以将错误契约与服务操作进行关联,以指示可能返回到调用放的错误,一个操作可能具有零各或多个与其相关联的错误,这些错误是在编程模型中建模为异常的SOA错误,5)消息契约:消息契约描述消息的格式,他会声明消息元素应包含在消息头中还是包含在消息正文中,应该对消息的任何元素应用何种级别的安全。契约也是组成服务终结点的三要素之一。
4、终结点( endpoint )
一个终结点有三个要素组成,终结点用来发送或接收消息的构造,一个终结点就相当于服务等公共接口,每个服务可以拥有一个或多个终结点,由于每个服务都只拥有一个地址,所以某个服务拥有的所有终结点共享一个地址,终结点的配置或者编程,并不属于业务逻辑的编程,所以WCF设计分离了终结点的定义和契约的具体实现。
5、元数据
服务的元数据描述服务的特性,外部实体需要了解这些特性以便于该服务进行通信,服务所公开的元数据包括XML架构文件和WSDL文档,启用元数据后,WCF通过检查服务以及终结点自动生成服务元数据,若要发布服务元数据,必须显示启动元数据行为,在WCF中,可以为元数据设置一个专用的终结点。
其配置: < serviceMetadata httpGetEnabled ="True" httpGetUrl ="http://localhost:8731/Service" />
6、宿主
服务必须承载与某个进程中,宿主的控制服务的生存期的应用程序,服务可以是自寄宿,也可以由现有的寄宿进程进行管理,从内部实现来看,一个服务宿主进程,可以包含一个或多个应用程序域,而每个应用程序域理论上有可以被放入到任意个服务宿主,每个服务宿主可以拥由任意个上下文,每个上下文可以有0个或1和服务示例。
最后给出一个msdn中wcf的实例:注意引用 System.ServiceModel.dll
using
System;
using
System.ServiceModel;
using
System.ServiceModel.Description;
namespace
Microsoft.ServiceModel.Samples
{
// Define a service contract.
[ServiceContract(Namespace =
"http://Microsoft.ServiceModel.Samples"
)]
public
interface
ICalculator
{
[OperationContract]
double
Add(
double
n1,
double
n2);
[OperationContract]
double
Subtract(
double
n1,
double
n2);
[OperationContract]
double
Multiply(
double
n1,
double
n2);
[OperationContract]
double
Divide(
double
n1,
double
n2);
}
// Service class that implements the service contract.
// Added code to write output to the console window.
public
class
CalculatorService : ICalculator
{
public
double
Add(
double
n1,
double
n2)
{
double
result = n1 + n2;
Console.WriteLine(
"Received Add({0},{1})"
, n1, n2);
Console.WriteLine(
"Return: {0}"
, result);
return
result;
}
public
double
Subtract(
double
n1,
double
n2)
{
double
result = n1 - n2;
Console.WriteLine(
"Received Subtract({0},{1})"
, n1, n2);
Console.WriteLine(
"Return: {0}"
, result);
return
result;
}
public
double
Multiply(
double
n1,
double
n2)
{
double
result = n1 * n2;
Console.WriteLine(
"Received Multiply({0},{1})"
, n1, n2);
Console.WriteLine(
"Return: {0}"
, result);
return
result;
}
public
double
Divide(
double
n1,
double
n2)
{
double
result = n1 / n2;
Console.WriteLine(
"Received Divide({0},{1})"
, n1, n2);
Console.WriteLine(
"Return: {0}"
, result);
return
result;
}
}
class
Program
{
static
void
Main(
string
[] args)
{
// Step 1 of the address configuration procedure: Create a URI to serve as the base address.
Uri baseAddress =
new
Uri(
"http://localhost:8000/ServiceModelSamples/Service"
);
// Step 2 of the hosting procedure: Create ServiceHost
ServiceHost selfHost =
new
ServiceHost(
typeof
(CalculatorService), baseAddress);
try
{
// Step 3 of the hosting procedure: Add a service endpoint.
selfHost.AddServiceEndpoint(
typeof
(ICalculator),
new
WSHttpBinding(),
"CalculatorService"
);
// Step 4 of the hosting procedure: Enable metadata exchange.
ServiceMetadataBehavior smb =
new
ServiceMetadataBehavior();
smb.HttpGetEnabled =
true
;
selfHost.Description.Behaviors.Add(smb);
// Step 5 of the hosting procedure: Start (and then stop) the service.
selfHost.Open();
Console.WriteLine(
"The service is ready."
);
Console.WriteLine(
"Press <ENTER> to terminate service."
);
Console.WriteLine();
Console.ReadLine();
// Close the ServiceHostBase to shutdown the service.
selfHost.Close();
}
catch
(CommunicationException ce)
{
Console.WriteLine(
"An exception occurred: {0}"
, ce.Message);
selfHost.Abort();
}
}
}
}

