检测到应用程序的传入连接

时间:2017-06-22 08:33:01

标签: c# rest mono tcplistener

我有一个使用RESTful服务的应用程序,当错误的请求到达时,应用程序完全爆炸。 (在Mono上,Windows只返回错误请求 - 这是我期望的行为)。

应用程序运行得非常好并且稳定,高效 - 直到我们为应用程序找到了新客户,每天发送数百个请求,并且每隔一段时间,我收到一个没有内容类型标头的请求 - 这是我能够重现此错误的唯一方法(发送"损坏的"请求)。

我能够通过CurrentDomain.UnhandledException事件在发生此致命异常时进行记录。所以我收到以下信息:

[09:48] [Trace]
{
  "ClassName": "System.ArgumentNullException",
  "Message": "Value cannot be null.",
  "Data": null,
  "InnerException": null,
  "HelpURL": null,
  "StackTraceString": "  at (wrapper managed-to-native) System.Object:__icall_wrapper_mono_delegate_end_invoke (object,intptr)\n  at (wrapper delegate-end-invoke) <Module>:end_invoke_bool__this___RequestContext&_IAsyncResult (System.Ser$
  "RemoteStackTraceString": null,
  "RemoteStackIndex": 0,
  "ExceptionMethod": null,
  "HResult": -2147467261,
  "Source": null,
  "ParamName": "contentType"
}

我尝试做的是记录应用程序的所有连接。我找到了一个TCP侦听器,但这不是我想要的。我正在寻找事件驱动的东西,每次连接进入时都会触发事件。

有点像这样:

ConnectionListener conListener = new ConnectionListener;
conListener.ConnectionIncoming += IncomingConnection;

func IncomingConnection(Connection conn):
     if conn.Headers["Content-Type"] is null then
         conn.Block(400, "Content-Type missing!")
         Error("Received faulty request!")
         Error(ToJson(conn))
     end
end

有什么类似于我正在寻找的东西吗?遗憾的是我无法简单地切换到Windows,其中一切都很糟糕,所以我需要能够拦截这些连接,并且只有当它们包含错误数据/缺少标题时才会拒绝连接或类似的东西。

0 个答案:

没有答案