http://www.microsoft.com/china/msdn/events/webcasts/shared/webcast/Series/WCF_Begin.aspx
服务和协定
服务协定使用接口,可以建模
定义服务协定:在类或接口上使用 ServiceContractAttribute属性标记
在方法上使用 OperationContractAttribute
服务操作不会传递对对象的引用,他们传递的只是对象的副本。
所以要求参数或返回值中使用的类型都是可序列化的。
基元类型都是可序列化的。
消息模式:
请求/答复:默认模式
单向消息模式:客户端不必等待操作完成
将IsOneWay 属性设置为true即可,默认是false
[OperationContract(IsOneWay=true)]
双工模式:服务和客户端均能够独立地向对方发送消息。
若要设计双工协定,还必须设计回调协定,并将该回调协定的类型分配给
ServiceContractAttribute 的 CallBackContract 属性。
数据协定: DataContract DataMember
双工模式例子:
建一个wcflibrary
namespace WcfServiceLibrary
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。
[ServiceContract(Namespace="http://Moicrosoft.ServiceModel.Samples",SessionMode = SessionMode.Required
, CallbackContract = typeof(ICalculatorDuplexCallback))]
public interface IService1
{
[OperationContract(IsOneWay = true)]
void Clear();
[OperationContract(IsOneWay = true)]
void AddTo(double n);
[OperationContract(IsOneWay = true)]
void SubtractFrom(double n);
[OperationContract(IsOneWay = true)]
void MultiplyBy(double n);
[OperationContract(IsOneWay = true)]
void DivideBy(double n);
// TODO: 在此添加您的服务操作
} public interface ICalculatorDuplexCallback
{
[OperationContract(IsOneWay = true)]
void Equals(double result);
[OperationContract(IsOneWay = true)]
void Equation(string eqn);
}
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class Service1 : IService1
{
double result;
string equation;
ICalculatorDuplexCallback callback = null;
public Service1()
{
result = 0.0D;
equation = result.ToString();
callback = OperationContext.Current.GetCallbackChannel<ICalculatorDuplexCallback>();
} public void Clear()
{
callback.Equation(equation + "=" + result.ToString());
result = 0.0D;
equation = result.ToString();
}
public void AddTo(double n)
{
result += n;
equation += "+" + n.ToString();
callback.Equals(result);
}
public void SubtractFrom(double n)
{
result -= n;
equation += "-" + n.ToString();
callback.Equals(result);
}
public void MultiplyBy(double n)
{
result *= n;
equation += "*" + n.ToString();
callback.Equals(result);
}
public void DivideBy(double n)
{
result /= n;
equation += "/" + n.ToString();
callback.Equals(result);
}
}
配置文件中修改两个地方:
<host>
<baseAddresses>
<add baseAddress = "http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary/Service1/" />
</baseAddresses>
</host>
<!-- Service Endpoints -->
<!-- 除非完全限定,否则地址将与上面提供的基址相关 -->
<endpoint address="" binding="wsDualHttpBinding" contract="WcfServiceLibrary.IService1">
在控制台应用程序中进行调试:
先引用WCF的WEB服务
namespace ConsoleApplication1
{
public class CallbackHandler : IService1Callback
{
public void Equals(double result)
{
Console.WriteLine("result{0}",result);
}
public void Equation(string result)
{
Console.WriteLine("Equation{0}", result);
}
}
class Program
{
static void Main(string[] args)
{
InstanceContext instanceContext = new InstanceContext(new CallbackHandler());
ServiceReference1.Service1Client client = new ServiceReference1.Service1Client(instanceContext);
Console.WriteLine("Enter .");
double value = 100.00D;
client.AddTo(value); value = 50.00D;
client.SubtractFrom(value); value = 17.65D;
client.MultiplyBy(value); value = 2.00D;
client.DivideBy(value);
client.Clear();
Console.ReadLine();
client.Close();
}
}
}