未在IIS中托管时通过HTTP公开WCF服务

时间:2009-11-08 19:58:21

标签: wcf

与标题一样,我们需要在.NET应用程序和Adobe 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>

3 个答案:

答案 0 :(得分:3)

设置承载WCF服务并公开HTTP端点的Windows NT服务应该没有问题 - 不需要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>

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

马克

答案 1 :(得分:2)

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

麦克

答案 2 :(得分:1)

除配置文件设置外,还有一件事需要考虑。 如果你在Windows服务中自我托管,那么http端点

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