title: WCF service runs in Debug mode but not in Release
body: <p>I've come up against a weird problem: The WCF web service I'm building runs in debug mode but not in release.</p>
  
  <p>I've got a simple Hello web method in the web service that just returns a string and does nothing else (no logging, exception handling, nothing).  I run the WCFTestClient from within Visual Studio by selecting the .svc file and clicking F5 to start debugging.  When the WCFTestClient is open I double-click on the Hello web method and Invoke it.  In Debug mode it runs fine.  In Release mode almost instantly I get the following error:</p>
  
  <pre><code>Object reference not set to an instance of an object.
  
  Server stack trace: 
     at System.ServiceModel.Channels.ServiceChannel.ThrowIfFaultUnderstood(Message reply, MessageFault fault, String action, MessageVersion version, FaultConverter faultConverter)
     at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc&amp; rpc)
     at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
     at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
     at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
  
  Exception rethrown at [0]: 
     at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
     at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&amp; msgData, Int32 type)
     at IVehicleRisk.Hello()
     at VehicleRiskClient.Hello()
  </code></pre>
  
  <p>(the web service is VehicleRisk.svc and the service contract is IVehicleRisk).</p>
  
  <p>Adding breakpoints to the application I can see when I invoke the Hello web method the Application_BeginRequest method in Global.asax.cs is being called (it's an empty method).  When the exception is thrown no breakpoints in VehicleRisk.svc.cs are being hit, in either the constructor or the Hello method (when the web method works, the breakpoints are being hit).</p>
  
  <p>I looked at the project build properties in Visual Studio (right click on the project in Solution Explorer, select Properties, open the Build tab in the Properties window).  The only difference I can see between Debug and Release modes are:</p>
  
  <ol>
  <li><p>Define DEBUG constant:  Set on in Debug mode, off in Release;</p></li>
  <li><p>Optimize code:  Set off in Debug mode, on in Release;</p></li>
  <li><p>Advanced > Output Debug Info:  Set to full in Debug mode, pdb-only in Release.</p></li>
  </ol>
  
  <p>Experimenting with changing the Build properties in Release mode, I found that I can get the web method to work only when Optimize code is set off and Advanced > Output Debug Info is set to full.  Setting the DEBUG constant on or off makes no difference.</p>
  
  <p>Does anyone have any idea why a simple web method would fail when code optimization is turned on, or when debug info is set to pcb-only?</p>