开发者

Exposing WCF Services Via HTTP when not hosted in IIS

Like the title says, we need to set up WCF services between a .NET app, and a Adobe AIR app. We don't want to run IIS on the machine, and would much prefer to install and run the WCF services hosted within a windows service.

However, I am uncertain of doing that will let us use HTTP as the transport, of does that only work within IIS? I was able to set things up to use the TCP transport, but that doesn't interop with AIR nearly as nice as using HTTP.

EDIT: Some test code I've been using to see if this works:

Regular console app:

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

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

TestService is a simple HelloWorld type service.

In the 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 o开发者_运维知识库r 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>


You should have no trouble setting up a Windows NT Service which hosts your WCF service and exposes HTTP endpoints - no need for IIS (but the WCF runtime will use the http.sys kernel mode driver).

Have you tried and failed? If so - can you show us what you had, and how and where it failed?

As a bare minimum, you'd probably want to have something like this config on your service side:

  <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>

Of course, you might need to tweak things like the timeout settings, buffer size settings etc. on your binding, the security mode, and quite possibly other settings as you need them to be.

Marc


You could skip all the config and use the WebServiceHost class (which will do it all for you in a fairly standard way). Get that working then look into tailoring the config manually to meet any extra requirements you may have. All the info you need is here WebServiceHost on MSDN it's a very straightforward way to get started on a custom (i.e. non IIS) hosted http service.

Mike


Apart from the config file settings one more thing to consider. If you selfhost in a windows service, a http endpoint then

  • Make the service login account a local admin on the machine or
  • You have to register the service account for the http namespace with http.sys. This step has to be done by a local admin but only once in each machine. You can use the HttpSysCfg tool to do this in XP/win 2003. For vista/win 2008 use netsh.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜