Configuring an ASP.NET hosted WCF 3.5 WebServiceHost programmatically? (i.e. No WebConfig)

835 views Asked by At

I'm having a bit of trouble getting this all running.

This is my web.config:

  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <bindings>
      <webHttpBinding>
        <binding name="requestSizeMax_1MB" maxReceivedMessageSize="1048576" transferMode="Streamed"></binding>
        <binding name="requestSizeMax_10MB" maxReceivedMessageSize="10485760" transferMode="Streamed"></binding>
        <binding name="requestSizeMax_100MB" maxReceivedMessageSize="104857600" transferMode="Streamed"></binding>
        <binding name="requestSizeMax_1000MB" maxReceivedMessageSize="1048576000" transferMode="Streamed"></binding>
      </webHttpBinding>
    </bindings>
    <behaviors>
      <endpointBehaviors>
        <behavior name="http">
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <services>
      <service name="REST.IO.FileHandler">
        <endpoint binding="webHttpBinding" bindingConfiguration="requestSizeMax_1000MB" behaviorConfiguration="http" contract="REST.IO.IFileHandler"/>
      </service>
    </services>

This is my attempt at a programmatic version (in Global.asax):

protected void Application_Start(object sender, EventArgs e) {
  //Dynamically Add Service EndPoint
    Uri baseAddress = new Uri("http://localhost:8000/");
    WebServiceHost serviceHost = new WebServiceHost(typeof(FileHandler), baseAddress);
    WebHttpBinding restBinding = new WebHttpBinding();
    restBinding.MaxReceivedMessageSize = 1048576000;
    restBinding.TransferMode = TransferMode.Streamed;
    ServiceEndpoint restService = serviceHost.AddServiceEndpoint(typeof(IFileHandler), restBinding, "Services/IO/FileHandler");
    restService.Behaviors.Add(new WebHttpBehavior());
    serviceHost.Open();
}

Now this will work, but only if the WebServiceHost is opened on a separate port from the website. Which is different than if it's setup via the web.config. How can I get a programmatic setup that mirrors the functionality of web.config. Trying to set it to the same port as the website will generate a "Process cannot access a file because it is being used by another process" error.

I heard you can override ServiceHostFactory, but all my attempts to do so have failed. I can't seem to get WebServiceHost working with it. ServiceHost seems to work, but I'm using a REST service which was previously developed, and I need to get it hosted via our website programmatically, mainly because it's the first of a few services planned to be hosted, and I need to make this as dynamic as possible for the multiple rollouts we will need to perform.

1

There are 1 answers

1
Wiktor Zychla On BEST ANSWER

I wonder what issues you possibly had. Exposing WCF via imperative configuration is merely setting the ServiceHostFactory, pointing the *.svc file to the factory and creating the binding in the factory.

This should let you started, I've blogged on how to configure SSL for basic HTTP binding WCF but the general idea is the same, you only change bindings, endpoints and contracts.

http://www.wiktorzychla.com/2010/05/how-to-programmatically-configure-ssl.html