【问题标题】:Converting a HttpListener to run in IIS将 HttpListener 转换为在 IIS 中运行
【发布时间】:2011-11-21 04:49:23
【问题描述】:

我听说有一些方法可以获取使用 HttpListener 的 C# 应用程序并对其进行更改,以便它可以在 IIS 中运行,但我无法找到任何具体的参考资料。有人对这个话题有什么想法可以分享吗?

【问题讨论】:

  • 到目前为止你有没有尝试过?
  • 不-甚至不知道从哪里开始:-/
  • 这和 Azure 平台有什么关系?
  • Azure PaaS 应用在 IIS 中运行。我想对这个做同样的事情,但它使用的是 HttpListener。据我所知,有一些方法可以做到这一点。
  • 有人知道吗?谢谢

标签: c# iis azure httplistener


【解决方案1】:

我在这里找到了我想要的东西:http://support.microsoft.com/kb/308001

看起来只有很小的代码更改需要实现。希望这对其他人也有帮助。

2019 年 6 月 21 日编辑

@Vermin 问我是否能找到链接,我找不到,但我确实找到了我的一个非常老的代码库,其中包含一些令人尴尬的代码,至少可以提供一些起点/方向(希望对其他人也是如此)。如果我没有很好地说明其中一些项目背后的“原因”,请原谅我(这是不久前的事了,我还没有完全理解到我应该理解的深度)。

1) 服务合约

[ServiceContract] public interface <svcName> ... 
{
        [OperationContract]
        [WebGet(UriTemplate = "/*", BodyStyle = WebMessageBodyStyle.Bare)]
        Stream MyGetMethod();

        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "/*", BodyStyle = WebMessageBodyStyle.Bare)]
        Stream MyPostMethod(Stream bodyStream);

2) 我还必须提供一个内容映射器(我真的不记得为什么了)

    public class RawContentTypeMapper : WebContentTypeMapper
    {
        public override WebContentFormat GetMessageFormatForContentType(string contentType)
        {
            return WebContentFormat.Raw;
        }
    }

3) 然后我需要在 web.config 中做一些事情...

  <system.serviceModel>
    <services>
      <service name="<namespace>.<svcName>" behaviorConfiguration="<behaviorName>">
        <endpoint address="" binding="customBinding" behaviorConfiguration="<behaviorName>" contract="<namespace>.<svcName>" bindingConfiguration="<customBinding>"/>
      </service>
    </services>
    <bindings>
      <customBinding>
        <binding name="<customBinding>">
          <!-- Used for REST.  See http://www.codehosting.net/blog/BlogEngine/post/WebContentFormatRaw-in-your-WCF-config-file.aspx -->
          <!-- Provide the fully qualified name of the WebContentTypeMapper, set max message size to 500MB  -->
          <webMessageEncoding webContentTypeMapperType="<namespace>.RawContentTypeMapper, <namespace>, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
          <httpTransport manualAddressing="true" maxReceivedMessageSize="524288000" transferMode="Buffered" />
        </binding>
      </customBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="<behaviorName>">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="<behaviorName>">
          <webHttp helpEnabled="true"/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <diagnostics performanceCounters="All"/>
  </system.serviceModel>

我还在项目的 cmets 中看到了这个链接,这可能很有用:http://www.codehosting.net/blog/BlogEngine/post/WebContentFormatRaw-in-your-WCF-config-file

如果我无法真正解释上述所有内容的“原因”,您将不得不原谅我。其中一些很容易理解(映射 GET、POST 方法),其中一些我觉得我只是在尝试一些东西(web.config 的东西)。

在某些情况下,我记得我正在迁移的应用程序有代码来处理所有底层 HTTP 连接等,一旦我使用上述方法将它迁移到 IIS,并不是说它非常脆弱,我只是觉得好像我失去了很多控制权,却没有真正了解如何或为什么,并且需要做出改变确实让我觉得它很脆弱。这也是我构建 Watson Webserver(无耻插件)以快速构建 RESTful 服务器的原因之一:https://www.nuget.org/packages/Watson/2.0.5

【讨论】:

  • 您好 - 这是我现在正在调查的内容,但您发布的链接似乎不再有效!您能否提供一个可以为我指明正确方向的替代链接??
  • 嗨@Vermin 我似乎再也找不到它了。但是,我确实记得它需要使用 ServiceContract 中的 OperationContract 将我自己的方法映射回 WebGet 和 WebInvoke。如果你想直接给我发消息,我可以分享一些非常令人尴尬的旧源代码。
  • 编辑为@Vermin 添加更多上下文
【解决方案2】:

它是否编译为可以在命令行上启动的 exe?如果是这样:

Azure 项目有一个挂钩,可用于启动需要在后台运行的第 3 方应用程序。我建议您创建一个 Worker Role,添加一个端点以在 Azure 中打开必要的端口。将您现有的可执行文件作为项目添加到项目中(将其设置为复制本地),并使用服务定义文件中的后台任务启动它。

Steve Marx 有一篇关于如何做到这一点的精彩博文:http://blog.smarx.com/posts/using-other-web-servers-on-windows-azure

【讨论】:

  • 谢谢 - 是的,它被编译为 EXE,但我想在 IIS 中运行它(就像我可以使用 WCF 服务等一样)。据我所知,有一种方法可以使用通用处理程序来做到这一点,但我似乎找不到一个例子。
  • 您说希望它在 IIS 中运行,但有什么特别的原因吗?
  • 推理无关紧要。我不是在寻找替代解决方案。感谢您在 IIS 中运行 HttpListener 应用程序所需的更改方面提供的任何帮助。
猜你喜欢
  • 2014-12-14
  • 1970-01-01
  • 2019-12-12
  • 2012-01-30
  • 2014-10-15
  • 2022-01-22
  • 1970-01-01
  • 1970-01-01
  • 2022-08-17
相关资源
最近更新 更多