What's new in WCF 4.5 - Part 5
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
How to create HTTPS with default endpoint?
Step 1:Create the WCF service and browse the WSDL, you will find only basicHttpBinding as shown below
Fig 1 : default 'basicHttpBinding'
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 ServiceHostFactory that created the ServiceHost 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)
{
ServiceEndpoint se = new ServiceEndpoint(new ContractDescription("IService1"),
new BasicHttpBinding(), new EndpointAddress("basic"));
se.Behaviors.Add(new MyEndpointBehavior());
config.AddServiceEndpoint(se);
config.Description.Behaviors.Add(new ServiceMetadataBehavior {
HttpGetEnabled = true });
config.Description.Behaviors.Add(new ServiceDebugBehavior {
IncludeExceptionDetailInFaults = true });
}
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
}
Tips!
- Always create the service with Interface->Implementation format, mention the contract in Interface.
- Define the service in Class library and refer the class library in Host project. Don’t use service class in host project.
- Change the instance mode to per call as default.
- Always catch exception using try/catch block and throw exception using FaultException < T >.
- Logging and Include exception should be enable while compiling the project in debug mode. While in production deployment disable the logging and Include exception details.
|