WCF messages behind an operation

Damir Dobric Posts

Next talks:

 

    

Follow me on Twitter: #ddobric



 

 

Archives

When one service is implemented, there has to be a class or interface marked with attribute ServiceContract. Exactly this attribute
hides the beginning of the magic behind WCF, which allows one to build the real service based on class. Next example shows such implementation:

[ServiceContract]
public interface IMyExtendedService
{
 [OperationContract]
 string op1(string hello);

 [OperationContract]
 void op2(string hello);
}


public class MyExtendedService : IMyExtendedService
{
  
public void op1(string hello)
  
{
     return hello;
   }

   public void op2(string hello)
   {
     return;
   }
}


The question at this point is how this operation is defined under the hub? In general everything is described in the service description. This description describes all endpoints, contracts an operation in each contract. Following code snippet shows the description of the operation op2 in the WSDL:

<xs:element name="op2">
 <xs:complexType>
 
<xs:sequence >
    
 <xs:element minOccurs="0" name="hello" nillable="true" type="xs:string" /> 
   </xs:sequence>
 </xs:complexType>
</xs:element>

<xs:element name="op2Response">
 
<xs:complexType>
    
<xs:sequence />
  </xs:complexType>
</xs:element>


Analog, messages in the operation description look like shown below:

Action="http://tempuri.org/IMyExtendedService/op2", Direction=Input, MessageType=null

Action="http://tempuri.org/IMyExtendedService/op2Response", Direction=Output, MessageType=null


It is obvious, that the operation op2 is defined with two messages. One with Input direction and one with output direction. Interesting is that the operation does not returns the value (void), but it is not marked as one way. It means the property IsOneWay is set on false. In this specific case there is response message which is empty.
If the same operation is now marked with atrtribute IsOneWay=true there will be only one message with Input direction:

[OperationContract(IsOneWay=true)]
void op2(string hello);


<xs:element name="op2">
<xs:complexType>

<xs:sequence >
<xs:element minOccurs="0" name="hello" nillable="true" type="xs:string" />
</xs:sequence>

</xs:complexType>
</xs:element>

Action="http://tempuri.org/IMyExtendedService/op2", Direction=Input, MessageType=null


By using of duplex things get a little bit more complicated. For example, if the service interface is changed to support duplex,
it can happen that there one message only, but this time with direction Output.

[ServiceContract(CallbackContract=typeof(ICallback))]
public interface IMyExtendedService
{
 
[OperationContract]
 string op1(string hello);

 [OperationContract]
 void op2(string hello);
}


public interface ICallback
{
  [OperationContract(IsOneWay = true)]
  [CustomOperationBehavior]
  void SignalAlarm(string reminderMessage);
}

Please note that direction of message is very important for Service Mode during creating of the factory and listener classer. Depending of message patterns in the contract, service model will during initialization process select the proper channel shape.


Posted Mar 12 2007, 01:30 AM by Damir Dobric
Filed under:
developers.de is a .Net Community Blog powered by daenet GmbH.