如何查看谁使用我的网络服务

时间:2010-09-27 10:38:28

标签: c# .net web-services

有没有办法查看谁使用我的网络服务以及他如何使用它?那有记录系统吗?

我的消费者不确定他是否已连接到我的网络服务,我也看不到。

2 个答案:

答案 0 :(得分:5)

您可以使用log4net等记录库或enterprise library

中的记录库

如果您的服务是WCF服务,则添加操作行为允许您在每次调用操作时执行某些操作

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

答案 1 :(得分:1)

您可以使用Wireshark之类的工具查看您的服务正在接收哪些邮件。

最好添加日志记录以确切了解您的服务对消息的处理方式,Log4Net之类的内容非常好。

如果没有这两种设置,您将无法确定发生了什么。