Operationcontractattribute

We already saw that each method that should be used for invocation on a WCF service, needs to be decorated with the OperationContractAttribute. So now we will look at it in more detail and explore what the properties do, how you can mimic some CLR concepts like inheritance and operation overloading.

Also we are going to look at all the ways an operation can be invoked by a service, which is determined by the proeprties of the OperationContract.

  1. The composition of the OperationContractAttribute (this section)
  2. What do the properties do
  3. Mimic CLR behavior like inheritance and operation overloading
  4. What modi are possible for methods

Which is defined as follows:

[AttributeUsageAttribute(AttributeTargets.Method)]
public sealed class OperationContractAttribute : Attribute
{ 
     public string Name { get; set; }
     public string Action { get; set; } 
     public bool IsOneWay { get; set; }
     public virtual object TypeId { get; } 
     public bool AsyncPattern { get; set; } 
     public bool IsInitiating { get; set; }
     public bool HasProtectionLevel { get; } 
     public bool IsTerminating { get; set; } 
     public string ReplyAction { get; set; } 
     public ProtectionLevel ProtectionLevel { get; set; }
     // other members
}

Next we are going to look at the properties and what they are used for, as well as we take a look at how to apply CLR concepts like operation overloading and Inheritance to WCF with the help of this properties.

Name

Simply lets you define the name of the operation in the SOAP message when the name of the CLR method should not be used. See Action/ReplyAction below.

Action and ReplyAction

The Action and ReplyAction are used to specify the name of the operation that is defined in WSDL. The default value for this properties are the ServiceContractNamespace, the ContractName (mostly the interface name, or the defined service name as I recommended in the overview of ServiceContractAttribute) the OperationName (defined or methodName) and the term Response in the case of ReplyAction.

So for Instance the following definition would yield a SOAP message like the one displayed below:

[ServiceContract(Namespace="mydomain", Name="myservice")]
public interface IMyService
{
      [OperationContract]
      string MyMethod();
}
<s:Envelope xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope">
  <s:Header>
    <a:Action>http://mydomain/MyMethodResponse</a:Action> 
  </s:Header>
  <s:Body>
    <MyMethodResponse xmlns="http://mydomain">
      <MyMethodResult>The service greets you: Hello!</MyMethodResult> 
    </MyMethodResponse>
  </s:Body>
</s:Envelope>

So these two properties can be used to enact fine grained control over WSDL. I seldom tend to use this properties, because I always specify the namespace and name of my ServiceContract and tend to use methodnames that show intent.

IsOneWay

If this property is set to true, it correlates to a fire-and-forget method. Those might be feasible if you are not interested in the result of the operation. Examples are queueing of messages, tracing or broadcasting like messages.

I say correlate because it is not truly one way operation, because with the operationcontext of WCF the client can still access the Exceptions that are propageted from the service to the client. We will see one way in the section about methods below.

IsInitiating and IsTerminating

These two properties control if a call on the service can initiate a session or terminate it respectively. This has a lot of uses, like capturing client side callbackcontracts from the operationcontext, notify when a client retreats and other scenarios.

You can also steer the communication in a way that a specific method needs to be called first. For this to work, you would set all methods to IsInitiating=false, becuase true is the default. The same is true for terminating, yet the default is false.

In the below example, if any other method then the connect method is called the service throws an exception. Is a method called after the isTerminating is called, also is an exception thrown.

[ServiceContract(Namespace="mydomain", Name="mysessionservice")]
public interface MySessionfulService
{
     [OperationContract(IsInitiating=true, IsTerminating=false)]
     void Connect();

     [OperationContract(IsInitiating=false, IsTerminating=false)]
     void OperateOnSessionfulState();

     [OperationContract(IsInitiating=false, IsTerminating=true)]
     void Disconnect();
}

ProtectionLevel

If you have set a default on the ServiceContract this can be overridden with this property.

AsyncPattern

When the AsyncPattern is set to true, the async invocation of methods from the client side or service side is enabled. This lets the client use the asynchronous programming (APM) pattern or TPL when calling service methods, which lets the client get control back immediately after a call to the service. I am going to talk about asynchronous method calls in WCF in the section about ways to invoke methods.

Operation overloading

Overloading like in CLR is not supported in WSDL. Yet you might want to use this feature. And with WCF you can do it with a little trick. The following would compile, yet thow an exception at service load time.

[ServiceContract]
public interface IMyService
{
    [OperationContract]
    void DoStuff();

    [OperationContract]
    void DoStuff(int input);
}

To support this kind of behavior you can simply utilize the Name property of the OperationContractAttribute like in the following example:

[ServiceContract]
public interface IMyService
{
    [OperationContract(Name="DoStuff")]
    void DoStuff();

    [OperationContract((Name="DoStuffWithInput")]
    void DoStuff(int input);
}

Inheritance

Inheritance in WCF is possible on the interface, yet you will need to implement the ServiceContractAttribute on the implementing classes, because the ServiceContractAttribute is not inheritable. Yet even it is possbile it might be better to factor your interfaces in a way that does not require you to use inheritance.

Ways to invoke Methods

With an operationcontract you can also specify the way a method is called. There are six different ways that I am going to briefly introduce.

  1.  Request and Reply (the default mode)
  2. One way calls (IsOneWay = true)
  3. Duplex callbacks
  4. Streaming
  5. Asynchrounous (see AsyncPattern)
  6. Queued messages

 

Categories: Contracts

0 Comments

Leave a Reply