Thursday 2 August 2012

WCF- Other questions


What is Proxy and how to generate proxy for WCF Services?

 The proxy is a CLR class that exposes a single CLR interface representing the service contract.

The proxy provides the same operations as service's contract, but also has additional methods for managing the proxy life cycle and the connection to the service.

The proxy completely encapsulates every aspect of the service

  •  its location, 
  • its implementation technology and 
  • runtime platform, 
  • communication transport. 


The proxy can be generated using Visual Studio by right clicking Reference and clicking on Add Service Reference. This brings up the Add Service Reference dialog box, where you need to supply the base address of the service (or a base address and a MEX URI) and the namespace to contain proxy.

Proxy can also be generated by using SvcUtil.exe command-line utility.
We need to provide SvcUtil with the HTTP-GET address or the metadata exchange endpoint address and, optionally, with a proxy filename.

The default proxy filename is output.cs but you can also use the /out switch to indicate a different name.

SvcUtil http://localhost/MyService/MyService.svc /out:Proxy.cs 


When we are hosting in IIS and selecting a port other than port 80 (such as port 88), we must provide that port number as part of the base address:


SvcUtil http://localhost:88/MyService/MyService.svc /out:Proxy.cs



What are different elements of WCF Srevices Client configuration file?

WCF Services client configuration file contains endpoint, address, binding and contract.

A sample client config file looks like

<system.serviceModel>
  <client>
      <endpoint name = "MyEndpoint"
         address  = "http://localhost:8000/MyService/"
         binding  = "wsHttpBinding"
         contract = "IMyContract"
      />
   </client>
</system.serviceModel>


How to set the timeout property for the WCF Service client call?

The timeout property can be set for the WCF Service client call using binding tag.
If no timeout has been specified, the default is considered as 1 minute

<client>
    <endpoint
       ...

      binding = "wsHttpBinding"

      bindingConfiguration = "LongTimeout" 

      ...
    />

</client>

<bindings>

   <wsHttpBinding>

      <binding name = "LongTimeout" sendTimeout = "00:04:00"/> 

   </wsHttpBinding>

</bindings>


What are various ways of hosting WCF Services?

There are three major ways of hosting a WCF services

  • Self-hosting the service in his own application domain. The service comes in to existence when you create the object of Service Host class and the service closes when you call the Close of the Service Host class. 
  • Host in application domain or process provided by IIS Server. 
  • Host in Application domain and process provided by WAS (Windows Activation Service) Server.

What is .svc file in WCF?

.svc file is a text file. This file is similar to.asmx file in web services.
This file contains the details required for WCF service to run it successfully.
This file contains following details :

  • Language (C# / VB) 
  • Name of the service 
  • Where the service code resides 

Example of .svc file

<%@ ServiceHost Language="C#/VB" Debug="true/false" CodeBehind="Service code files path" Service="ServiceName" 


We can also write our service code inside but this is not the best practice.

Which protocol is used for platform-independent communication?

SOAP (Simple Object Access Protocol), which is directly supported from WCF (Windows Communication Foundation).

What are the advantages of hosting WCF service in WAS?

WAS (Windows Activation Service) is a component of IIS 7.0. Following are few advantages :
  • We are not only limited to HTTP protocol. We can also use supported protocols like TCP, named pipes and MSMQ 
  • No need to completely install IIS. We can only install WAS component and keep away the WebServer.

What is service host factory in WCF?

  • Service host factory is the mechanism by which we can create the instances of service host dynamically as the request comes in. 
  • This is useful when we need to implement the event handlers for opening and closing the service. 
  • WCF provides ServiceFactory class for this purpose.


What are the advantages of Hosting WCF in IIS

  • Provides process activation and recycling ability thereby increasing reliability 
  • It is a simplified way of deployment and development of hosted services. 
  • Hosting WCF services in IIS can take advantage of scalability and density features of ASP.NET


What does a Windows Communication Foundation, or WCF, service application use to present exception information to clients by default?

  • WCF service cannot return a .NET exception to the client. 
  • The WCF service and the client communicate by passing SOAP messages. 
  • If an exception occurs, the WCF runtime serializes the exception into XML and passes that to the client.


What is Asynchronous Messaging ?

Asynchronous messaging describes a way of communications that takes place between two applications or systems, where the system places a message in a message queue and does not need to wait for a reply to continue processing.

Why in WCF, the "httpGetEnabled" attribute is essential?

The attribute "httpGetEnabled" is essential because we want other applications to be able to locate the metadata of this service that we are hosting.


<serviceMetadata httpGetEnabled="true" /> 


Without the metadata, client applications can't generate the proxy and thus won't be able to use the service.

What is "Automatic activation" in WCF?

Automatic Activation means that the service is not necessary to be running in advance. When any message is received by the service it then launches and fulfills the request. But in case of self hosting the service should always be running.

Can we propagate CLR Exception across service boundaries in WCF ?

No, we cannot propagate CLR Exceptions across service boundaries. If you want to propagate exception details to the clients then it can be done through "FaultContract " attribute. This way a custom exception is being passed to the client.

In WCF, what happens if there is an unhandled exception ?


  • The service model returns a generic SOAP fault to the client which does not include any exception specific details by default. 
  • However, an exception details in SOAP faults can be included using IncludeExceptionDetailsInFaults attribute. 
  • If IncludeExceptionDetailsInFaults is enabled, exception details including stack trace are included in the generated SOAP fault. IncludeExceptionDetailsInFaults should be enabled for debugging purposes only. 
  • Sending stack trace details is risky. 
  • IncludeExceptionDetailsInFaults can be enabled in the web.config file :- 


<behaviors>
            <serviceBehaviors>
                <behavior name="ServiceGatewayBehavior">
                    <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
                    <serviceMetadata httpGetEnabled="true"/>
                    <!-- 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 -->
                    <serviceDebug includeExceptionDetailInFaults="true"/>
                </behavior>
            </serviceBehaviors>
        </behaviors>


Are SOAP faults inter-operable ?

Yes, SOAP faults are inter-operable as they are expressed to clients in XML form. .Net exception classes are not good to be used in SOAP faults. Instead we should define our own DataContract class and then use it in a FaultContract.


public interface ICalculator
{       
    [OperationContract]
    [FaultContract(typeof(MathFault))]
    int Divide(int n1, int n2);
}
[DataContract]
public class MathFault
{  
    [DataMember]  
    private string operation { get; set; }
    [DataMember]    
    private string problemType { get; set; }    
}
public int Divide(int n1, int n2)
{
    try
    {
       return n1 / n2;
    }
    catch (DivideByZeroException)
    {
      MathFault objMath = new MathFault();
        objMath.operation = "division";
        objMath.problemType = "divide by zero";

        throw new FaultException<MathFault>(objMath);
    }
}


State 3 primary aspects of WCF ?


  • Inter-operability with applications built on differnet technologies. 
  • Unification of the original Dotnet Framework communication Technologies. 
  • Support for Service Oriented Architecture (SOA).


If you have a Dotnet Web Service and a Java Client Application, then which Communication technology will you use other than WCF ?

Web Service (that is), ASMX will be the most likely way to achieve cross-vender inter-operability.

In WCF, which binding is used if you are using WCF-to-WCF communication between processes on the same machine ?

NetNamedPipesBinding binding is used.

Defining endpoints programmatically in WCF is a good practice or not ?

Even though defining endpoints programmatically is possible, the most common approach today is to use a configuration file associated with the service.Endpoint definitions embedded in code are difficult to change when a service is deployed, yet some endpoint characteristics, such as the address, are very likely to differ in different deployments. Defining endpoints in config files makes them easier to change, since changes don’t require modifying and recompiling the source code for the service class.

Which element is the prime element in the config file in which all WCF-based application configuration is contained ?

Configuration information for all services implemented by a WCF-based application is contained within the system.serviceModel element. This element contains a services element that can itself contain one or more service elements.

Briefly explain WCF Data Services ?

WCF Data Services are used when you want to expose your data model and associated logic through a RESTful interface. It includes a full implementation of the Open Data (OData) Protocol for .NET to make this process very easy. WCF Data Services was originally released as ‘ADO.NET Data Services’ with the release of .NET Framework 3.5.

Can you show a sample of Duplex Contract in WCF ?

[ServiceContract(Namespace = "http://www.Microsoft.com",

                     SessionMode = SessionMode.Required, 

                     CallbackContract = typeof(IDuplexCallBack) )]
    public interface IService1
    {
        [OperationContract(IsOneWay = true)]
        void getData();        
    }
    public interface IDuplexCallBack
    {
        [OperationContract(IsOneWay = true)]
        void filterData(DataSet Output);
    }

In the above code,
  •  getData() is a method which will be called by the client on the Service. This getdata() is implemented in the server side. 
  • filterData() is a method which will be called by the server on the Client. This method is implemented in the client side. 
  • CallbackContract is the name of the contract which will be called by the server on the client to raise an event or to get some information from the client.

In what are the different ways a WCF Metadata can be accessed ?

WCF Metadata can be accessed in two ways :- 
  • WSDL document can be generated which represents the endpoints and protocols 
  • Or the ServiceHost can expose a metadata exchange endpoint to access metadata at runtime

Difference between reliable messaging and reliable sessions in WCF ?

  • Reliable messaging is concerned with ensuring that messages are delivered exactly once. Reliable session provides a context for sending and receiving a series of reliable messages. reliable sessions have a dependency on reliable messaging. 
  • You have to use reliable messaging to provide an end-to-end reliable session between a client application and a service. 

What is Windows Server AppFabric?

  • It is a set of extensions to the Windows OS aimed at making it easier for developers to build faster, scalable, and more-easily managed services. 
  • It provides a distributed in-memory caching service and replication technology that helps developers improve the speed and availability of .NET Web applications and WCF services. 
  • If you are hosting WCF services by using IIS or WAS in a production environment, you might want to consider implementing Windows Server AppFabric. 
  • You can use use AppFabric in place of Memcached because AppFabric provides more feature than Memcached

What is the difference between RIA and WCF services?

  • WCF is concerned with technical aspects of communication between servers across tiers and message data management whereas RIA Web Services delegates this 
  • functionality to WCF. 
  • WCF is a generic service that could be used in Windows Forms for example whereas RIA Web Services is focused on Silverlight and ASP.NET clients 
  • WCF does not generate source code to address class business logic such as validation that may need to be shared across tiers like RIA Web Services does. 
  • The RIA Services can either exist on top of WCF or replace the WCF layer with RIA Services using alternative data source e.g. an ORM layer(EF/NHibernate etc.) 
  • RIA Services allow serializing LINQ queries between the client and server.

How many different types of authorization supported by WCF ?

  • Role-based : Map users to roles and check whether a role can perform the requested operation. 
  • Identity-based : Authorize users based on their identity. 
  • Claims-based : Grant or deny access to the operation or resources based on the client’s claims. 
  • Resource-based : Protect resources using access control lists (ACLs)



No comments:

Post a Comment