Search code examples
web-configwcf-web-api

WCF Web API Configuration File to IIS


I have implemented a restful service with WCF Web API and I want to publish it in IIS. During developing process I was using the service as Console Application and all configuration was made through API. Now I'm trying to publish the service as ASP.NET application and the only way I see is somehow to move all configuration into web config file.

Here the coded configuration:

var cfg = HttpHostConfiguration.Create()
                .AddMessageHandlers(typeof(AllowCrossDomainRequestHandler));

using (var host = new HttpConfigurableServiceHost(typeof(RESTfulService), cfg , new Uri("http://localhost:8081")))
            {
                var endpoint = ((HttpEndpoint)host.Description.Endpoints[0]);  //Assuming one endpoint
                endpoint.TransferMode = TransferMode.Streamed;
                endpoint.MaxReceivedMessageSize = 1024 * 1024 * 10;  // Allow files up to 10MB

                host.Open();
                Console.WriteLine("Host opened at {0} , press any key to end", host.Description.Endpoints[0].Address);
                Console.ReadKey();
            }

How should my web.config look to reflect this configuration? Or is there any other approach instead of using ASP.NET?

Any help is appreciated.


Solution

  • If you want to preserve your existing config, you can put all your config set up stuff into a method, and call it from global.asax Application_Start() method. All the Global.asax methods will get called in WCF the same as they do for ASP.NET.

    Or, you can wire your services to a custom ServiceHostFactory and ServiceHost that has all the configuration in it (this is the approach I am using in my current app).