How to use custom headers in WCF (typical scenario: Login and use of business systems)

Source: Internet
Author: User
Question:

InWCFTypical scenarios are as follows:

[Servicecontract]

Public   Interface Ilogin {

[Operationcontract]

BoolSignin (StringUsername,StringPassword );

}

[Servicecontract]

Public   Interface Ibiztest {

[Operationcontract]

StringGetwelcomeinfo ();

}

Never start

WCF the built-in instancecontextmode to find a solution, because WCF persession the call is only for each service class, unless you are abnormal, there is only one class on the server to implement all the interfaces.

Change your mind. Can you use something similar?. Net remotingInCallcontextWhat about it? But I checked it.WCFThe Manual does not seem to have such a thing. How can this problem be solved? That isCustom header.

Before the solution is proposed, the server must know thatHeaderMethod:

traverse first operationcontext . current. incomingmessageheaders Find the header name sent by the client , then use operationcontext . current. incomingmessageheaders. getheader T (I) you can get the value.

The following question is how the client sends the custom header.
Policy1: In each clientProxyAdd the followingCode

Using (Operationcontextscope Scope =   New Operationcontextscope (innerchannel )) {

Messageheader MH = Messageheader. createheader ( " Headername " , String . Empty, " Headervalue " );

Operationcontext. Current. outgoingmessageheaders. Add (MH );

// ...

}

 

But every client needs to be added, which is too troublesome.

2.CustomizeCallcontextattribute,The Code is as follows:

1.First defineIclientmessageinspectorInterface implementation class

Public   Class Contextheader: iclientmessageinspector {

Public   Void Afterreceivereply ( Ref System. servicemodel. channels. Message reply, Object Correlationstate) {

//

}

Public   Object Beforesendrequest ( Ref System. servicemodel. channels. Message request, iclientchannel channel) {

Messageheader clientheader=Messageheader. createheader ("Headername",String. Empty,"Headervalue");

Request. headers. Add (clientheader );

Return Null;

}

}

OK,Then we can implementCallcontextattributeNow

Public   Class Callcontextattribute: attribute, iendpointbehavior, ioperationbehavior {

Iendpointbehavior members # Region Iendpointbehavior members

Public   Void Addbindingparameters (serviceendpoint endpoint, bindingparametercollection bindingparameters) {

}

Public   Void Applyclientbehavior (serviceendpoint endpoint, clientruntime) {

Clientruntime. messageinspectors. Add (NewContextheader ());

}

Public   Void Applydispatchbehavior (serviceendpoint endpoint, endpointdispatcher) {

}

Public   Void Validate (serviceendpoint endpoint) {

}

# Endregion

Ioperationbehavior members # Region Ioperationbehavior members

Public   Void Addbindingparameters (operationdescription, bindingparametercollection bindingparameters) {

}

Public   Void Applyclientbehavior (operationdescription, clientoperation) {

Clientoperation. Parent. messageinspectors. Add (NewContextheader ());

}

Public   Void Applydispatchbehavior (operationdescription, dispatchoperation) {

}

Public   Void Validate (operationdescription) {

}

# Endregion

}

Finished, finally at usContractJoinCallcontextattributeThe client does not need to add any code.

[Servicecontract]

[Callcontext]

Public   Interface Ibiztest {

[Operationcontract]

[Callcontext]

StringGetwelcomeinfo ();

}

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.