30 March 2014

Simple to expose HTTPS with IIS

WCF 4.0

WCF 4.0 has one of new feature called default endpoint, when ever new service is created default endpoint created with ‘basicHttpBinding’. But if you need to expose the service in ‘HTTPS’ binding then you need to Explicitly create the endpoint and provide all information.

WCF 4.5

In WCF 4.5, default endpoint can be created with HTTPS binding, by simply specifying the SSL and enabling ‘https’ in IIS setting

To Create a End Point:

Step 1:Create the WCF service and browse the WSDL, you will find only basicHttpBinding as shown below


Step 2: Host the WCF service in IIS and set the ‘https’ binding for the website
Step 3: Browse the wsdl file from IIS, you can see the https enabled by default. It is so simple WCF 4.5


Configuring WCF Services in Code

In general, WCF provides option to configure the service using config file or through code.
In older version of WCF (4.0 or older), if you want to configure the web hosted service, then you need to create a Service Host Factory that created the Service Host and performed any needed configuration.

But in case of WCF 4.5 it is very simple, you need to define a public static method called Configure with the following signature in your service implementation class. This method will be called before service host is opened.

Note: If static Configuration () method is specified, setting mention in app.config or web.config file will be ignored.

Code sample:
public class Service : IService
{
    public static void Configure(ServiceConfiguration config)
    {
       Service Endpoint se = new Service Endpoint(new Contract Description("IService1"),
       new BasicHttpBinding(), new Endpoint Address("basic"));
       se.Behaviors.Add (new MyEndpointBehavior ());
       config.AddServiceEndpoint (se); 
       config.Description.Behaviors.Add(new ServiceMetadataBehavior { 
                                                HttpGetEnabled = true});
        config.Description.Behaviors.Add (new ServiceDebug Behavior {
                                     IncludeExceptionDetailInFaults = true});
    }
Public string Get Data (int value)
{
                        Return string.Format ("You entered: {0}", value);
}
}

No comments:

Post a Comment