Showing posts with label Windows Communication Foundation [WCF]. Show all posts
Showing posts with label Windows Communication Foundation [WCF]. Show all posts

WCF : System-Provided Bindings

BasicHttpBinding: An HTTP protocol binding suitable for connecting to Web services that conforms to the WS-I Basic Profile specification (for example, ASP.NET Web services-based services).

WSHttpBinding: An interoperable binding suitable for connecting to endpoints that conform to the WS-* protocols.

NetNamedPipeBinding: Uses the .NET Framework to connect to other WCF endpoints on the same machine.

NetMsmqBinding: Uses the .NET Framework to create queued message connections with other WCF endpoints.

WCF : What a Binding Defines?

The information in a binding can be very basic, or very complex. The most basic binding specifies only the transport protocol (such as HTTP) that must be used to connect to the endpoint. More generally, the information a binding contains about how to connect to an endpoint falls into one of the following categories.

Protocols
Determines the security mechanism being used: either reliable messaging capability or transaction context flow settings.

Encoding
Determines the message encoding (for example, text or binary).

Transport
Determines the underlying transport protocol to use (for example, TCP or HTTP).

How to control the service instance lifetime using code

Apply the ServiceBehaviorAttribute to the service class.

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

How to Implement a service operation asynchronously

In your service contract, declare an asynchronous method pair according to the .NET asynchronous design guidelines. The Begin method takes a parameter, a callback object, and a state object, and returns a System.IAsyncResult and a matching End method that takes a System.IAsyncResult and returns the return value. For more information about asynchronous calls, see Asynchronous Programming Design Patterns.

Mark the Begin method of the asynchronous method pair with the System.ServiceModel.OperationContractAttribute attribute and set the System.ServiceModel.OperationContractAttribute.AsyncPattern property to true.

WCF : How to create a basic data contract for a class or structure

Declare that the type has a data contract by applying the DataContractAttribute attribute to the class.

Define the members (properties, fields, or events) that are serialized by applying the DataMemberAttribute attribute to each member. These members are called data members.

WCF: How to create a one-way contract

Create the service contract by applying the ServiceContractAttribute class to the interface that defines the methods the service is to implement.

Indicate which methods in the interface a client can invoked by applying the OperationContractAttribute class to them.

Designate operations that must have no output (no return value and no out or ref parameters) as one-way by setting the IsOneWay property to true. Note that the operations that carry the OperationContractAttribute class satisfy a request-reply contract by default because the IsOneWay property is false by default. So you must explicitly specify the value of the attribute property to be true if you want a one-way contract for the method.

WCF : How to create a request-reply contract

Create the service contract by applying the ServiceContractAttribute class to the interface that defines the methods the service is to implement.

Indicate which methods in the interface the client can invoke by applying the OperationContractAttribute class to them.

The value of the IsOneWay property indicates whether an operation returns a reply message. If an operation has a request-reply contract, this property is set to false.

If the operation has a one-way contract, the property is set to true. All of the operations that carry the OperationContractAttribute class satisfy a request-reply contract by default because the IsOneWay property is false by default. So it is optional to explicitly specify the value of the attribute property to be false.

WCF : How to use a Windows Communication Foundation Client

//Step 1: Create an instance of the WCF Client.
EndpointAddress epAddress = new EndpointAddress("http://localhost:8000/ServiceModelSamples/Service/CalculatorService");
CalculatorClient client = new CalculatorClient(new WSHttpBinding(), epAddress);

// Step 2: Call the service operations.
// Call the Add service operation.
double value1 = 100.00D;
double value2 = 15.99D;
double result = client.Add(value1, value2);
Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);

// Call the Subtract service operation.
value1 = 145.00D;
value2 = 76.54D;
result = client.Subtract(value1, value2);
Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result);

// Call the Multiply service operation.
value1 = 9.00D;
value2 = 81.25D;
result = client.Multiply(value1, value2);
Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result);

// Call the Divide service operation.
value1 = 22.00D;
value2 = 7.00D;
result = client.Divide(value1, value2);
Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);

//Step 3: Closing the client gracefully closes the connection and cleans up resources.
client.Close();

WCF : How to create a Windows Communication Foundation client

To create a Windows Communication Foundation client

  1. Create a new project for the client in Visual Studio 2005 by doing the following steps:

    1. In Solution Explorer (on the upper right) within the same solution that contains the service, right-click the current solution, and select Add New Project.

    2. In the Add New Project dialog, select Visual Basic or Visual C#, and choose the Console Application template, and name it Client. Use the default Location.

    3. Click OK.

  2. Provide a reference to the System.ServiceModel namespace for the project: Right-click the Service project in the Solution Explorer, select the System.ServiceModel from the Component Name column on the .NET tab, and click OK.

WCF : How to run a basic Windows Communication Foundation (WCF) service

This procedure consists of the following steps:

  • Create a base address for the service.

  • Create a service host for the service.

  • Enable metadata exchange.

  • Open the service host.

Example:-

using System;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace Microsoft.ServiceModel.Samples
{
// Define a service contract.
[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
public interface ICalculator
{
[OperationContract]
double Add(double n1, double n2);
[OperationContract]
double Subtract(double n1, double n2);
[OperationContract]
double Multiply(double n1, double n2);
[OperationContract]
double Divide(double n1, double n2);
}

// Service class that implements the service contract.
// Added code to write output to the console window.
public class CalculatorService : ICalculator
{
public double Add(double n1, double n2)
{
double result = n1 + n2;
Console.WriteLine("Received Add({0},{1})", n1, n2);
Console.WriteLine("Return: {0}", result);
return result;
}

public double Subtract(double n1, double n2)
{
double result = n1 - n2;
Console.WriteLine("Received Subtract({0},{1})", n1, n2);
Console.WriteLine("Return: {0}", result);
return result;
}

public double Multiply(double n1, double n2)
{
double result = n1 * n2;
Console.WriteLine("Received Multiply({0},{1})", n1, n2);
Console.WriteLine("Return: {0}", result);
return result;
}

public double Divide(double n1, double n2)
{
double result = n1 / n2;
Console.WriteLine("Received Divide({0},{1})", n1, n2);
Console.WriteLine("Return: {0}", result);
return result;
}
}


class Program
{
static void Main(string[] args)
{

// Step 1 of the address configuration procedure: Create a URI to serve as the base address.
Uri baseAddress = new Uri("http://localhost:8000/ServiceModelSamples/Service");

// Step 1 of the hosting procedure: Create ServiceHost
ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), baseAddress);
try
{

// Step 3 of the hosting procedure: Add a service endpoint.
selfHost.AddServiceEndpoint(
typeof(ICalculator),
new WSHttpBinding(),
"CalculatorService");


// Step 4 of the hosting procedure: Enable metadata exchange.
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
selfHost.Description.Behaviors.Add(smb);

// Step 5 of the hosting procedure: Start (and then stop) the service.
selfHost.Open();
Console.WriteLine("The service is ready.");
Console.WriteLine("Press to terminate service.");
Console.WriteLine();
Console.ReadLine();

// Close the ServiceHostBase to shutdown the service.
selfHost.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine("An exception occurred: {0}", ce.Message);
selfHost.Abort();
}
}
}
}

How to Create a Windows Communication Foundation contract with an interface


  1. Open Visual Studio 2005 as an administrator by right-clicking the program in the StartRun as administrator. menu and selecting

  2. Create a new console application project. In the New Project dialog, select Visual BasicVisual C#, and choose the Console Application template, and name it Service. Use the default Location. or

  3. Change the default Service namespace to Microsoft.ServiceModel.Samples.

  4. Provide a reference to the System.ServiceModel namespace for the project: Right-click the Service project in the Solution Explorer, select The System.ServiceModelComponent Name from the .NET tab, and click OK.

Following Example:-
using System;
// Step 5: Add the using statement for the Sytem.ServiceModel namespace
using System.ServiceModel;
namespace Microsoft.ServiceModel.Samples
{
// Step 6: Define a service contract.
[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
public interface ICalculator
{
// Step7: Create the method declaration for the contract.
[OperationContract]
double Add(double n1, double n2);
[OperationContract]
double Subtract(double n1, double n2);
[OperationContract]
double Multiply(double n1, double n2);
[OperationContract]
double Divide(double n1, double n2);
}
}

Windows Communication Foundation :What is Messaging?

The messaging layer is composed of channels. A channel is a component that processes a message in some way, for example, by authenticating a message. A set of channels is also known as a channel stack. Channels operate on messages and message headers. This is different from the service runtime layer, which is primarily concerned about processing the contents of message bodies.

Windows Communication Foundation :Contracts and Descriptions

Contracts and Descriptions
Contracts define various aspects of the message system. The data contract describes every parameter that makes up every message that a service can create or consume. The message parameters are defined by XML Schema definition language (XSD) documents, enabling any system that understands XML to process the documents. The message contract defines specific message parts using SOAP protocols, and allows finer-grained control over parts of the message, when interoperability demands such precision. The service contract specifies the actual method signatures of the service, and is distributed as an interface in one of the supported programming languages, such as Visual Basic or Visual C#.