在 WCF 服务中获取请求者信息 [英] Getting Requester Info In WCF Service

查看:28
本文介绍了在 WCF 服务中获取请求者信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一项服务,该服务将支持 Android、黑莓、iOS 和 WP7 平台上的移动应用程序.这些应用程序将连接到我正在处理的各种基于 REST 的 WCF 服务.我想查看客户端应用程序传递给我的服务的信息.为了做到这一点,我在我的 WCF 服务中编写了当前操作:

I am working on a service that will support mobile applications on the Android, BlackBerry, iOS, and WP7 platforms. These applications will connect to various REST-based WCF services that I am working on. I would like to see what information a client application passes to my service. In an effort to do this, I've written the current operation in my WCF service:

[OperationContract]
[WebGet(UriTemplate = "/GetRequesterInfo")]
public string GetRequesterInfo()
{
  OperationContext context = OperationContext.Current;

  string message = "Session ID: " + context.SessionId;
  return message;
}

当我调用此代码时,我注意到 SessionId 是一个空字符串.此外,我想获得尽可能多的客户信息.例如,如果这是 ASP.NET,我可以使用 HttpRequest 对象并获取:

When I call this code, I notice that the SessionId is an empty string. In addition, I would like to get as much information about the client as possible. For instance, if this were ASP.NET, I could use the HttpRequest object and get:

  • HttpMethod
  • 是本地的
  • IsSecureConnection
  • 请求类型
  • Url.AbsoluteUri
  • Url.OriginalString
  • 用户代理
  • 用户主机地址
  • 用户主机名
  • 浏览器 ID
  • 浏览器.浏览器
  • Browser.CanInitiateVoiceCall
  • Browser.ClrVersion.Minor
  • Browser.Cookies
  • Browser.EcmaScriptVersion
  • Browser.GatewayVersion
  • Browser.InputType
  • Browser.MobileDeviceManufacturer
  • Browser.MobileDeviceModel

虽然有更多的属性,但我相信你明白了.这让我想到了几个问题:

While there are more properties, I'm sure you get the idea. This leads me to several questions:

  1. 如何获取与我的 WCF 服务的请求关联的请求线程?我认为这就是 OperationContext 的用途.但我愿意接受纠正.
  2. 如何获取与对 WCF 服务的请求关联的所有属性名称/值?
  3. 我是在要求有意义的事情还是我不在乎?似乎我应该能够获得有关请求客户端的一些信息.

推荐答案

您可以使用 System.ServiceModel.Channels.MessageProperties :

OperationContext context = OperationContext.Current;

if (context != null)
{
    MessageProperties messageProperties = context.IncomingMessageProperties;

如果请求确实来自浏览器,您可以获得一个 HttpRequest 对象,这就是您所要求的.

If the request actually came from a browser, you can get a HttpRequest object, which is what you asked for.

这是我的 MessageProperties 的屏幕截图,它应该为您提供有关如何访问这些属性的足够信息:

Here's a screen capture of my MessageProperties, it should give you enough information on how to access these properties:

这篇关于在 WCF 服务中获取请求者信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆