2

我有一个从 Iphone 应用程序调用的 WebService(我也在构建)

在我的网络服务中,它是在服务中自我托管的,并且一切正常,除了我想将安全令牌移动到请求的标头中,以便类对象保持整洁。(如果我不能在标题中得到它,我会求助于把它放在课堂上,但这有点难看)。

我查看了此http://msdn.microsoft.com/en-us/library/system.servicemodel.operationcontext.incomingmessageheaders.aspx#Y342中的代码,我似乎无法枚举标头值。

在 Fiddler 中查看,我可以看到标题正在通过

POST http://192.168.1.221:11001/StockControl/json/SubmitResults HTTP/1.1
Device-Token: bwI2YiAHR4q3Ba5JVj99Cw==
Content-Type: application/json
Content-Length: 1663
User-Agent: StockManage/1.0 CFNetwork/609 Darwin/12.1.0

我不确定我是否没有正确设置我的 SelfHosted 配置,或者我是否没有实现必要的接口。

WCF IClientMessageInspector 和传入的 SOAP 标头,但这是使用 SOAP 而我使用的是 JSON。

我的端点是使用以下设置的

WebHttpBinding jsonBind = new WebHttpBinding();
ServiceEndpoint jsonServer = host.AddServiceEndpoint(typeof(POSServer.StockControl.IStockService), jsonBind, "json");

jsonServer.Behaviors.Add(new WebHttpBehavior
{
    DefaultBodyStyle = System.ServiceModel.Web.WebMessageBodyStyle.Bare,
    HelpEnabled = true,
    DefaultOutgoingResponseFormat = WebMessageFormat.Json
});

最后在我的 Service 实现中的 SubmitResults 函数中

public bool SubmitResults(Business.StockResultData theData)
{
    DateTime uploadTime = DateTime.Now;
    int index = OperationContext.Current.IncomingMessageHeaders.FindHeader("Device-Token", "");
    this.WriteHeaders(OperationContext.Current.IncomingMessageHeaders);
    this.WriteHeaders(OperationContext.Current.RequestContext.RequestMessage.Headers);

但索引始终为 -1(未找到),并且 WriteHeaders 看不到标题。

4

2 回答 2

3

经过大量搜索,我相信我在这里找到了答案。(http://social.msdn.microsoft.com/Forums/pl-PL/wcf/thread/72ee44cc-58bb-45b2-aff7-49d9bbc8176e)

HttpRequestMessageProperty reqMsg = 
          OperationContext.Current.IncomingMessageProperties["httpRequest"] as 
          HttpRequestMessageProperty;
于 2012-09-25T01:24:08.740 回答
0

这对我有用......其中 apiKey 是标题名称

>  var headers =OperationContext.Current.IncomingMessageProperties["httpRequest"];
            var apiToken = ((HttpRequestMessageProperty)headers).Headers["apiKey"];
于 2016-09-10T23:30:57.930 回答