【问题标题】:Exposing WCF Services Via HTTP when not hosted in IIS未托管在 IIS 中时通过 HTTP 公开 WCF 服务
【发布时间】:2009-11-08 19:58:20
【问题描述】:

正如标题所说,我们需要在 .NET 应用程序和 Adob​​e AIR 应用程序之间设置 WCF 服务。我们不想在机器上运行 IIS,而是更愿意安装和运行托管在 Windows 服务中的 WCF 服务。

但是,我不确定这样做是否会让我们使用 HTTP 作为传输,这是否仅在 IIS 中有效?我能够设置使用 TCP 传输,但它与 AIR 的互操作不如使用 HTTP 好。

编辑:我一直在使用一些测试代码来查看这是否有效:

常规控制台应用程序:

    static void Main()
    {
        using (ServiceHost host = new ServiceHost(typeof(TestService)))
        {
            host.Open();
        }

        Console.WriteLine("Waiting...");
        Console.ReadLine();
    }

TestService 是一个简单的 HelloWorld 类型的服务。

在 App.Config 中:

<configuration>
  <system.serviceModel>
    <services>
      <service name="WCFExample2.TestService" behaviorConfiguration="WCFExample2.TestServiceBehavior">
        <host>
          <baseAddresses>
            <add baseAddress = "http://localhost:8731/Design_Time_Addresses/WCFExample2/Service1/" />
          </baseAddresses>
        </host>
        <!-- Service Endpoints -->
        <!-- Unless fully qualified, address is relative to base address supplied above -->
        <endpoint address ="" binding="wsHttpBinding" contract="WCFExample2.ITestService">
          <!-- 
              Upon deployment, the following identity element should be removed or replaced to reflect the 
              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
              automatically.
          -->
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <!-- Metadata Endpoints -->
        <!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. --> 
        <!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="WCFExample2.TestServiceBehavior">
          <!-- To avoid disclosing metadata information, 
          set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="True"/>
          <!-- To receive exception details in faults for debugging purposes, 
          set the value below to true.  Set to false before deployment 
          to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

【问题讨论】:

  • 好吧,使用此处的默认配置(没有任何 &lt;bindings&gt; 部分),您将使用 wsHttpBinding (SOAP 1.2),并且您将使用基于消息的安全性,期望 Windows 用户凭据进行身份验证。在互操作场景中,这可能不是一个很好的选择(取决于您的具体设置)

标签: wcf


【解决方案1】:

您应该可以轻松设置 Windows NT 服务,该服务托管您的 WCF 服务并公开 HTTP 端点 - 不需要 IIS(但 WCF 运行时将使用 http.sys 内核模式驱动程序)。

您尝试过但失败了吗?如果是这样 - 你能告诉我们你有什么,以及失败的原因和地点吗?

至少,您可能希望在您的服务端有类似这样的配置:

  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="Default">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <basicHttpBinding>
        <binding name="Default" 
                 sendTimeout="00:05:00" 
                 maxBufferSize="500000" 
                 maxReceivedMessageSize="500000" >
          <security mode="Message">
            <message clientCredentialType="UserName" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <services>
      <service name="Namespace.MyWCFService"
               behaviorConfiguration="Default">
        <host>
          <baseAddresses>
            <add baseAddress="http://MyServer:8282/MyWCFService/"/>
          </baseAddresses>
        </host>
        <endpoint
             address="basic"
             binding="basicHttpBinding" bindingConfiguration="Default"
             contract="Namespace.IMyWCFService" />
      </service>
    </services>
  </system.serviceModel>

当然,您可能需要在绑定上调整超时设置、缓冲区大小设置等、安全模式以及很可能需要的其他设置。

马克

【讨论】:

  • 它似乎工作,但浏览到端点给我一个“无法连接到这个网站”的错误,而不是拉取服务信息。
  • 嗯,“真正的”端点是不可见的,也不能通过浏览来访问它——毕竟它是一个 SOAP 消息。使用 Visual Studio“Common7/IDE”文件夹中的 WcfTestClient 应用程序 - 这非常适合测试。为了查看 WSDL(元数据),您需要我在更新回复中发布的 behaviorConfiguration。
【解决方案2】:

您可以跳过所有配置并使用 WebServiceHost 类(它将以相当标准的方式为您完成所有工作)。让它工作,然后考虑手动调整配置以满足您可能有的任何额外要求。 您需要的所有信息都在这里WebServiceHost on MSDN 这是开始使用自定义(即非 IIS)托管 http 服务的一种非常简单的方法。

迈克

【讨论】:

  • 但这有点不同 - 使用 WebServiceHost,您使用的是 REST 与 WCF 通常使用的 SOAP。这可能(也可能不是)是个好主意 - 取决于场景。
【解决方案3】:

除了配置文件设置之外,还要考虑一件事。 如果您在 Windows 服务中自托管,则为 http 端点

  • 使服务登录帐户成为机器上的本地管理员 或
  • 您必须使用 http.sys 为 http 命名空间注册服务帐户。 此步骤必须由本地管理员完成,但每台机器只能执行一次。您可以在 XP/win 2003 中使用 HttpSysCfg 工具来执行此操作。对于 vista/win 2008,请使用 netsh。

【讨论】:

    猜你喜欢
    • 2012-02-23
    • 1970-01-01
    • 2022-12-13
    • 1970-01-01
    • 2011-08-12
    • 2020-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多