简体   繁体   中英

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.

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
{
    ...
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM