title: WCF post method implementation
body: <p>I have a WCF service which is connected to an sql server database which is called from a mobile application. I have the following method which helps create bookings. </p>
  
  <pre><code>public void CreateBooking(Booking booking)
      {
          Booking newbooking = new Booking();
          sql = new SqlConnection("Data Source=comp;Initial Catalog=BookingDB;Integrated Security=True");
          sql.Open();
  
          string command =
              ("INSERT INTO Bookings( BookingName, BookingStart, BookingEnd, RoomID ) " +
               "VALUES ("
                  + "'" + booking.BookingName + "'" + ", "   
                  + "'" + booking.BookingStart  + "'" + ", " 
                  + "'" + booking.BookingEnd + "'" + ", "
                          + booking.RoomID + ")");
  
          SqlCommand cmd = new SqlCommand(command, sql);                
          cmd.ExecuteNonQuery();            
      }
  
      public void Close()
      {
          sql.Close();
      }
  </code></pre>
  
  <p>Markup:</p>
  
  <pre><code>&lt;%@ ServiceHost Language="C#" Debug="true" Service="BookingServices.BookingService" CodeBehind="BookingService.svc.cs" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %&gt;
  </code></pre>
  
  <p>Config File:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;configuration&gt;
    &lt;system.web&gt;
      &lt;!--&lt;authentication mode="None"/&gt;--&gt;
      &lt;compilation debug="true" targetFramework="4.0"&gt;
        &lt;assemblies&gt;
          &lt;add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/&gt;
        &lt;/assemblies&gt;
      &lt;/compilation&gt;
    &lt;/system.web&gt;
    &lt;system.serviceModel&gt;
  &lt;services&gt;
    &lt;service name="RoomBookingServices.RoomBookingService" behaviorConfiguration="RoomBookingServiceBehaviour"&gt;
      &lt;endpoint address="http://192.168.0.4:6321/RoomBookingServices/RoomBookingService.svc" binding="webHttpBinding" bindingConfiguration="webHttpBindingWithJsonP" contract="RoomBookingServices.IRoomBookingService" behaviorConfiguration="webHttpBehavior"&gt;
        &lt;identity&gt;
          &lt;servicePrincipalName value=""/&gt;
        &lt;/identity&gt;
      &lt;/endpoint&gt;
    &lt;/service&gt;
  &lt;/services&gt;
  &lt;behaviors&gt;
    &lt;serviceBehaviors&gt;
      &lt;behavior name="RoomBookingServiceBehaviour"&gt;
        &lt;!-- To avoid disclosing metadata information, 
        set the value below to false and remove the metadata endpoint above before deployment --&gt;
        &lt;serviceMetadata httpGetEnabled="true" /&gt;
        &lt;!-- To receive exception details in faults for debugging purposes, set the value below to true.  
        Set to false before deployment to avoid disclosing exception information --&gt;
            &lt;serviceDebug includeExceptionDetailInFaults="true" /&gt;
         &lt;/behavior&gt;
       &lt;/serviceBehaviors&gt;
        &lt;endpointBehaviors&gt;
          &lt;behavior name="webHttpBehavior"&gt;
           &lt;webHttp /&gt;
      &lt;/behavior&gt;
    &lt;/endpointBehaviors&gt;
  &lt;/behaviors&gt;
  &lt;bindings&gt;
    &lt;webHttpBinding&gt;
      &lt;binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true"&gt;&lt;/binding&gt;
    &lt;/webHttpBinding&gt;
  &lt;/bindings&gt;
  &lt;!--&lt;serviceHostingEnvironment multipleSiteBindingsEnabled="true" /&gt;--&gt;
  &lt;!--&lt;serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /&gt;--&gt;
    &lt;/system.serviceModel&gt;
    &lt;system.webServer&gt;
      &lt;modules runAllManagedModulesForAllRequests="true" /&gt;
    &lt;/system.webServer&gt;
    &lt;connectionStrings&gt;
      &lt;add name="RoomBookingDatabaseEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;data source=HAL;initial catalog=RoomBookingDatabase;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&amp;quot;" providerName="System.Data.EntityClient" /&gt;
      &lt;add name="RoomBookingDatabaseEntities1" connectionString="metadata=res://*/RoomBookingDB.csdl|res://*/RoomBookingDB.ssdl|res://*/RoomBookingDB.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;data source=HAL;initial catalog=RoomBookingDatabase;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&amp;quot;" providerName="System.Data.EntityClient" /&gt;
    &lt;/connectionStrings&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>Interface:</p>
  
  <pre><code>[OperationContract(Name="postmethod")]
      [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, UriTemplate = "postmethod/new")]
  
      void CreateBooking(Booking booking); 
  }
  </code></pre>
  
  <p>Booking class:</p>
  
  <pre><code>[DataContract]
  public class Booking
  {
      [DataMember]
      public int BookingID { get; set; }
  
      [DataMember]
      public string BookingName { get; set; }
  
      [DataMember]
      public DateTime BookingStart { get; set; }
  
      [DataMember]
      public DateTime BookingEnd { get; set; }
  
      [DataMember]
      public int RoomID { get; set; }
  
  }
  </code></pre>
  
  <p>However, whenever I call the method I am getting a 405 error. My question is, is the method above causing the error or is it something in the connection end of things? Thanks.</p>