Advertisement
turtlespin

Configuring WCF Web API

Jul 25th, 2011
665
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.28 KB | None | 0 0
  1. You cant specify the configuration via web.config unfortunately so you have to do it in code..
  2.  
  3. You can pass in a custom service host factory into the MapServiceRoute call which then allows you to get access to the ServiceHost instance and all the associated runtime objects so you can configure them via code.
  4.  
  5. e.g. Here is a custom factory which just sets the MaxReceivedMessageSize property:
  6.  
  7. public class CustomServiceHostFactory : HttpConfigurableServiceHostFactory
  8. {
  9.   public override ServiceHostBase CreateServiceHost(string constructorString, Uri[] baseAddresses)
  10.   {
  11.     var host = base.CreateServiceHost(constructorString, baseAddresses);
  12.      
  13.     foreach (HttpEndpoint endpoint in host.Description.Endpoints)
  14.     {
  15.       endpoint.MaxReceivedMessageSize = 1024 * 1024;
  16.     }
  17.  
  18.     return host;
  19.   }
  20. }
  21.  
  22. You will need to adjust your MapServiceRoute call.. e.g. lets say you were doing this before:
  23.  
  24. routes.MapServiceRoute<IFooContract>("api/foo");
  25.  
  26. You would now need to specify it as:
  27.  
  28. routes.MapServiceRoute<IFooContract, CustomServiceHostFactory>("api/foo", null, null);
  29.  
  30. (The two null arguments at the end are needed, you will run into errors if you dont supply them explicitly for some reason despite there being an overload which looks like it would default them..)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement