wcf学习笔记
引用System.ServiceMode程序集(WCF框架的绝大部分实现和API定义在该程序集中)
契约(Contract):
WCF包含四种类型的契约:服务契约、数据契约、消息契约和错误契约。
服务契约:
从功能上讲,服务契约抽象了服务提供的所有操作;而站在消息交换的角度来看,服务契约则定义了基于服务调用的消息交换过程中,请求消息和回复消息的结构,以及采用的消息交换模式。
例:把接口定义为服务契约
using System.ServiceModel;
namespace Contracts
{
[ServiceContract(Name="CalculatorService", Namespace="http://www.artech.com/")]//契约名称,命名空间
public interface ICalculator
{
[OperationContract]
double Add(double x, double y);
服务就是接口实现
namespace Services
{
public class CalculatorService:ICalculator
{
public double Add(double x, double y)
{
寄宿:
绑定(Binding):
绑定实现了通信的所有细节,包括网络传输、消息编码,以及其他为实现某种功能(比如安全、可靠传输、事务等)对消息进行的相应处理。WCF中具有一系列的系统定义绑定,比如BasicHttpBinding、WsHttpBinding、NetTcpBinding等
地址(Address):
契约是对服务操作的抽象,也是对消息交换模式以及消息结构的定义。
生成exe文件(控制台):
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(CalculatorService)))
{
host.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), "http://127.0.0.1:9999/calculatorservice");
if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)
{
ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
behavior.HttpGetEnabled = true;
behavior.HttpGetUrl = new Uri("http://127.0.0.1:9999/calculatorservice/metadata");
host.Description.Behaviors.Add(behavior);
}
host.Opened += delegate
{
Console.WriteLine("CalculaorService已经启动,按任意键终止服务!");
};
host.Open();
Console.Read();
}
}
或配 config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="metadataBehavior">
<serviceMetadata httpGetEnabled="true" httpGetUrl="http://127.0.0.1:9999/calculatorservice/metadata"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="metadataBehavior" name="Services.CalculatorService">
<endpoint address="http://127.0.0.1:9999/calculatorservice" binding="wsHttpBinding" contract="Contracts.ICalculator" />
</service>
</services>
</system.serviceModel>
</configuration>
生成网站
每一个ASP.NET Web服务都具有一个.asmx文本文件
基于IIS的服务寄宿要求相应的WCF服务具有相应的.svc文件
摘自:http://www.cnblogs.com/artech/archive/2007/02/26/656901.html
浙公网安备 33010602011771号