Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to see who consumes my webservice

Is there any way to see who consumes my web service and how he uses it? Is there a logging system for that?

My consumer is not sure whether he is connected to my web service or not, and I cannot see that either.

like image 756
bileyazan Avatar asked Sep 27 '10 10:09

bileyazan


2 Answers

You can use a logging library such as log4net or the one in the enterprise library

If your service is a WCF service, adding an operation behavior allows you to perform some action every time an operation is invoked

internal class OperationLoggerBehavior : IOperationBehavior
{
    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(new OperationLogger());
    }

    public void Validate(OperationDescription operationDescription)
    {
    }
}

internal class OperationLoggerAttribute : Attribute, IContractBehavior
{
    public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {
    }

    public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
    {
        foreach (OperationDescription operationDescription in contractDescription.Operations)
        {
            if (!operationDescription.Behaviors.Contains(typeof(OperationLoggerBehavior)))
            {
                operationDescription.Behaviors.Add(new OperationLoggerBehavior());
            }
        }
    }

    public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
    {
    }
}


internal class OperationLogger : IParameterInspector
{
    /// <summary>
    /// Called before an operation is invoked.
    /// </summary>
    /// <param name="operationName"></param>
    /// <param name="inputs"></param>
    /// <returns></returns>
    public object BeforeCall(string operationName, object[] inputs)
    {
        // Write to log     
    }

    /// <summary>
    /// Called after an operation has been invoked.
    /// </summary>
    /// <param name="operationName"></param>
    /// <param name="outputs"></param>
    /// <param name="returnValue"></param>
    /// <param name="correlationState"></param>
    public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState)
    {
        // Write to log     
    }
}




// Service contract implementation    
[OperationLogger]
[ServiceBehaviorAttribute(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple)]
public partial class MyService : IMyService
{
    ...
}
like image 168
vc 74 Avatar answered Oct 15 '22 17:10

vc 74


You can use a tool like Wireshark to see exactly what messages your service is receiving.

It's good to add logging to to see exactly what your service is doing with the messages, something like Log4Net is very good.

Without both of these setup you won't be sure what is going on.

like image 37
Joe Ratzer Avatar answered Oct 15 '22 18:10

Joe Ratzer