Follow I will continue to do this until the sample is completed. Don't ask why, as I will tell you in the next article.
This article adds one of the custom behavior, parameterinspector, that is, the parameters passed by the client when calling the service method, we can filter before the service is actually called.
In the above example, we define our own behavior. Download Code: wcfbehaviorsolution0.zip
(1) Add the customservicebehaviors class library, which is the class library of the Service's custom behavior.
Add a reference to the customservicebehaviors class library in the wcfservicelibrary1 and serverconsoleapplication projects.
(2) create a custom behavior for the service
Create the myparameterinspector class in the customservicebehaviors project:
public class MyParameterInspector : Attribute, IOperationBehavior, IParameterInspector{ #region IOperationBehavior Members public void AddBindingParameters(OperationDescription operationDescription, System.ServiceModel.Channels.BindingParameterCollection bindingParameters) { } public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation) { } public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation) { dispatchOperation.ParameterInspectors.Add(this); } public void Validate(OperationDescription operationDescription) { } #endregion #region IParameterInspector Members public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState) { Console.WriteLine("after the call."); } public object BeforeCall(string operationName, object[] inputs) { Console.WriteLine("before the call."); inputs[0] = Convert.ToInt16(inputs[0]) + 1; return null; } #endregion}
This class is our custom behavior. We will enter some text before and after the method is called, and modify the first input parameter to increase it by 1.
On the Service side, we need to implement the applydispatchbehavior method of ioperationbehavior and add the current parameterinspector to the parameter detector of the behavior.
(3) Locally modify the main function of servicehost (that is, the serverconsoleapplication project:
static void Main(string[] args){ using (ServiceHost host = new ServiceHost(typeof(Service1))) { host.Description.Endpoints[0].Contract.Operations[0].Behaviors.Add(new MyParameterInspector()); host.Open(); Console.WriteLine("Service started."); Console.ReadKey(true); }}
In fact, there is one more sentence:
Host. description. endpoints [0]. Contract. Operations [0]. behaviors. Add (New myparameterinspector ());
That is to say, add the custom behavior myparameterinspector for the 1st operations (getdata method) of the 1st endpoints in the configuration file.
So far, we have added a custom behavior on the Service side.
Accordingly, update the service reference in the client.
// Base. endpoint. Contract. Operations [0]. behaviors. Add (New myparameterinspector ());
Run serverconsoleapplication and clientconsoleapplication successively. The results are as follows:
We can see that the client clearly enters 1982, but the output is 1983. This is because the beforecall function modifies this parameter in the Custom behavior of the service.
Download the sample code after completion: wcfbehaviorsolution1.zip