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 ();
}