HTTPS configuration and HttpsServiceHostFactory in Preview 5

Topics: Web Api
Sep 26, 2011 at 7:25 AM

Hi,

Can somebody suggest how to pass HTTPsServiceHostFactory in new Preview 5 bits?

I have already changed my HttpsServiceHostFactory settings as mentioned below.

//public class HttpsServiceHostFactory : HttpConfigurableServiceHostFactory
public class HttpsServiceHostFactory : HttpServiceHostFactory 

I used to send HTTPS configuration as mentioned below earlier -

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

var config = HttpHostConfiguration.Create();
config.AddMessageHandlers(typeof(ApiKeyVerificationChannel));
config.Configuration.OperationHandlerFactory.Formatters.Clear();

config.Configuration.OperationHandlerFactory.Formatters.Add(new JsonMediaTypeFormatter());
config.Configuration.OperationHandlerFactory.Formatters.Add(new FormUrlEncodedMediaTypeFormatter());

routes.MapServiceRoute<CustomerResource, HttpsServiceHostFactory>("customer", config);

Now I am getting error in MapServiceRoute saying "Using the generic method 'System.Web.Routing.RouteCollectionExtensions.MapServiceRoute<TService>(System.Web.Routing.RouteCollection, string, Microsoft.ApplicationServer.Http.HttpConfiguration, object, bool)' requires 1 type arguments"

Thanks.

Sep 26, 2011 at 10:01 PM

Hi Mahesh,

I don't believe you need to have an HttpsSericeHostFactory anymore. I am running an https only service with the following RegisterRoutes():

private void RegisterRoutes()
		{
var config = new HttpConfiguration();
config.MessageHandlers.Add(typeof(ApiKeyVerificationChannel));
config.EnableHelpPage = false;
config.EnableTestClient = true;
HttpServiceHostFactory factory = new HttpServiceHostFactory();
factory.Configuration = config;
RouteTable.Routes.SetDefaultHttpConfiguration(config);
RouteTable.Routes.MapServiceRoute<ArticlesHost>("api/articles");
RouteTable.Routes.MapServiceRoute<GeneralHost>("api");

		}

 

I think some of the changes from Preview4 to Preview 5 are that all HttpServiceHost(s) are now configurable and support https out of the box.

hth,

\ ^ / i l l

Coordinator
Sep 27, 2011 at 7:06 AM

Mahesh, Will is correct. We had the specific goal in Preview 5 of NOT requiring you to ever create your own host. Instead you can use the configuration object by either newing it up or even creating  custom configuration class. For example above you could have created a derived configuration class. We expose a Security property on our new class which you can use to configure security settings. Thus with the new config model you can have multiple custom config classes and use them across all hosts i.e. web host, self host, etc.

WIll, you don't need to create an HttpServiceHostFactory above. If you use MapServiceRoute it creates the factory for you. You just need to call SetDefautHttpConfiguration and pass in your config or simply pass the config when you call MapServiceRoute.

Thanks

Glenn

Sep 28, 2011 at 7:26 AM

Thanks Will and Glenn,

So to summarize, following piece of code will support HTTPS out of the box?

private void RegisterRoutes()
{
var config = new HttpConfiguration();
config.MessageHandlers.Add(typeof(ApiKeyVerificationChannel));
config.EnableHelpPage = false;
config.EnableTestClient = true;
RouteTable.Routes.MapServiceRoute<ArticlesHost>("api/articles",config);
RouteTable.Routes.MapServiceRoute<GeneralHost>("api",config);
}

Please confirm.Many Thanks.
Sep 29, 2011 at 5:40 AM

Hi Mahesh,

It does for me.

hth,

\ ^ / i l l