Thursday, September 15, 2011

new keyword in C#

In C# the “new” keyword can be used in 3 different ways, as operator, modifier and constraint. We all are familiar with new operator usage, i.e. for creating object and invoking constructor.  But other two usages are not much popular in normal coding.

new” Operator
It is used for instantiating objects and invoking constructors. For example,
BaseClass baseClass = new BaseClass();

new” Modifier
The new keyword can be used for hiding the inherited member of base class. If the base class and derived class having the member with same name and type, and we need to hide the base class member in child class, then we can use the “new” keyword to hide the base class member.
public class BaseClass
{
    public int GetValue() { return 10; }
}

public class DerrivedClass : BaseClass
{
    public new int GetValue() { return 20; }
}

Here the BaseClass and DerrivedClass have the same method and we are hiding the BaseClass member in DerrivedClass using “new”.

new” Constraint
Here new keyword is using as part of generic class definition.  If the generic class needs to create any instances of the specified type, then we need to specify the new constraint. As an example,
public class MyFormatter
    where T : IFormattable
    where F : IMyFormatProvider, new()
{
    public T Value { get; set; }

    public MyFormatter(T Value)
    {
        this.Value = Value;
    }

    public string Format()
    {
        F formatter = new F();
        return Value.ToString(formatter.GetFormatter(), null);
    }
}

In this scenario we need to make sure that the specified class having Parameter Less constructor. In this example, we are instantiating new class instance of type F, so we need to specify “new()” over the generic class definition. If we missed to specify “new”, then we will get the build error like” Cannot create an instance of the variable type 'F' because it does not have the new() constraint”.

Thursday, January 13, 2011

Windows Authentication in WCF

The WCF services with BasicHttpBinding are usually hosted with Anonymous access. If we are disabling the Anonymous Authentication from the IIS, the web service will throw the following error message

Security settings for this service require 'Anonymous' Authentication but it is not enabled for the IIS application that hosts this service.

For changing the Anonymous to Windows authentication we need to make some service configuration level changes.

First we need to make sure that the Authentication mode of service is windows.

<authentication mode="Windows" />

After that, the service binding settings need to be changed for implementing the security. The BasicHttpBinding uses HTTP as the transport for sending the messages and does not provide any security. This binding is designed for interoperability with web service provides that do not implement security. However, you can switch on security by setting the Mode property to Transport, to enable transport security.

Inside the <system.serviceModel> configuration tag in web.config,

<bindings>
  <basicHttpBinding>
    <binding name="SecurityByTransport">
      <security mode="Transport">
        <transport clientCredentialType="Windows" />
       security>
     binding>
  basicHttpBinding>
bindings>

Here you can configure the service settings with above binding configuration by assigning bindingConfiguration as binding name provided.

<services>
  <service behaviorConfiguration="WCFWindowsBasicHttpBinding.Service1Behavior"
      name="WCFWindowsBasicHttpBinding.Service1">
    <endpoint address="" binding="basicHttpBinding"
        bindingConfiguration="SecurityByTransport"
        name="BasicHttpEndpoint" contract="WCFWindowsBasicHttpBinding.IService1">
      <identity>
        <dns value="localhost" />
      identity>
    endpoint>
  service>
services>

This is the configurations for hosting the BasicHttpBinding service with Windows authentication.

In the service client also you need to implement the same changes. In the binding configuration you need to put the same Security Mode and clientCredentialType.