【问题标题】:C# hide WCF hide endpoint not found responseC# 隐藏 WCF 隐藏端点未找到响应
【发布时间】:2017-02-04 10:08:41
【问题描述】:

我正在尽可能地隐藏我的 WCF。如果有人将 URL 输入到我的 WCF,则会显示响应“找不到端点”。如果他们尝试获取请求,则会显示不允许的方法,因为我的服务只接受发布请求。

我的 web.config 中已经包含以下内容

    <behaviors>
      <endpointBehaviors>
        <behavior name="webBehavior">
          <webHttp helpEnabled="false" />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="Default">
          <serviceDebug includeExceptionDetailInFaults="False" />
          <serviceMetadata httpGetEnabled="false" httpsGetEnabled="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>

除非他们有准确的 url 和准确的 post 参数,否则我不希望显示任何响应。我不确定是否需要更改“自定义错误部分”。我的理解是自定义错误仅适用于引发异常时。 Endpoint not found 和 method not allowed 也不例外。

任何帮助或建议将不胜感激。

【问题讨论】:

    标签: c# wcf hide


    【解决方案1】:

    我建议将 IServiceBehavior 或 IEnpointBehavior 与 IDispatchMessageInspector 或 IDispatchOperationSelector 结合使用。这样,当收到请求时,您可以编写代码来做出自定义响应。

    我没有完全符合您需求的示例,但您可以查看下面的文章,其中我修改了传出消息上的 XML 命名空间标签。我的想法是,如果请求不符合您的标准,您可以简单地返回 null 或 404。还包括一些有关该主题的 MSDN 文章。

    How can I create custom XML namespace attributes when consuming a legacy SOAP service?

    https://msdn.microsoft.com/en-us/library/system.servicemodel.description.iservicebehavior(v=vs.110).aspx

    https://msdn.microsoft.com/en-us/library/system.servicemodel.description.iendpointbehavior(v=vs.110).aspx

    https://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.idispatchmessageinspector(v=vs.100).aspx

    https://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.idispatchoperationselector(v=vs.100).aspx

    【讨论】:

      【解决方案2】:

      阅读这篇文章https://blogs.msdn.microsoft.com/carlosfigueira/2011/05/09/wcf-extensibility-operation-selectors/

      如果要抛出 404,可以从 OperationSelector 中抛出如下:

      ...
      throw new System.ServiceModel.Web.WebFaultException(System.Net.HttpStatusCode.NotFound);
      ...
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多