简体   繁体   English

在WCF服务中获取请求者信息

[英]Getting Requester Info In WCF Service

I am working on a service that will support mobile applications on the Android, BlackBerry, iOS, and WP7 platforms. 我正在开发一项支持Android,BlackBerry,iOS和WP7平台上的移动应用程序的服务。 These applications will connect to various REST-based WCF services that I am working on. 这些应用程序将连接到我正在处理的各种基于REST的WCF服务。 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: 为了做到这一点,我在WCF服务中编写了当前操作:

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

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

When I call this code, I notice that the SessionId is an empty string. 当我调用此代码时,我注意到SessionId是一个空字符串。 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: 例如,如果这是ASP.NET,我可以使用HttpRequest对象并获取:

  • HttpMethod 列举HTTPMethod
  • IsLocal IsLocal
  • IsSecureConnection IsSecureConnection
  • RequestType 请求类型
  • Url.AbsoluteUri Url.AbsoluteUri
  • Url.OriginalString Url.OriginalString
  • UserAgent 用户代理
  • UserHostAddress UserHostAddress
  • UserHostName UserHostName
  • Browser.Id Browser.Id
  • Browser.Browser Browser.Browser
  • Browser.CanInitiateVoiceCall Browser.CanInitiateVoiceCall
  • Browser.ClrVersion.Minor Browser.ClrVersion.Minor
  • Browser.Cookies Browser.Cookies
  • Browser.EcmaScriptVersion Browser.EcmaScriptVersion
  • Browser.GatewayVersion Browser.GatewayVersion
  • Browser.InputType Browser.InputType
  • Browser.MobileDeviceManufacturer Browser.MobileDeviceManufacturer
  • Browser.MobileDeviceModel Browser.MobileDeviceModel

While there are more properties, I'm sure you get the idea. 虽然有更多的属性,但我相信你明白了。 This leads me to several questions: 这引出了几个问题:

  1. How do I get the request thread associated with the request to my WCF service? 如何将与请求关联的请求线程提供给我的WCF服务? I thought that's what OperationContext was for. 我认为这就是OperationContext的用途。 But I'm open to correction. 但我愿意接受纠正。
  2. How do I get all of the property name / values associated with a request to a WCF service? 如何获取与WCF服务请求关联的所有属性名称/值?
  3. Am I asking for something that makes sense or am I off my rocker? 我是在寻找有意义的东西还是我的摇杆? It seems like I should be able to get some info about the requesting client. 看起来我应该能够获得有关请求客户端的一些信息。

You can use System.ServiceModel.Channels.MessageProperties : 您可以使用System.ServiceModel.Channels.MessageProperties

OperationContext context = OperationContext.Current;

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

If the request actually came from a browser, you can get a HttpRequest object, which is what you asked for. 如果请求实际来自浏览器,您可以获得HttpRequest对象,这就是您要求的对象。

Here's a screen capture of my MessageProperties , it should give you enough information on how to access these properties: 这是我的MessageProperties的屏幕截图,它应该为您提供有关如何访问这些属性的足够信息:

替代文字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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