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

编程入门 行业动态 更新时间:2024-10-25 22:24:53
本文介绍了在 WCF 服务中获取请求者信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在开发一项服务,该服务将支持 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.AbsoluteUriUrl.OriginalString用户代理用户主机地址用户主机名浏览器 ID浏览器.浏览器Browser.CanInitiateVoiceCallBrowser.ClrVersion.MinorBrowser.CookiesBrowser.EcmaScriptVersionBrowser.GatewayVersionBrowser.InputTypeBrowser.MobileDeviceManufacturerBrowser.MobileDeviceModel

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

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

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

推荐答案

您可以使用 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-26 19:57:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1141186.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:信息   请求者   WCF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!