我有一个自托管服务,需要监听 upload notifications coming from a BITS server (它们是带有自定义 header 的简单 HTTP POST 请求)。如果我不是自托管我的服务并且使用的是 IIS,我只需创建一个 ASPX 页面并且可以处理传入请求,但我使用的是自托管 WCF,并且无法切换到 IIS。
我考虑使用 WebInvokeAttribute ,但是这似乎仅用于发送 JSON 或 XML 作为回复,我需要遵循协议(protocol)规范。另外,我没有找到提取自定义 header 的方法。
我查看的下一个内容是 HttpListener它似乎可以满足我的需要,但是我没有看到是否有办法像普通 WCF 端点一样通过我的 app.config 文件配置它。
我是否只需将地址添加到我的 applicationSettings
部分,还是有更好的方法来实现我想要做的事情?
最佳答案
我最终只使用了 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();
关于c# - 从自托管服务使用 HTTP POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13350310/