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.