· Indigo programming involves the use of an object model, declarative attributes, and configuration settings.
- Declarative programming. Usually Declarative attributes are used to define contracts and specify service behaviors.
- Imperative programming, in which you work with the Indigo object model in code.
- Configuration-based programming, in which you specify behavior in application configuration files. Configuration-based development helps to modify behaviors like addresses, bindings, security details, service behavior, tracing etc without recompile the program code.
· It’s not mean that you can do three different way of Indigo Programming. It is to say that WCF provide features to do few things in more than one way.
· For instance, An endpoint defined in a configuration file:
· The same endpoint definition in code:
serviceHost = new ServiceHost
serviceHost.AddEndpoint(typeof(IMyContract), new WSProfileBinding(),
http://localhost:8000/MyService/);
Hello World WCF Service
· Let’s first see, how to develop a simple WCF service which simply returns “Hello World” when client connects to it.
· Service programs contain four elements: Contract definitions, implementation code, hosting code and Endpoint definitions.
namespace HelloService
{
class Infrastructure
{
static void Main(string[] args)
{
// A Uri, which represents the address of the service.
Uri baseURI = new Uri("http://localhost/helloworld");
ServiceHost HelloWCF = new
ServiceHost(typeof(HelloService), baseURI);
//Binding (BasicHttpBinding) provides how to communicate with the endpoint.
HelloWCF.AddServiceEndpoint(typeof(HelloService),
new BasicHttpBinding(), baseURI);
HelloWCF.Open();
Console.WriteLine("Hello World Service Started.");
Console.ReadKey();
HelloWCF.Close();
}
}
[ServiceContract]
class HelloWorld
{
[OperationContract]
string Hello()
{
return ("Hello World!");
}
}
}
- The [ServiceContract] attribute specifies that class will be exposed as a WCF service.
- The attribute [OperationContract] specifies that the Hello() function which returns the string “Hello World!" will be exposed as a method on that service.
- The class Infrastructure contains the basic infrastructure including "ServiceHost" which provides the hosting infrastructure for the service and the endpoint definition.
- BasicHttpBinding uses HTTP as the transport for sending SOAP 1.1 messages.
- A service can use this binding to expose endpoints that conform to WS-I BP 1.1, such as those that ASMX clients consume.
- Both classes and interfaces can be used to define a WCF service contract. It’s better to use Interfaces, because they directly model service contracts.
· Now, let's move the endpoint definition of the “HellowWorld” service from the code to the config file.
· The config file will be read at runtime. Therefore we can modify the binding details with out rebuild and restart of the application.
· Add a configuration file “app.config” and comment the code:
HelloWCF.AddServiceEndpoint(typeof(HelloService),
new BasicHttpBinding(), baseURI);
· Add the following configuration details in the app.config:
· By default, the framework does not expose any metadata.
- To expose the metadata required to generate the proxy, you must add a
· To test our Service, let’s create a client. A client must create a proxy, which establishes a channel to the service.
· To generate client code from a service, we need to use the Svcutil tool. Svcutil not only generates contract code, but it also provides a proxy class for accessing the service.
· Clients create a new instance of the proxy, and they can then access the service through the proxy.
- svcutil http://localhost/hello?wsdl
- Then SvcUtil will get the WSDL file for the service, download the metadata, and generate the service proxy for you. Along with client code from a running service it also generates a config file.
· Add the proxy class and config file in the project and you can access the Service.
No comments:
Post a Comment