The operation in WCF is divided by the call sequence and the release of sessions.

Source: Internet
Author: User

The operation in WCF is divided by the call sequence and the release of sessions.
Operation demarcation

In the design of the WCF operation contract, there are sometimes calls in sequence. Some operations cannot be called first, and some operations must be called at last, for example, when taking out an item from a box, you must first open the box, and close the box should be executed after all the work is done.

public interface Box
{
    void Open(int boxId);
    int GetTotalFrenchletter();
    void Close();
}

To solve this problem, WCF provides the IsInitiating and IsTerminating attributes in OperationContractAttribute. The default value of IsInitiating is true, which indicates that the current operation can be called first. The default value of IsTerminating attribute is false, indicates that the service object will not be released after this method is executed. These two attributes can be used to control the requirements provided above.

In addition, after the default value is modified, when the service is installed, WCF verifies whether the service contract is defined as SessionMode. Required. If not, WCF throws an InvalidOperationExample exception.

The contract definition above can be redesigned:

[ServiceContract(SessionMode=SessionMode.Required)]
public interface Box
{
    [OperationContract(IsInitiating=true,IsTerminating=false)]
    void Open(int boxId);
    [OperationContract(IsInitiating = false, IsTerminating = false)]
    int GetTotalFrenchletter();
    [OperationContract(IsInitiating = false, IsTerminating = true)]
    void Close();
}

Attribute on the Open method has the same meaning as not adding it, but it looks clearer.

One thing to note is that, with reference to the preceding contract definition, after the Close call is executed, WCF releases the object asynchronously and closes the session, the client will no longer be able to call operations in the service through the current proxy.

Instance stopped

In the lifecycle of a service, the context is always accompanied by the entire process of creating and releasing a service instance. Then, for some purpose, WCF also provides the option of separating the two, the service instance can be stopped independently. The method is to set the ReleaseInstanceMode attribute of OperationBehaviorAttribute. It is an enumeration type named ReleaseInstanceMode and contains four values: AfterCall, BeforeCall, BeforeAndAfterCall, And None. The default value is None.

BeforeCall: Before calling the current operation, WCF will release the current service instance, create a new instance to replace it, and then call the method on the new instance;

AfterCall: the current service instance will be released after the current operation is called;

BefireAndAfterCall: This is a supplement to the first two settings. If OperationBehavior applies this value, the current method can be called after the BeforeCall or None method is marked, it can also be called after the AfterCall or None method is marked.

In the example above, we can make the following definitions:

public class Box : IBox
{
    public void Open(int boxId)
    {
        throw new NotImplementedException();
    }

    public int GetTotalFrenchletter()
    {
        throw new NotImplementedException();
    }

    [OperationBehavior(ReleaseInstanceMode=ReleaseInstanceMode.AfterCall)]
    public void Close()
    {
        lockBox();
    }
}

Even so, WCF still provides a method to stop a service instance directly, so that none of the above settings can find a perfect solution for your needs. Then, we should try not to use it unless we have to, because it destroys the separation of business logic and service lifecycle.

The method is simple. There is InstanceContext in OperationContext, and this attribute contains a ReleaseServiceInstance method. After this method is called, the service will be released:

[OperationBehavior(ReleaseInstanceMode=ReleaseInstanceMode.AfterCall)]
public void Close()
{
    lockBox();
    OperationContext.Current.InstanceContext.ReleaseServiceInstance();
}

Then, the technology described above is only some optimization technologies provided by WCF for special needs, which are generally not required to be used.

Related Article

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.