HTTPS configuration for Web API on IIS 7.5

Topics: Web Api
Jun 3, 2011 at 1:33 PM

I'm really close to getting this to work.  I've got IIS setup for HTTPS binding with a properly installed certificate.  I'm currently getting this error:

Type 'Microsoft.ApplicationServer.Http.HttpResponseMessage`1[System.String]' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.

My resource class is this:

 

namespace BodyTalkAPI.Resources
{
  [ServiceContract]
  public class MainResource
  {
    [WebGet(UriTemplate = "{dataValue}")]
    HttpResponseMessage<string> GetData(int dataValue)
    {
      HttpResponseMessage<string> hrm = new HttpResponseMessage<string>(string.Format("you entered {0}", dataValue));
      return hrm;
    }
  }
}

 

 

My web.config is this:

  <system.serviceModel>
    <bindings>
      <webHttpBinding>
        <binding name="BodyTalkSecureBinding">
          <security mode="Transport">
            <transport clientCredentialType="None" />
          </security>
        </binding>
      </webHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="BodyTalkBehavior">
          <serviceMetadata httpGetEnabled="false" httpsGetEnabled="true" />
        </behavior>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service name="BodyTalkAPI.Resources.MainResource" behaviorConfiguration="BodyTalkBehavior">
        <endpoint address="" binding="webHttpBinding" bindingConfiguration="BodyTalkSecureBinding" contract="BodyTalkAPI.Resources.MainResource" />
      </service>
    </services>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

Coordinator
Jun 19, 2011 at 11:00 PM

That won't work as you are using the webHttpBinding. Web API uses a new binding called HttpBinding. You can't set it via config in the same way as it requires changes in our 4.0 bits. It is possible, but it's much more verbose as you have to specify the binding namespace / assembly explicitly.

The easiest way to do this now is via a custom service host. If you look at this post: http://wcf.codeplex.com/discussions/260860 you will see an HttpsServiceHostFactory that you can use.