Operations in WCF are delimited by invocation order and session release

Source: Internet
Author: User

Operation Demarcation

In the design of the WCF operation contract, sometimes there will be a number of calls in the order of the business, some operations can not be called first, some operations must be last called, such as in a box to take out a thing, you must first do open the box operation, and the closing of the box should be done after the completion of all work to be executed.

 Public Interface Box
{
void Open (int boxid);
int Gettotalfrenchletter ();
void Close ();
}

To address this practical need, WCF provides the isinitiating and IsTerminating properties in OperationContractAttribute, and the default value of IsInitiating is true, indicating that the current operation can be called by the first , the IsTerminating property defaults to False, which means that the service object will not be freed after this method has finished executing. You can use these two properties to control the requirements that are provided above.

In addition, after modifying the default values, WCF will verify that the service contract is defined as sessionmode.required, and if not, WCF throws a Invalidoperationexample exception when it is loaded into the service.

The contract definitions above can be redesigned:

 [ServiceContract (sessionmode=sessionmode.required)] 
public  < Span style= "color: #0000ff;" >interface Box
{
    [OperationContract (Isinitiating=, Isterminating=void Open (int Boxid);
    [OperationContract (isinitiating = false , IsTerminating = false )]
    int Gettotalfrenchletter ();
    [OperationContract (isinitiating = false , IsTerminating = true )]
    void Close ();
}

The attribute on the open method and does not add it is the same meaning, but looks a little clearer

One thing to note is that, referring to the contract definition above, after the close call executes, WCF releases the object asynchronously and closes the session, and the client will no longer be able to invoke the operation in the service through the current proxy.

Instance stop

In the life cycle of a service, the context is always accompanied by the creation of the service instance in the entire process of releasing, and then for some purposes, WCF also provides an option to detach both, allowing the service instance to be stopped separately. The method is to set the Releaseinstancemode property of the OperationBehaviorAttribute, which is an enumeration type named Releaseinstancemode that contains the Aftercall, Beforecall, Beforeandaftercall and none four values, the default value is None.

Beforecall: Before invoking the current operation, WCF frees the current service instance, then creates a new instance to replace it, and then invokes the method on the new instance;

Aftercall: The current service instance is freed after the current operation is invoked;

Befireandaftercall: It is a supplement to the first two settings, OperationBehavior if this value is applied, the current method can be called after a method that has the Beforecall or none marked. It can also be called after a method marked Aftercall or None.

In the example above, we can define the following

 PublicclassBox:ibox
{
PublicvoidOpen (intBOXID)
{
ThrowNewNotImplementedException ();
}

PublicintGettotalfrenchletter ()
{
Throw NewNotImplementedException ();
}

[OperationBehavior (Releaseinstancemode=releaseinstancemode.aftercall)]
PublicvoidClose ()
{
LockBox ();
}
}

Even so, WCF still provides a way to stop the service instance directly to meet the above setup without finding a perfect solution for your needs. And then, as a last resort, you should try not to use it because it destroys the separation of the business logic and the service's life cycle.

The method is simple, there is a instancecontext in OperationContext, and this property contains a Releaseserviceinstance method, after which the service will be freed:

[OperationBehavior (Releaseinstancemode=releaseinstancemode.aftercall)]
Public void Close ()
{
LockBox ();
OperationContext.Current.InstanceContext.ReleaseServiceInstance ();
}

Then, the techniques described above are just some of the optimizations that WCF provides for special needs, which are often not necessary to be used.

Operations in WCF are delimited by invocation order and session release

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.