WCF-service instance Management Mode

Source: Internet
Author: User

WCF provides three instance context modes: precall, presession, and single. Developers pass
Servicebehavior. instancecontextmode can easily control the instance management mode of the service object. When
When releasing a service object, the system checks whether the object implements the idisposable interface and calls its dispose
To release relevant resources in a timely manner, and to observe the object release behavior.

1. precall

In precall mode, even if the same proxy object is used, a service instance is created for each call. After the call is completed, the service instance is immediately released (non-garbage collection ). For binding that does not support sessions, such as basichttpbinding, the default behavior is precall.

[Servicecontract]
Public interface imyservice
{
[Operationcontract]
Void test ();
}

[Servicebehavior (instancecontextmode = instancecontextmode. percall)]
Public class myservie: imyservice, idisposable
{
Public myservie ()
{
Console. writeline ("constructor: {0}", this. gethashcode ());
}

[Operationbehavior]
Public void test ()
{
Console. writeline ("test: {0}", operationcontext. Current. sessionid );
}

Public void dispose ()
{
Console. writeline ("dispose ");
}
}

Public class wcftest
{
Public static void test ()
{
Appdomain. createdomain ("server"). docallback (delegate
{
Servicehost host = new servicehost (typeof (myservie), new uri ("http: // localhost: 8080/myservice "));
Host. addserviceendpoint (typeof (imyservice), new wshttpbinding (),"");
Host. open ();
});

//-----------------------

Imyservice channel = channelfactory <imyservice>. createchannel (New wshttpbinding (),
New endpointaddress ("http: // localhost: 8080/myservice "));

Using (channel as idisposable)
{
Channel. Test ();
Channel. Test ();
}
}
}

Output:
Constructor: 30136159
Test: urn: UUID: df549447-52ba-4c54-9432-31a7a533d9b4
Dispose
Constructor: 41153804
Test: urn: UUID: df549447-52ba-4c54-9432-31a7a533d9b4
Dispose

2. presession

Presession
The Mode needs to be bound to a binding object that supports session. Before the client proxy triggers the termination operation, WCF maintains the same service object for each client. Therefore
The presession mode can be used to maintain the call status. Because of this, presession
Be careful when using highly concurrent services to avoid excessive server load. The presession is enabled by default for binding objects that support sessions.
However, we recommend that you specify sessionmode. required and instancecontextmode. persession.

[Servicecontract (sessionmode = sessionmode. Required)]
Public interface imyservice
{
[Operationcontract]
Void test ();
}

[Servicebehavior (instancecontextmode = instancecontextmode. persession)]
Public class myservie: imyservice, idisposable
{
Public myservie ()
{
Console. writeline ("constructor: {0}", this. gethashcode ());
}

[Operationbehavior]
Public void test ()
{
Console. writeline ("test: {0}", operationcontext. Current. sessionid );
}

Public void dispose ()
{
Console. writeline ("dispose ");
}
}

Public class wcftest
{
Public static void test ()
{
Appdomain. createdomain ("server"). docallback (delegate
{
Servicehost host = new servicehost (typeof (myservie), new uri ("http: // localhost: 8080/myservice "));
Host. addserviceendpoint (typeof (imyservice), new wshttpbinding (),"");
Host. open ();
});

//-----------------------

Imyservice channel = channelfactory <imyservice>. createchannel (New wshttpbinding (),
New endpointaddress ("http: // localhost: 8080/myservice "));

Using (channel as idisposable)
{
Channel. Test ();
Channel. Test ();
}
}
}

Output:
Constructor: 30136159
Test: urn: UUID: 2f01b61d-40c6-4f1b-a4d6-4f4bc3e8847a
Test: urn: UUID: 2f01b61d-40c6-4f1b-a4d6-4f4bc3e8847a
Dispose

3. Single

The server creates a unique (Singleton) service object at startup. This object serves all clients and will not be released with the termination of the client.

[Servicecontract]
Public interface imyservice
{
[Operationcontract]
Void test ();
}

[Servicebehavior (instancecontextmode = instancecontextmode. Single)]
Public class myservie: imyservice, idisposable
{
Public myservie ()
{
Console. writeline ("constructor: {0}; {1}", datetime. Now, this. gethashcode ());
}

[Operationbehavior]
Public void test ()
{
Console. writeline ("test: {0}; {1}", datetime. Now, operationcontext. Current. sessionid );
}

Public void dispose ()
{
Console. writeline ("dispose: {0}", datetime. Now );
}
}

Public class wcftest
{
Public static void test ()
{
Appdomain. createdomain ("server"). docallback (delegate
{
Servicehost host = new servicehost (typeof (myservie), new uri ("http: // localhost: 8080/myservice "));
Host. addserviceendpoint (typeof (imyservice), new basichttpbinding (),"");
Host. open ();
});

//-----------------------

For (INT I = 0; I <2; I ++)
{
Imyservice channel = channelfactory <imyservice>. createchannel (New basichttpbinding (),
New endpointaddress ("http: // localhost: 8080/myservice "));

Using (channel as idisposable)
{
Channel. Test ();
Channel. Test ();
}
}
}
}

Output:

Constructor: 17:31:01; 63238509
Test: 17:31:03;
Test: 17:31:03;
Test: 17:31:03;
Test: 17:31:03;

There is another way to start single servicehost.

Appdomain. createdomain ("server"). docallback (delegate
{
Myservie service = new myservie ();

Servicehost host = new servicehost (service, new uri ("http: // localhost: 8080/myservice "));
Host. addserviceendpoint (typeof (imyservice), new basichttpbinding (),"");
Host. open ();
});

The biggest advantage of this method is that it allows us to use non-default structures. In addition, it is no different from the above example.

Note that by default, single controls the concurrency of service methods. That is to say, multiple clients need to wait in queue until the call from other clients is completed. Let's look at the example below.

[Servicecontract]
Public interface imyservice
{
[Operationcontract]
Void test ();
}

[Servicebehavior (instancecontextmode = instancecontextmode. Single)]
Public class myservie: imyservice, idisposable
{
Public myservie ()
{
Console. writeline ("constructor: {0}", this. gethashcode ());
}

[Operationbehavior]
Public void test ()
{
Console. writeline ("test: {0}", operationcontext. Current. sessionid );
Thread. Sleep (2000 );
Console. writeline ("test end: {0}", operationcontext. Current. sessionid );
}

Public void dispose ()
{
Console. writeline ("dispose ");
}
}

Public class wcftest
{
Public static void test ()
{
Appdomain. createdomain ("server"). docallback (delegate
{
Servicehost host = new servicehost (typeof (myservie), new uri ("http: // localhost: 8080/myservice "));
Host. addserviceendpoint (typeof (imyservice), new wshttpbinding (),"");
Host. open ();
});

//-----------------------

For (INT I = 0; I <2; I ++)
{
New thread (delegate ()
{
Imyservice channel = channelfactory <imyservice>. createchannel (New wshttpbinding (),
New endpointaddress ("http: // localhost: 8080/myservice "));

Using (channel as idisposable)
{
While (true)
{
Channel. Test ();
}
}
}). Start ();
}
}
}

Output:
Constructor: 63238509
Test: urn: UUID: d613cfce-d454-40c9-9f4d-62b30a93ff27
Test end: urn: UUID: d613cfce-d454-40c9-9f4d-62b30a93ff27
Test: urn: UUID: 313de31e-96b8-47c2-8cf3-7ae743236dfc
Test end: urn: UUID: 313de31e-96b8-47c2-8cf3-7ae743236dfc
Test: urn: UUID: d613cfce-d454-40c9-9f4d-62b30a93ff27
Test end: urn: UUID: d613cfce-d454-40c9-9f4d-62b30a93ff27
Test: urn: UUID: 313de31e-96b8-47c2-8cf3-7ae743236dfc
Test end: urn: UUID: 313de31e-96b8-47c2-8cf3-7ae743236dfc
Test: urn: UUID: d613cfce-d454-40c9-9f4d-62b30a93ff27
Test end: urn: UUID: d613cfce-d454-40c9-9f4d-62b30a93ff27
Test: urn: UUID: 313de31e-96b8-47c2-8cf3-7ae743236dfc
Test end: urn: UUID: 313de31e-96b8-47c2-8cf3-7ae743236dfc
...

We can change this behavior by modifying the concurrencymode, but we need to maintain multi-thread security on our own.

[Servicebehavior (instancecontextmode = instancecontextmode. Single, concurrencymode = concurrencymode. Multiple)]
Public class myservie: imyservice, idisposable
{
//...
}

Output:
Constructor: 10261382
Test: urn: UUID: 2bdac798-f774-40f2-b8f9-58de8d599d3c
Test: urn: UUID: 9a328d0e-8df7-4885-94ed-e04ceedc5a09
Test end: urn: UUID: 2bdac798-f774-40f2-b8f9-58de8d599d3c
Test: urn: UUID: 2bdac798-f774-40f2-b8f9-58de8d599d3c
Test end: urn: UUID: 9a328d0e-8df7-4885-94ed-e04ceedc5a09
Test: urn: UUID: 9a328d0e-8df7-4885-94ed-e04ceedc5a09
Test end: urn: UUID: 2bdac798-f774-40f2-b8f9-58de8d599d3c
Test: urn: UUID: 2bdac798-f774-40f2-b8f9-58de8d599d3c
Test end: urn: UUID: 9a328d0e-8df7-4885-94ed-e04ceedc5a09
Test: urn: UUID: 9a328d0e-8df7-4885-94ed-e04ceedc5a09
Test end: urn: UUID: 2bdac798-f774-40f2-b8f9-58de8d599d3c
Test: urn: UUID: 2bdac798-f774-40f2-b8f9-58de8d599d3c
Test end: urn: UUID: 9a328d0e-8df7-4885-94ed-e04ceedc5a09
Test: urn: UUID: 9a328d0e-8df7-4885-94ed-e04ceedc5a09
...

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.