您的位置:首页 > 其它

WCF学习笔记之契约(Contract)

2012-08-03 00:21 387 查看

一、初识契约(Contract)

    契约简单讲就是服务端和客户端进行消息交换定义的一种交换协议。在wcf中服务契约的定义涉及到System.ServiceModel.ServiceContractAttribute和

System.ServiceModel.OperationContractAttribute这两个自定义特性。其中应用在ServiceContractAttribute服务契约特性上的特性[AttributeUsage(AttributeTargets.

Class)]表示服务契约特性只能应用于类或接口;应用在OperationContractAttribute服务契约特性上的特性[AttributeUsage(AttributeTargets.Method)]表示操作契约特性只能应用于方法成员。

二、ServiceContractAttribute和OperationContractAttribute的属性成员

      1、 ServiceContractAttribute特性具有以下一些成员:

public sealed class ServiceContractAttribute : Attribute


[code]{


public Type CallbackContract { get; set; }




  public string ConfigurationName { get; set; }


 


  public bool HasProtectionLevel { get; }




  public string Namespace { get; set; }


 


  public ProtectionLevel ProtectionLevel { get; set; }




  public SessionMode SessionMode { get; set; }


}

[/code]

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

       我们在定义一个类或接口时为该类型应用[ServiceContract]特性,同时还可以指定服务契约的一个或多个属性,最常用的就是指定命名空间和名称,比如:[ServiceContract(Name="CalculatorService",Namespace=”http://www.ainote.cn”)]。

       服务契约在终结点配置项的contract属性中所表示的值,也是服务契约的ConfigurationName属性的值,默认是服务契约类型的完全名称(服务契约的CLR类型名称),我们也可以配置   ServiceContract   特性的ConfigurationName属性值来指定一个契约配置名称。

服务契约的默认配置Stone.Contract.Contract.ICalculator

<endpoint  address="http://127.0.0.1:126/CalculatorService"


 binding="ws2007HttpBinding"


 contract="Stone.Contract.Contract.ICalculator"></endpoint>


通过ConfigurationName属性指定一个配置值CalculatorCatract

<endpoint address="http://127.0.0.1:99/CalculatorService"


[code]contract="CalculatorCatract"


binding="wsHttpBinding"></endpoint>

[/code]

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

在双工通信模式下面还需要在ServiceContract特性应用中指定CallbackContract类型typeof(ICalculatorCallback):

  [ServiceContract(Name="CalculatorService",Namespace="http://www.ainote.cn",


[code] ConfigurationName="CalculatorCatract",CallbackContract=typeof(ICalculatorCallback))]


public  interface ICalculator


{


}

[/code]

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

   2、 OperationContractAttribute特性具有以下一些成员:

public sealed class OperationContractAttribute : Attribute


[code]{


  public string Action { get; set; }


  public bool AsyncPattern { get; set; }


  public bool HasProtectionLevel { get; }


  public bool IsOneWay { get; set; }


  public string Name { get; set; }


  public ProtectionLevel ProtectionLevel { get; set; }


  public string ReplyAction { get; set; }


}

[/code]

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

      在我们定义的服务类型里面需要指定一组操作方法,这些操作方法要作为契约的一部分必须应用[OperationContract],同时还可以指定操作契约的一个或多个属性,最常用的就是指定Name、Action和ReplyAction。因为服务契约里面的每个操作都要有一个唯一的名称,所以面向对象编程的重载要素在这儿就不适用,不过我们可以通过为重名的每个操作方法的操作契约的名称属性设置一个唯一的名称来表示这个操作方法的重载,如以下设置:

[ServiceContract(Name = "CalculatorService", Namespace = "http://www.ainote.cn")]


[code] public interface ICalculator1


{


  [OperationContract(Name = "AddDouble")]


  double Add(double x, double y);


  [OperationContract(Name = "AddInt")]


  int Add(int x, int y);


}

[/code]

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

     [OperationContract]的Action和ReplyAction属性,默认情况下Action和ReplyAction属性的值是有服务契约命名空间、服务契约的名称以及操作名称三者共同组成,比如:http://www.ainote.cn/CalculatorService/Add;ReplyAction属性的值则是在Action属性值的基础上加上Response后缀。我们也可以在服务契约里面为这两个属性指定一个唯一的值。如:

[ServiceContract(Name="CalculatorService",Namespace="http://www.ainote.cn",


[code] ConfigurationName="CalculatorCatract",CallbackContract=typeof(ICalculatorCallback))]


public  interface ICalculator


{


  [OperationContract(Action = "http://www.ainote.cn/CalculatorService/AddDouble",


ReplyAction = "http://www.ainote.cn/CalculatorService/AddDoubleReply")]


  double Add(double x, double y);


  [OperationContract(Name = "AddInt", Action = "http://www.ainote.cn/CalculatorService/Add",


ReplyAction = "http://www.ainote.cn/CalculatorService/AddReply")]


  int Add(int x, int y);


}

[/code]

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

三、服务契约的继承

      服务契约本质上是一个类或接口,所以他具有面向对象的一些特征,但是ServiceContractAttribute特性所应用的AttributeUsage的Inherited属性设置为false所以服务契约不具有继承性,OperationContractAttribute特性所应用的AttributeUsage的Inherited属性设置为true,他就具有继承性。不过为了支持服务契约的继承性我们必须通过在派生类型上应用ServiceContractAttribute特性。

[ServiceContract]


[code] public interface IBase


{


  [OperationContract]


  void BaseMethod(string str);


}

[ServiceContract]


public interface IDerived


{


  [OperationContract]


  void DerivedMethod(string str);


}

[/code]

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

终结点的配置如下:

<service name="Stone.Contract.Service.DerivedService" >


[code] <endpoint address="http://127.0.0.1:126/DerivedService"


 binding="ws2007HttpBinding"


 contract="Stone.Contract.Contract.IDerived"></endpoint>


</service>


<client>


<endpoint  name="DerivedService_Client" address="http://127.0.0.1:126/DerivedService"


binding="ws2007HttpBinding"


contract="Stone.Contract.Contract.IDerived"></endpoint>


<endpoint name="BaseService_Client" address="http://127.0.0.1:126/DerivedService"


    binding="ws2007HttpBinding"


    contract="Stone.Contract.Contract.IBase"></endpoint>


</client>

[/code]

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

客户端就可以分别创建IDerived和IBase的代理,调用各自契约里面的操作方法。

四、消息交换模式

       WCF 支持单向、请求-回复、双工三种消息交换模式,其中请求-回复模式是服务操作的默认消息交换模式,单向模式也是最基本的交换模式,通过操作契约就可以直接指定单向和请求-回复模式,双工模式的消息交换需要服务端对客户端的回调。

 请求-回复模式

       对于任何一个服务操作默认就是采用的这种消息交换模式,输入参数列表决定请求消息的格式。对于有返回值的操作方法则由返回值类型决定回复消息的格式;对没有返回值的操作方法(也就是返回值为void的方法)它的回复消息的主体就是为空;对于参数上标注了ref和out修饰符的操作方法对应回复消息由返回类型、out和ref参数的类型共同决定。

[ServiceContract(Name="CalculatorService",Namespace="http://www.ainote.cn",


[code] ConfigurationName="CalculatorCatract")]


public  interface ICalculator


{


  [OperationContract]


  double Add(double x, double y);


}

[/code]

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

      当把服务的元素数据以WSDL的方式发布出来时,服务契约对应WSDL中的<portType>节点,该节点具有一系列的<operations>子元素,这些<operations>子元素 表示服务契约中定义的操作契约,表示操作契约的<operations>元素通过<input>元素和<output>元素的有序组合来描述消息的交换模式。 <input><output> 元素的message属性指定WSDL中的<message>元素所对应的名称,而表示消息组成部分的<part>子元素则通过element引用的以XSD定义的XML元素所表示相应的元素节点结构。在<types> 元素节点的XSD定义中名称为AddAddResponse的xml元素分别定义请求消息和回复消息的结构。

用XSD定义的XML元素所表示的[b]<portType>节点:[/b]

<wsdl:portType name="CalculatorService">


[code] <wsdl:operation name="Add">


<wsdl:input wsaw:Action="http://www.ainote.cn/CalculatorService/Add" message="tns:CalculatorService_Add_InputMessage" />


<wsdl:output wsaw:Action="http://www.ainote.cn/CalculatorService/AddResponse" message="tns:CalculatorService_Add_OutputMessage" />


</wsdl:operation>

<wsdl:operation name="Substract">


<wsdl:input wsaw:Action="http://www.ainote.cn/CalculatorService/Substract" message="tns:CalculatorService_Substract_InputMessage" />


</wsdl:operation>


</wsdl:portType>

[/code]

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

用XSD定义的XML元素所表示的[b]<message>节点:[/b]

<wsdl:message name="CalculatorService_Add_InputMessage">


[code] <wsdl:part name="parameters" element="tns:Add" />


</wsdl:message>


<wsdl:message name="CalculatorService_Add_OutputMessage">


<wsdl:part name="parameters" element="tns:AddResponse" />


</wsdl:message>

[/code]

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

用XSD定义的XML元素所表示的[b]<types>节点:[/b]

<wsdl:types>


[code]<xsd:schema targetNamespace="http://www.ainote.cn/Imports">


<xsd:import schemaLocation="http://127.0.0.1:99/CalculatorService/Metadata?xsd=xsd0" namespace="http://www.ainote.cn" />


<xsd:import schemaLocation="http://127.0.0.1:99/CalculatorService/Metadata?xsd=xsd1" namespace="http://schemas.microsoft.com/2003/10/Serialization/" />


</xsd:schema>


</wsdl:types>

[/code]

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

单向模式

       如果我们调用某个服务时不需要有返回结果,并且也不关心服务调用是否成功执行,在这种情况下我们就可以采用单向的消息交换模式。在采用单向模式的服务调用时,一旦消息进入网络传输层,就马上返回,此后的过程完全与客户端无关了。

      如果需要将某个方法定义成单向服务操作,需要将应用在该服务操作上的 OperationContractAttribute 特性的IsOneWay 显式的设置成true,并且单向服务操作不允许有返回值,也不允许参数有任何的ref和out修饰符,所以只有返回类型为void没有任何引用参数 (就是任何参数不带ref和out修饰符)的方法才能定义成单向服务操作。

    [ServiceContract(Name="CalculatorService",Namespace="http://www.ainote.cn",


[code] ConfigurationName="CalculatorCatract")]


public  interface ICalculator


{


  [OperationContract(IsOneWay = true)]


  void Substract(double x, double y);


}

[/code]

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

     在基于单向的消息交换模式下的服务操作只有输入消息没有输出消息,所以单向服务操作在WSDL中用XSD定义的XML元素结构在<operations> 元素下只有输入消息<input>元素。如下XML片段:

<wsdl:operation name="Substract">


[code] <soap12:operation soapAction="http://www.ainote.cn/CalculatorService/Substract" style="document" />


<wsdl:input>


<wsp:PolicyReference URI="#MyBinding_CalculatorService_Substract_Input_policy" />


<soap12:body use="literal" />


</wsdl:input>


</wsdl:operation>

[/code]

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }双工模式

      双工模式的消息交换,使参与消息交换的双方均可以向对方发送消息,使服务能回调客户端,同时还可以帮助我们实现基于发布-订阅的方式进行通信。定义双工通信的服务契约时还需要定义一个回调契约,并且服务终结点的绑定必须采用具有双工通信能力的NetTcpBindingWSDualHttpBinding。就传输协议本身来说,TCP 是支持双向通信的,而HTTP 不支持,所以WSDualHttpBinding 提供的双向通信本质上是通过在服务端和客户端各创建一个HTTP 通道以实现双向通信的回调。

   [ServiceContract(Name="CalculatorService",Namespace="http://www.ainote.cn",


[code] ConfigurationName="CalculatorCatract",CallbackContract=typeof(ICalculatorCallback))]


public  interface ICalculator


{


  [OperationContract(IsOneWay = true)]


  double Add(double x, double y);


  [OperationContract(IsOneWay = true)]


  void Substract(double x, double y);


  }

服务端配置:

[/code]

<configuration>


[code] <system.serviceModel>


<services>


<service name="Stone.Contract.Service.CalculatorService" behaviorConfiguration="CalculatorServiceBehavior">


  <!--netTcpBinding实现双工通信的回调-->


  <endpoint address="net.tcp://127.0.0.1:99/CalculatorService"


binding="netTcpBinding"


contract="CalculatorCatract"></endpoint>


  


  <!--wsDualHttpBinding实现双工通信回调-->


  <endpoint address="http://127.0.0.1:98/CalculatorService"


binding="wsDualHttpBinding"


contract="CalculatorCatract"></endpoint>


</service>


<behaviors>


<serviceBehaviors>


  <behavior name="CalculatorServiceBehavior">


    <serviceMetadata httpGetEnabled="true" httpGetUrl="http://127.0.0.1:999/CalculatorService/Metadata" />


    <serviceDebug includeExceptionDetailInFaults="true"/>


  </behavior>


 </serviceBehaviors>


</behaviors>


</system.serviceModel>


</configuration>

[/code]

客户端配置:

<configuration>


[code] <system.serviceModel>


<client>


<endpoint  name="CalculatorService_NetTcpClient"   address="net.tcp://127.0.0.1:99/CalculatorService"


binding="netTcpBinding"


contract="CalculatorCatract"></endpoint>


 


<!--wsDualHttpBinding实现双工通信回调-->


<endpoint name="CalculatorService_WsDaulClient" address="http://127.0.0.1:98/CalculatorService"


   binding="wsDualHttpBinding"


   contract="CalculatorCatract"></endpoint>


</client>


 </system.serviceModel>


</configuration>

[/code]

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }     客户端调用的时候需要通过 DuplexChannelFactory 创建服务代理。

 static void CalculatorServiceNetTcpClient()


[code] {


InstanceContext callback = new InstanceContext(new CalculatorCallbackService());


using (DuplexChannelFactory<ICalculator> channel = new DuplexChannelFactory<ICalculator>(callback, "CalculatorService_NetTcpClient"))


{


    ICalculator proxy = channel.CreateChannel();


    proxy.Substract(10,5);


}


  }

[/code]

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

      WCF服务学习笔记之契约就总结到这儿,有关契约的其他信息后续再补充。

      本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: