Showing posts with label WCF Tutorial. Show all posts
Showing posts with label WCF Tutorial. Show all posts

SOA Principles

SOA Principles


Service-oriented architecture ( SOA ) is based on the four fundamental tenets that follow:

1. Explicit Boundaries

Everything needed by the service to provide its functionality should be passed to it when it is invoked. All access to the service should be via its publicly exposed interface; no hidden assumptions must be necessary to invoke the service. “Services are inextricably tied to messaging in that the only way into and out of a service are through messages”. A service invocation should – as a general pattern – not rely on a shared context; instead service invocations should be modeled as stateless. An interface exposed by a service is governed by a contract that describes its functional and non-functional capabilities and characteristics. The invocation of a service is an action that has a business effect, is possibly expensive in terms of resource consumption, and introduces a category of errors different than those of a local method invocation or remote procedure call. A service invocation is not a remote procedure call.

While consuming and providing services certainly should be as easy as possible, it is therefore undesirable to hide too much of the fact that an interaction with a service takes place. The message sent to or received from the service, the service contract, and the service itself should all be first-class constructs within the SOA. This means, for example, that programming models and tools that are used should at least provide an API that exposes these concepts to the service programmer. In summary, a service exposes its functionality through an explicit interface that encapsulates its internals; interaction with a service is an explicit act, relying on the passing of messages between consumer and provider.

2. Shared Contract and Schema, not Class

Starting from a service description (a contract), both a service consumer and a service provider should have everything they need to consume or provide the service. Following the principle of loose coupling, a service provider can not rely on the consumer’s ability to reuse any code that it provides in its own environment; after all, it might be using a different development or runtime environment. This principle puts severe limits on the type of data that can be exchanged in an SOA. Ideally, the data is exchanged as XML documents validatable against one or more schemas, since these are supported in every programming environment one can imagine.

As a consequence, adherence to this principle is not possible in a DCOM-based or RMI-based environments - which basically rules them out as a valid option for SOA.


3. Policy-driven

To interact with a service, two orthogonal requirement sets have to be met:

1.the functionality, syntax and semantics of the provider must fit the consumer’s requirements,
2.the technical capabilities and needs must match.
For example, a service provider may offer exactly the service a consumer needs, but offer it over JMS while the consumer can only use HTTP (e.g. because it is implemented on the .NET platform); a provider might require message-level encryption via the XML Encryption standard, while the consumer can only support transport-level security using SSL. Even in those cases where both partners do have the necessary capabilities, they might need to be “activated” – e.g. a provider might encrypt response messages to different consumers using different algorithms, based on their needs.

To support access to a service from the largest possible number of differently equipped and capable consumers, a policy mechanism has been introduced as part of the SOA tool set. While the functional aspects are described in the service interface, the orthogonal, non-functional capabilities and needs are specified using policies.

4. Autonomous

Related to the explicit boundaries principle, a service is autonomous in that its only relation to the outside world – at least from the SOA perspective – is through its interface. In particular, it must be possible to change a service’s runtime environment, e.g. from a lightweight prototype implementation to a full-blown, application server-based collection of collaborating components, without any effect on its consumers. Services can be changed and deployed, versioned and managed independently of each other. A service provider can not rely on the ability of its consumers to quickly adapt to a new version of the service; some of them might not even be able, or willing, to adapt to a new version of a service interface at all (especially if they are outside the service provider’s sphere of control).

5. Wire formats, not Programming Language APIs

Services are exposed using a specific wire format that needs to be supported. This principle is strongly related to the first two principles, but introduces a new perspective: To ensure the utmost accessibility (and therefore, long-term usability), a service must be accessible from any platform that supports the exchange of messages adhering to the service interface as long as the interaction conforms to the policy defined for the service. For example, it is a useful test for conformance to this principle to consider whether it is possible to consume or provide a specific service from a mainstream dynamic programming language such as Perl, Python or Ruby. Even though none of these may currently play any role in the current technology landscape, this consideration can serve as a litmus test to assess whether the following criteria are met:

•All message formats are described using an open standard, or a human readable description
•It is possible to create messages adhering to those schemas with reasonable effort without requiring a specific programmer’s library
•The semantics and syntax for additional information necessary for successful communication, such as headers for purposes such as security or reliability, follow a public specification or standard
•At least one of the transport (or transfer) protocols used to interact with the service is a (or is accessible via a) standard network protocol

6. Document-oriented

To interact with services, data is passed as documents. A document is an explicitly modeled, hierarchical container for data. Self-descriptiveness is one important aspect of document-orientation. Ideally, a document will be modeled after real-world documents, such as purchase orders, invoices, or account statements. Documents should be designed so that they are useful on the context of a problem domain, which may suggest their use with one or more services.

Similarly to a real-world paper document, a document exchanged with a service will include redundant information. For example, a customer ID might be included along with the customer’s address information (although the customer ID would be enough). This redundancy is explicitly accepted since it serves to isolate the service interface from the underlying data model of both service consumer and service provider. Whena document-oriented pattern is applied, service invocations become meaningful exchanges of business messages instead of context-free RPC calls. While not an absolute required, it can usually be assumed that XML will be used as the document format/syntax.

Messages flowing between participants in an SOA connect disparate systems that evolve independently of each other. The loose coupling principle mandates that the dependence on common knowledge ought to be as small as possible. When messages are sent in a Distributed Objects or RPC infrastructure, client and server can rely on a set of proxy classes (stubs and skeletons) generated from the same interface description document. If this is not the case, communication ceases on the assumption that the contract does not support interaction between those two parties. For this reason, RPC-style infrastructures require synchronized evolution of client and server program code.

This is illustrated by the following comparison. Consider the following message:

2006-03-1347113and compare it to:

2006-03-13 4711 3While it is obvious that the second alternative is human-readable while the first one is not, it is also notable that in the second case, a participant that accesses the information via a technology such as XPath will be much better isolated against smaller, non-breaking changes than one that relies on the fixed syntax. Conversely, using a self-descriptive message format such as XML while still using RPC patterns, such as stub and skeleton generation, serves only to increase XML’s reputation as the most effective way to waste bandwidth. If one uses XML, the benefits should be exploited, too. (See this paper for an excellent discussion of why many current Web services stacks fail this test.)

7. Loosely coupled

Most SOA proponents will agree that loose coupling is an important concept. Unfortunately, there are many different opinions about the characteristics that make a system “loosely coupled”. There are multiple dimensions in which a system can be loosely or tightly coupled, and depending on the requirements and context, it may be loosely coupled in some of them and tightly coupled in others. Dimensions include:

•Time: When participants are loosely coupled in time, they don’t have to be up and running at the same time to communicate. This requires some way of buffering/queuing in between them, although the approach taken for this is irrelevant. When one participant sends a message to the other one, it does not rely on an immediate answer message to continue processing (neither logically, nor physically).
•Location: If participants query for the address of participants they intend to communicate with, the location can change without having to re-program, reconfigure or even restart the communication partners. This implies some sort of lookup process using a directory or address that stores service endpoint addresses.
•Type: In an analogy to the concept of static vs. dynamic and weak vs. strong typing in programming languages, a participant can either rely on all or only on parts of a document structure to perform its work.
•Version: Participants can depend on a specific version of a service interface, or be resilient to change (to a certain degree). The more exact the version match has to be, the less loosely coupled the participants (in this dimension). A good principle to follow is Postel’s Law: Service providers should be implemented to accept as many different versions as possible, and thus be liberal in what they accept (and possibly even tolerant of errors), while service consumers should do their best to conform to exact grammars and document types. This increases the overall system’s stability and flexibility.
•Cardinality: There may be a 1:1-relationship between service consumers and service providers, especially in cases where a request/response interaction takes place or an explicit message queue is used. In other cases, a service consumer (which in this case is more reasonably called a “message sender” or “event source” may neither know nor care about the number of recipients of a message.
•Lookup: A participant that intends to invoke a service can either rely on a (physical or logical) name of a service provider to communicate with, or it can perform a lookup operation first, using a description of a set of capabilities instead. This implies a registry and/or repository that is able to match the consumer’s needs to a providers capabilities (either directly or indirectly).
•Interface: Participants may require adherence to a service-specific interface or they may support a generic interface. If a generic interface is used, all participants consuming this generic interface can interact with all participants providing it. While this may seem awkward at first sight, the principle of a single generic (uniform) interface is at the core of the WWW’s architecture.
It is not always feasible nor even desirable to create a system that is loosely coupled in all of the dimensions mentioned above. For different types of services, different trade-offs need to be made. More discussion about dimensions of loose coupling can be found in Carlos Perez's excellent writings, e.g. here or here.


8. Standards-compliant

A key principle to be followed in an SOA approach is the reliance on standards instead of proprietary APIs and formats. Standards exists for technical aspects such as data formats, metadata, transport and transfer protocols, as well as for business-level artifacts such as document types (e.g. in UBL).

While this may seem absolutely obvious to many, some argue that a proprietary solution, such as those provided by some EAI or messaging vendors, follows SOA principles. This principle highlights the importance of standards - the more, the better. Of course it's arguable what relevance a specific has as there are so many to choose from. The most important aspect of any standard is its acceptance (which basically translates to "Microsoft needs to be on the author list" in case of Web services).


9. Vendor independent

No architectural principle should rely on any particular vendor’s product. To transform an abstract concept into a concrete, running system, it’s unavoidable to decide on specific products, both commercial and free/open source software. None of these decisions must have implications on an architectural level. This implies reliance on both interoperability and portability standards as much as reasonably possible. As a result, a service provider or service consumer can be built using any technology that supports the appropriate standards, not restricted by any vendor roadmap.

10. Metadata-driven

All of the metadata artifacts within the overall SOA need to be stored in a way that enables them to be discovered, retrieved and interpreted at both design and run time. Artifacts include descriptions of service interfaces, participants, endpoint and binding information, organizational units and responsibility, document types/schemas, consumer/provider relationships etc. As much as possible, usage of these artifacts should be automated by either code generation or interpretation and become part of the service and participant life cycle.

This concludes my list of principles. Even if you don't agree with all of them - and frankly, I don't expect you to, at least not with all of them -, I hope you can use them to fuel some discussion!

What are the 3 things that a WCF Services end point must have?

What are the 3 things that a WCF Services end point must have?

ABC are 3 things that WCFServices end point must have.



  • "A" stands for Address: Where is the service?


  • "B" stands for Binding: How do I talk to the service?


  • "C" stands for Contract: What can the service do for me


  • Address - Address specifies where is the service

    Binding - Binding specifies how to communicate with service

    Contract - Contract specifies what functionalities or operations are exposed by service



    Message Exchange Patterns in WCF

    Message Exchange Patterns in WCF

    There are three message exchange patterns in WCF  :

    1. One way
    2. Request Reply
    3. Duplex

    WCF Questions : What is WCF ?

    What is WCF ?

    WCF stands for Windows Communication Foundation. WCF was introduced with .NET Framework 3.0. It provides combined features of Web Services , Remoting , MSMQ and COM+ . It is used for building and deploying network distributed service

    WCF Proxy Creation ways

    WCF Proxy Creation ways


    There are different ways of creating WCF proxy :
    1. Adding service reference to a WCF service using Visual Studio

    2. Use SvcUtil.exe utility to create proxy

    3. Implementing ClientBase<T> Class


    Adding service reference to a WCF service using Visual Studio

    Right click on project in IDE , you will see three Add options as shown in the figure below :


    Click on Add Service Reference option , you'll see the dialog box that is shown in Figure  :
















    Once you've clicked OK in the dialog box, the add-in spawns SvcUtil.exe, generating the necessary proxy class and the required configuration file (or modifying it) and adding the necessary references to the project.
    This is the way to create sync WCF service proxy
    To create WCF service proxy in async way please follow below steps before clicking on OK :
    Click on Advanced Button

















    Click on Generate Asynchronous operation checkbox and then click on OK button
    Using SVCUtil.exe
    To generate manually, choose the CMD window by selecting Start | All Programs | Microsoft Windows SDK | CMD.The output files SVCUtil generates are the client proxy source code file and the application configuration file.
    Command for producing both proxy class and configuration file
    svcutil /config:app.config /out:"CalculatorService.cs" /language:csharp /n:*,SimpleClientWithProxy.CalculatorService "http://localhost/MathService/?CalculatorService.svc"
    By default , svcutil generates only synchronous method signatures in proxy . 
    Use option /async to generate both synchronous and asynchronous method signatures in proxy

    Implementing ClientBase<T> Class
     Of all the methods , implementing ClientBase is best one because we need not create proxy class every time we make changes in service implementation.
    public partial class SampleServiceClient : System.ServiceModel.ClientBase<ISampleService>, ISampleService
    {
       
        public SampleServiceClient()
        {
        }
       
        public SampleServiceClient(string endpointConfigurationName) :
                base(endpointConfigurationName)
        {
        }
       
        public SampleServiceClient(string endpointConfigurationName, string remoteAddress) :
                base(endpointConfigurationName, remoteAddress)
        {
        }
       
        public SampleServiceClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
                base(endpointConfigurationName, remoteAddress)
        {
        }
       
        public SampleServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
                base(binding, remoteAddress)
        {
        }
       
        public string SampleMethod(string msg)
        {
            return base.Channel.SampleMethod(msg);
        }
    }

    Serialization and Encoding in WCF

    Serialization and Encoding in WCF


    An important part of communication between WCF Applications is Serialization . Serialization and deserialization are processes involved in converting objects to stream of data and then retrieving objects .

    DataContractSerializer


    WCF provides new serialization engine called DataContractSerializer which performs serialization and deserialization .During serialization , DataContractSerializer converts data into XML Stream by using objects of XmlWriter class .During deserialization , DataContractSerializer reads XML Stream and retrieves data by using objects of XmlReader class.DataContractSerializer is the default and is always used unless specified otherwise.

    NetDataContractSerializer

    DataContractSerializer does not support sharing of CLR type information so WCF introduced NetDataContractSerializer to address this issue. NetDataContractSerializer includes CLR type information in the serialized XML

    XmlSerializer


    WCF also supports the XmlSerializer class. The XmlSerializer class is not unique to WCF. The XmlSerializer class does not support data contract types.
    When using Svcutil.exe or the Add Service Reference feature in Visual Studio to generate client code for a third-party service, or to access a third-party schema, an appropriate serializer is automatically selected for you. If the schema is not compatible with the DataContractSerializer, the XmlSerializer is selected.

    When to manually Switching to the XmlSerializer

    1. When migrating an application from ASP.NET Web services to WCF, you may want to reuse existing, XmlSerializer-compatible types instead of creating new data contract types.

    2. Web Services Description Language (WSDL) document is not available, for example, when creating a service with types that have to comply to a certain standardized, published schema that is not compatible with the DataContractSerializer.

    3. When creating services that follow the legacy SOAP Encoding standard.


    [ServiceContract]
    [XmlSerializerFormat]
    public class EmployeeService
    {
    [OperationContract]
    public void AddEmployee(Employee emp)
    {
    // Code not shown.
    }

    }

    //Employee is not a data contract class,
    //but is an XmlSerializer-compatible class instead.
    public class Employee
    {
    [XmlElement]
    public int employeeCode;
    [XmlElement]
    public string employeeName;
    [XmlElement]
    public string employeeSalary;
    [XmlElement]
    public Manager manager;
    }
    //Notice that the Manager class must also be XmlSerializer-compatible.

    WCF Channels

    WCF Channels

    Channels are building blocks of communication between client and server in WCF.

    WCF Contract Attributes

    Service Contract Attributes


    Service Contract Attributes are used to modify the service contract. Following are various Service Contract attributes that can be specified at service contract level :
    1. Name - controls the name of contract in the WSDL element.
    2. Namespace - controls the namespace of the contract in the WSDL element
    3. ConfigurationName - specifies the name of the service element in the configuration file to use.
    4. SessionMode - specifies whether the contract requires a binding that supports sessions.
    5. CallbackContract - specifies the return or call back contract in a two-way (duplex) conversation.
    6. HasProtectionLevel and ProtectionLevel - indicates whether all messages supporting the contract have a explicit ProtectionLevel value, and if so, what that level is.

    Operation Contract Attributes


    Operation Contract Attributes are used to modify the operation contract. Following are various Operation Contract attributes that can be specified at operation contract level :
    1. Action - specifies the action that uniquely identifies this operation. dispatches request messages to methods based on their action.
    2. AsyncPattern - indicates that the operation is implemented or can be called asynchronously using a Begin/End method pair.
    3. HasProtectionLevel - indicates whether the ProtectionLevel property has been explicitly set.
    4. IsOneWay - indicates that the operation only consists of a single input message. The operation has no associated output message.
    5. IsInitiating - specifies whether this operation can be the initial operation in a session.
    6. IsTerminating - specifies whether attempts to terminate the current session after the operation completes.
    7. ProtectionLevel - specifies the message-level security that an operation requires at run time.
    8. ReplyAction - specifies the action of the reply message for the operation.

    Data Contract Attribute


    Following are the Data Contracts attributes :
    Name - The default name of a data contract for a given type is the name of that type. To override the default, set the Name property of the DataContractAttribute to an alternative name.
    Namespace - By default, any given CLR namespace (in the format Clr.Namespace) is mapped to the namespace http://schemas.datacontract.org/2004/07/Clr.Namespace. To override the data contract namespace for each type, set the Namespace property of the DataContractAttribute.

    WCF Transactions

    WCF Transactions


    Transactions consists of set of activities in which all activities succeeds or fails.
    Transactions follows ACID properties .

    1. Atomicity ( A ) - means all transactions are either committed or rolled back to previous state

    2. Consistency ( C ) - means changes made by transactions causes transformation from one state to other

    3. Isolation ( I ) - prevents one transaction to see uncommitted changes belonging to other concurrent transaction

    4. Durability ( D ) - means updates are persistent even in case of failure

    To add transaction support to a WCF service, you will take the following actions:


    1. Add transaction support to the service contract. This is required.

    2. Add transaction support to the code that implements the service contract. This is required.

    3. Configure transactions in the implementation code. This is optional.

    4. Enable transactions on the binding. This is required.


    Step 1

    Create a Service Contract with two Operation Contract


    We will create a service contract with two operations .

    [OperationContract]
    Employee SaveEmployeeData(Employee empData);
    [OperationContract]
    void SaveEmployeeSalary(Employee empData , EmployeeSalary empSalary );
     
    SaveEmployeeData to save Employee Details like ID , Name and Role.
    SaveEmployeeSalary to save Employee Details like ID and Salary.
     
    Step 2

    Add Transaction Support to the Operation Contract


    To add transaction support to operation contract , we have to use TransactionFlow attribute .
    The TransactionFlow attribute specifies whether the operation supports transactions. There are three possible values for this attribute:
    1. NotAllowed : The operation cannot participate in a transaction. This is the default value for this attribute.
    2. Allowed : The operation will participate in a transaction if the client creates one.
    3. Mandatory : In order to call this operation, the client must create a transaction.  
    When the operation has TransactionFlowOption.NotAllowed attribute , the client cannot propagate its transaction to the service. Even if transaction flow is enabled at the binding and the client has a transaction, transaction will not propagate to the service. TransactionFlowOption.NotAllowed is the default TransactionFlowOption value of the TransactionFlow attribute.


    TransactionFlowOption.Allowed means the client may or may not have a transaction to propagate to the service. Trying to call a service without a transaction does not throw an exception on the client. With Allowed flow, the client’s transaction always propagates to the service. Once again, the service may or may not use the client’s transaction.

    TransactionFlowOption.Mandatory means the client must have a transaction to propagate to the service. Trying to call a service without a transaction throws an exception on the client. With mandatory flow, the client’s transaction always propagates to the service. Once again, the service may or may not use the client’s transaction.
     
    [OperationContract]
    [TransactionFlow(TransactionFlowOption.Allowed)]
    Employee SaveEmployeeData(Employee empData);


    [OperationContract]
    [TransactionFlow(TransactionFlowOption.Allowed)]
    void SaveEmployeeSalary(Employee empData , EmployeeSalary empSalary );

    Step 3

    Add TransactionScopeRequired Attribute to the Implementation


    TransactionScopeRequired Attribute can be set to true or false. To enable transaction set  TransactionScopeRequired attribute to true.

    [OperationBehavior(TransactionScopeRequired = true)]
    public Employee SaveEmployeeData(Employee empData)
    {
    SqlConnection objConnection = new SqlConnection(strConnection);
    objConnection.Open();
    SqlCommand objCommand = new SqlCommand("INSERT INTO Employee values ('" + empData.EmpID + "','" + empData.EmpName + "','" + empData.EmpRole + "')", objConnection);
    objCommand.ExecuteNonQuery();
    objConnection.Close();
    return empData;

    }
    [OperationBehavior(TransactionScopeRequired = true)]
    public void SaveEmployeeSalary(Employee empData , EmployeeSalary empSalary )
    {
    SqlConnection objConnection = new SqlConnection(strConnection);
    objConnection.Open();
    SqlCommand objCommand = new SqlCommand("INSERT INTO EmployeeSalary values ('" + empData.EmpID + "','" + empSalary.EmpSalary + "','" + empSalary.EmpCurrency + "')", objConnection);
    objCommand.ExecuteNonQuery();
    objConnection.Close();

    }

    Step 4

    Enable Transaction Flow using WCF Service Configuration File



    We also need to enable transactions for wsHttpBinding by setting the transactionFlow attribute to true.

    <bindings>
    <wsHttpBinding>
    <binding name="TransactionalBind" transactionFlow="true"/>
    </wsHttpBinding>
    </bindings>

    The transaction enabled binding we need to attach with the end point through which our WCF service is exposed.

    <endpoint address="" binding="wsHttpBinding"
    bindingConfiguration="TransactionalBind" contract="WcfService1.IService1">

    Step 5


    Call the two Operations in One Transaction


    Below is Client code to call these operations :
    using (TransactionScope ts = new TransactionScope(TransactionScopeOption.RequiresNew))
    {
    try
    {
    ServiceReference1.Service1Client obj = new ServiceReference1.Service1Client();
    obj.SaveEmployeeData(empData);
    obj.SaveEmployeeSalary(empData , empSalary);
    ts.Complete();
    }
    catch (Exception ex)
    {
    ts.Dispose();
    }
    }

    WCF Tutorial

    WCF - Windows Communication Foundation

    WCF - Windows Communication Foundation


    Introduction


    .NET Framework 3.0 introduces four important features :
    • Windows Communication Foundation ( WCF )
    • Windows Presentation Foundation ( WPF )
    • Windows Workflow Foundation ( WWF )
    • Windows Card Space
    WCF provides combined features of Web Services , Remoting , MSMQ and COM+ . It is used for building and deploying network distributed services.

    WCF provides few important features. So lets discuss few difference and features which make it better than Webservices

    Difference between Web Services and WCF


    1. Hosting


    Web Service can be hosted in IIS whereas WCF can be hosted in IIS , WAS and self hosting

    2. Programming


    Web Service Class requires [WebService] attribute whereas WCF requires [ServiceContract] attribute
    Web Service method requires [WebMethod] attribute whereas WCF methods requires [OperationContract] attribute

    3. Serialization


    Web Service uses System.Xml.Serialization for serialization whereas WCF uses System.Runtime.Serialization for serialization.

    4. Protocols


    Web Service supports Security protocol whereas WCF supports Security , Reliable Messaging and Transaction protocols

    5. Encoding


    Web Service uses XML 1.0 , MTOM , DIME and Custom encoding whereas WCF uses XML 1.0 , MTOM , Binary and Custom encoding.

    6. Transport


    Web Service uses HTTP and TCP as transport protocols whereas WCF uses HTTP, TCP, Named Pipe , MSMQ and P2P as transport protocols.

    WCF Endpoints


    WCF Endpoints 
    WCF Service can expose number of endpoints. Client communicates  with WCF Service using these endpoints. Endpoints consists of three parts :

    Address - Address specifies where is the service
    Binding - Binding specifies how to communicate with service
    Contract - Contract specifies what functionalities or operations are exposed by service

    Hence Endpoint is ABC ( A=Address , B=Binding, C=Contract )












    Concurrency Mode in WCF

    Concurrency Mode in WCF


    Concurrency is used to control number of threads active in an InstanceContext at any one time.


    The following three concurrency modes are available:

    Single: Each instance context is allowed to have a maximum of one thread processing messages in the instance context at a time. Other threads wishing to use the same instance context must block until the original thread exits the instance context.

    Multiple: Each service instance can have multiple threads processing messages concurrently. The service implementation must be thread-safe to use this concurrency mode.

    Reentrant: Each service instance processes one message at a time, but accepts re-entrant operation calls. The service only accepts these calls when it is calling out through a WCF client object.

    Instance Context Mode in WCF

    Instance Context Mode in WCF


    InstanceContextMode is used to control how service instances are allocated in response to client calls.

    InstanceContextMode has following values:

    Single – One service instance is allocated for all client calls.

    PerCall – One service instance is allocated for each client call.

    PerSession – One service instance is allocated for each client session.


    The default setting for InstanceContextMode is PerSession.


    How to set InstanceContextMode  ?

    Steps to set InstanceContextMode  in WCF :

    1.Apply the ServiceBehaviorAttribute to the service class.

    2.Set the InstanceContextMode property to one of the following values: PerCall, PerSession, or Single.


    Per Session Service and Client

    Per Session Service Code
    [ServiceContract(Session = true)]
    interface IMyContract
    {
    [OperationContract]
    void MyMethod();
    }

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
    class MyService : IMyContract,IDisposable
    {
    int m_Counter = 0;

    MyService()
    {
    Trace.WriteLine("MyService.MyService()");
    }

    public void MyMethod()
    {
    m_Counter++;
    Trace.WriteLine("Counter = " + m_Counter);
    }

    public void Dispose()
    {
    Trace.WriteLine("MyService.Dispose()");
    }
    }

    Client Code :
    MyContractProxy proxy = new MyContractProxy();
    proxy.MyMethod();
    proxy.MyMethod();
    proxy.Close();

    Output :
    MyService.MyService()
    Counter = 1
    Counter = 2
    MyService.Dispose()

    Singleton Service and Client :

    Singleton Service Code :

    Service Code :
    [ServiceContract(Session=true)]
    interface IMyContract
    {
    [OperationContract]
    void MyMethod();
    }

    [ServiceContract]
    interface IMyOtherContract
    {
    [OperationContract]
    void MyOtherMethod();
    }

    [ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
    class MySingleton : IMyContract, IMyOtherContract, IDisposable
    {
    int m_Counter = 0;
    public MySingleton()
    {
    Trace.WriteLine("MyService.MyService()");
    }
    public void MyMethod()
    {
    m_Counter++;
    Trace.WriteLine("Counter = " + m_Counter);
    }
    public void MyOtherMethod()
    {
    m_Counter++;
    Trace.WriteLine("Counter = " + m_Counter);
    }
    public void Dispose()
    {
    Trace.WriteLine("MyService.Dispose()");
    }
    }

    Client Code :
    MyContractProxy proxy1 = new MyContractProxy();
    proxy1.MyMethod();
    proxy1.Close();

    MyOtherContractProxy proxy2 = new MyOtherContractProxy();
    proxy2.MyOtherMethod();
    proxy2.Close();

    Output :
    MyService.MyService()
    Counter = 1
    Counter = 2

    Service Throttling Behavior in WCF

    Service Throttling Behavior in WCF


    The ServiceThrottlingBehavior class exposes properties that you can use to limit how many instances or sessions are created at the application level. Using this behavior, you can fine-tune the performance of your Windows Communication Foundation (WCF) application.

    Controlling Service Instances and Concurrent Calls


    ServiceThrottlingBehavior exposes three properties :
    1. MaxConcurrentSessions

    2. MaxConcurrentCalls

    3. MaxConcurrentInstances


    MaxConcurrentSessions
    Specifies maximum number of sessions a service host accepts. The default is 10.

    MaxConcurrentCalls
    Gets or sets a value that specifies the maximum number of messages actively processing across a ServiceHost.The default is 16.

    MaxConcurrentInstances
    Gets or sets a value that specifies the maximum number of InstanceContext objects in the service that can execute at one time.The default is 26.

    These values are set in application configuration file :
    <serviceThrottling
    maxConcurrentCalls="1"
    maxConcurrentSessions="1"
    maxConcurrentInstances="1"
    />


    Programmatic Throttling


    ServiceHost host = new ServiceHost(typeof(MyService));

    ServiceThrottlingBehavior throttle;

    throttle = host.Description.Behaviors.Find();

    if(throttle == null)

    {

    throttle = new ServiceThrottlingBehavior();

    throttle.MaxConcurrentCalls = 12;

    throttle.MaxConnections = 34;

    throttle.MaxInstances = 56;

    host.Description.Behaviors.Add(throttle);

    }

    host.Open();

    Reading Throttled Values :


    class MyService : ...
    {
    public void MyMethod() //Contract operation
    {
    ChannelDispatcher dispatcher = OperationContext.Current.Host.
    ChannelDispatchers[0] as ChannelDispatcher;

    ServiceThrottle serviceThrottle = dispatcher.ServiceThrottle;

    Trace.WriteLine("MaxConcurrentCalls = " +
    serviceThrottle.MaxConcurrentCalls);
    Trace.WriteLine("MaxConnections = " +
    serviceThrottle.MaxConnections);
    Trace.WriteLine("MaxInstances = " + serviceThrottle.MaxInstances);
    }
    }

    Hosting WCF Service

    WCF service can be hosted in four ways :


    1. Internet Information Server (IIS)


    WCF services can be hosted within Internet Information Services. There is no requirement to write hosting code as part of the application and IIS automatically activates the service code as required. Services also benefit from IIS features such as management of process lifetime and automatic application restart after configuration changes. Services can be run within IIS by creating a .svc file, which contains the service code, and a configuration file, which contains binding information, and then saving them in an IIS virtual directory.

    2. Windows Activation Service (WAS)


    Windows Activation Service is the new process activation mechanism that is a feature of IIS 7.0. WAS builds on the existing IIS 6.0 process and hosting models, but is no longer dependent on HTTP. Although IIS 7.0 uses WAS over HTTP, WCF can use WAS to provide message-based activation over other protocols, such as TCP and named pipes. This helps WCF applications to take advantage of WAS features, such as process recycling, rapid fail protection, and the common configuration system, which were previously available only to HTTP-based applications.

    3. Self-hosting


    WCF services can be hosted inside any .NET managed application, such as console applications and Windows Forms or Windows Presentation Foundation (WPF) graphical applications. A developer creates a class that implements a WCF service contract interface, and specifies binding information in the application configuration file. The application code can then use an instance of System.ServiceModel.ServiceHost to make the service available at a particular Uniform Resource Identifier (baseAddress in the following code example).
    [Visual Basic] Dim myHost As ServiceHost = New ServiceHost(GetType(MyService), baseAddress)
    [C#] ServiceHost myHost = new ServiceHost(typeof(MyService), baseAddress)

    4. Managed Windows Service


    A WCF service can be registered as a Windows Service, so that it is under control of the Windows Service Control Manager (SCM). This is suitable for long-running WCF services that are hosted outside of IIS in a secure environment and are not message-activated. The WCF service benefits from the features of Windows Services, such as automatic start at start time and control by the SCM.
    To host a WCF service in this way, the application must be written as a Managed Windows Service by inheriting from System.ServiceProcess.ServiceBase. It must also implement a WCF service contract interface and then create and open a ServiceHost to manage the WCF service.

    WCF Tool Support

    WCF Tool Support


    WCF provides a number of tools that make it easier to create, deploy, and administer WCF applications.

    1. COM+ Service Model Configuration Tool (ComSvcConfig.exe) - Existing COM applications can use the WCF Service moniker. The WCF Service moniker provides a representation of the service contracts and bindings for WCF Web services that is both strongly typed and COM-visible, and enables developers of COM+ applications to expose one or more interfaces on a COM+ component as Web services.

    2. Configuration Editor (SvcConfigEditor.exe) - Enables administrators and developers to create and modify configuration settings for WCF services by using a graphical user interface.

    3. Find Private Key Tool (FindPrivateKey.exe) - Retrieves a private key from a key store, for example, the name and location of the private key file that is associated with a particular X.509 certificate.

    4. ServiceModel Metadata Utility (svcutil.exe) - Generates service model code from metadata, and metadata from service model code. Most often used to generate client-side proxies to WCF services.

    5. ServiceModel Registration Tool (ServiceModelReg.exe) - Manages the registration of the WCF ServiceModel with Internet Information Services on a single computer. It can be used to register, unregister, and re-register WCF.

    6. TraceViewer Tool (SvcTraceViewer.exe) - Enables developers and administrators to view, group, and filter trace messages that relate to WCF services.

    7. WS-AtomicTransaction Configuration Utility (wsatConfig.exe) - Configures WS-AtomicTransaction support settings, such as ports, certificates, and accounts.

    WCF Contracts

    WCF Contract
    WCF contracts is used to define the behavior of WCF services. These are created in code by developers and are exposed to clients in the service metadata.

    There are five types of contracts :

    1. Service Contract - A service contract defines the operations that a service supports and maps to a portType in Web Service Description Language (WSDL). Service contracts are implemented as interfaces that are annotated with the ServiceContract attribute.

    [ServiceContract]
    public interface IRailwayTicket
    { ...}

    2. Operation contracts define the individual operations that a service supports and map to operations in WSDL. Operations are defined by adding methods to a Service Contract interface that is annotated with the OperationContract attribute.
    [OperationContract]
    void BookTrainTicket();

    3. Data contracts is used to define complex types. They are defined by applying the DataContract and DataMember attributes to classes.
    [DataContract]
    public class Ticket
    {
          [DataMember]
          public int TicketNo;
    }

    4. Message contracts describe the entire SOAP message format. They can use data contracts and serializable types to emit schema for complex types, and they also make it possible to control the SOAP message headers and body explicitly, by using a single type. Message contracts provide a simple method to add custom SOAP headers to incoming and outgoing messages.

    [MessageContract]
    public class BookingRequest
    {
            [MessageHeader]
            public string User;
            [MessageBody]
            public string TicketNo;
    }

    5. Fault Contract document the errors that WCF code is likely to produce, and WCF maps Fault objects to SOAP faults. Note that the type specified in the FaultContract does not have to be an exception, although it often will be.

    [OperationContract]
    [FaultContract(typeof(DivideByZeroException))]
    void GetDiscount();









    A Fault is generated by throwing a FaultException:



    throw new FaultException(someException);

    Labels

    .NET Framework Interview Questions (7) .NET Interview Questions (10) .NET Remoting Interview Questions (1) ADO.NET and BLOB Error (1) ADO.NET Interview Questions (4) Agile Articles (9) AJAX Articles (5) AJAX Interview Questions (11) Algorithms (2) Analytics Articles (2) Analytics Interview Questions (3) Android FAQs - Part 1 (2) Articles (13) ASP.NET Articles (24) ASP.NET Error and Resolution (4) ASP.NET Interview Questions (23) ASP.NET Tutorial (8) AWS Interview Questions (16) Business Analyst Interview Questions (1) Cloud Computing Interview Questions (16) CSharp Articles (17) CSharp Interview Questions (32) CSharp Tutorial (17) Data Analysis (2) Data Structure (1) Design Pattern Articles (5) DevOps Tutorial (1) Digital Marketing Interview Questions (1) Download Templates (1) Error Resolution (6) Excel Articles (9) Excel Macros (1) Excel Tips and Tricks (10) HTML5 Interview Questions (3) HTML5 Tutorial (3) Interview Preparation (2) Interview Questions (24) Introduction to Business Analytics (10) Introduction to Python (7) Introduction to R Programming (23) JAVA Articles (6) Java Tutorial (5) LINQ Articles (4) LINQ Interview Questions (2) LINQ Tutorial (3) Microservices Interview Questions (1) MVCInterviewQuestions (2) OOPs Interview Questions (4) Oracle 9i Tutorial (14) Oracle Articles (2) Oracle Interview Questions (15) Outlook Error (1) PHP Interview Questions (3) PHP Tutorial (3) Product Management (12) Product Management Interview Questions (14) Product Owner Interview Questions (2) Program Management (5) Project Management (13) Project Management Articles (34) Project Management Interview Questions (25) Quiz (1) RallyDev Help (1) Scrum Master Interview Questions (11) Selenium Tutorial (1) Sharepoint Articles (1) SQL Interview Questions (23) SQL Server Articles (20) SSIS Interview Questions (6) SSRS Interview Questions (1) Technical Program Management (12) Technical Program Management - Interview Questions (24) TechnicalProgramManagement (5) Threading Interview Questions (2) Tutorial (8) UML Articles (3) UML Interview Questions (2) Unix (3) UNIX Tutorial (3) WCF Articles (20) WCF Interview Questions (9) WCF Quiz (2) WCF Tutorial (16) Web Service Articles (5) Web Service Interview Questions (3) Window Azure (1) XML Articles (6) XML Interview Questions (3) XML Tutorial (3)