【问题标题】:WCF Web Service IP LoggingWCF Web 服务 IP 日志记录
【发布时间】:2012-07-06 15:27:44
【问题描述】:

我正在尝试在我的 Web 服务中创建一个方法,该方法将记录服务中每个方法的每个请求的 IP。我尝试使用 HttpContext,但无论我做什么,它都会返回一个空引用异常。我希望能够从请求中获取 IP 并将其记录到 SQL 数据库中。这是我尝试记录的一种方法的示例。

public GetPL GPL(string code)
    {
        var db = new TDCDataContext();

        var pq = db.PROC_SELECT_GPL(code).ToList();

        //a bunch of nonsense

        //logging
        var ip = HttpContext.Current.Request.UserHostAddress;
        var method = GetMethod();
        db.PROC_INSERT_Log(ip, code, method, true, null, null);

        return stuff;
    }

我是不是走错方向了?

【问题讨论】:

  • 可能的重复参见:stackoverflow.com/questions/93162/…
  • 根据您使用的协议,您的 WCF 服务将只能在以下情况下访问 HttpContext通过 GET 或 POST。正常的 WCF 调用不会使用 HttpContext

标签: c# wcf logging ip


【解决方案1】:
                try
                {
                    var context = OperationContext.Current;
                    if (context != null)
                    {
                        var prop = context.IncomingMessageProperties;

                        if (prop != null && 
                            !string.IsNullOrEmpty(RemoteEndpointMessageProperty.Name) && 
                            prop.ContainsKey(RemoteEndpointMessageProperty.Name))
                        {
                            endpoint = prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
                        }
                    }                    
                } 
                catch (Exception e)
                {
                    //log
                }

//endpoint.Address - ip address

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-13
    • 2012-05-14
    • 2015-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-18
    • 1970-01-01
    相关资源
    最近更新 更多