【问题标题】:Consume a HTTP POST from a self hosted service使用自托管服务的 HTTP POST
【发布时间】:2012-11-12 19:37:27
【问题描述】:
我有一个需要监听 upload notifications coming from a BITS server 的自托管服务(它们是一个带有自定义标头的简单 HTTP POST 请求)。如果我不是自托管我的服务而是使用 IIS,我只会创建一个 ASPX 页面,我可以处理传入的请求,但我使用的是自托管 WCF,我无法切换到 IIS。
我曾考虑使用WebInvokeAttribute,但这似乎仅用于发送 JSON 或 XML 作为回复,我需要遵循协议规范。我也没有看到拉出自定义标题的方法。
接下来我查看的是HttpListener,它似乎可以满足我的需要,但是我没有看到是否有办法像普通 WCF 端点一样通过我的 app.config 文件对其进行配置。
我只是将地址添加到我的applicationSettings 部分还是有更好的方法来实现我想要做的事情?
【问题讨论】:
标签:
c#
.net
wcf
wcf-binding
self-hosting
【解决方案1】:
我最终只使用了 Properties 类并将 url 存储在那里。
//This is run on it's own thread
HttpListener listener = new HttpListener();
listener.Prefixes.Add(Properties.Settings.Default.BitsReplierAddress);
listener.Start();
while (_running)
{
// Note: The GetContext method blocks while waiting for a request.
// Could be done with BeginGetContext but I was having trouble
// cleanly shutting down
HttpListenerContext context = listener.GetContext();
HttpListenerRequest request = context.Request;
var requestUrl = request.Headers["BITS-Original-Request-URL"];
var requestDatafileName = request.Headers["BITS-Request-DataFile-Name"];
//(Snip...) Deal with the file that was uploaded
}
listener.Stop();