Home > WCF > WCF # 3–Creating A Web Service (Implementing/Creating a WCF service with code and Configuration files)

WCF # 3–Creating A Web Service (Implementing/Creating a WCF service with code and Configuration files)

Hi Friends,

In our last article Creating a WCF Service completely in code we saw how to create a WCF service completely in code today we will see how to create a WCF Service using Code and Configuration files.

* WCF provides rich support for defining service attributes in configuration files.

* Defining endpoints and behaviours in configuration files makes for a much more flexible solution when compared with defining this in code.

Following is the example which will explain us how to implement a WCF service using code and This code requires a configuration file that defines behaviour and endpoint information. In this example, we do the following.

1) Define the contract.Write a .NET class that does something useful and decorate it with WCF attributes. There is no difference in a service definition, whether it is exposed in code or configuration.

2) Host the service in an operating system process so it can be accessed by a client on the network. This is done by creating a

3) Define a configuration file that specifies the base address for a service and the ABCs of the service endpoint.WCF looks in the application’s configuration file for the <ServiceModel> to apply the configuration data.

NOTE:
 
To Make the following code run 
1)IIS must be there on your machine
2)you have to take the CONSOLE APPLICATION
   
and then need to add System.ServiceModel reference to it.

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.ServiceModel; 
namespace WCFusingCode 
{ 
   
class Program 
   
{ 
       
[ServiceContract] 
       
public interface IStockService 
       
{ 
           
[OperationContract] 
           
double GetPrice(string ticker); 
       
} 

        public class StockService : IStockService 
       
{ 
           
public double GetPrice(string ticker) 
           
{ return 9.99; } 
       
} 

        static void Main(string[] args) 
       
{ 
           
ServiceHost servicehost = new ServiceHost(typeof(StockService),  
                                     
new Uri("http://localhost:8000/WCFusingCode&quot;)); 
           
servicehost.AddServiceEndpoint(typeof(IStockService),  
                                          
new BasicHttpBinding(), ""); 
           
servicehost.Open(); 
           
Console.WriteLine("Press Enere to to terminate"); 
           
Console.ReadLine(); 
           
servicehost.Close() 

        } 
   
} 
} 

 

Configuration File contents will be as follows

In the following code,the ServiceModel  section, define the endpoint. For each endpoint, define the address, binding, and contract. The address is blank, which indicates that the endpoint address is the same as the base address for the service.If there is more than one endpoint, each endpoint must have a unique address. The binding in this case is basicHttpBinding and the contract name is MyFirstWCFService.IStockService is the class name defined in the source code above.

<?xml version="1.0"?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="MyFirstWCFService.StockService">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8000//MyFirstWCFService"/>
          </baseAddresses>
        </host>
        <endpoint address=""  binding="basicHttpBinding"
                  contract="MyFirstWCFService.IStockService"></endpoint>
      </service>
   </services>
</system.serviceModel>
</configuration>

For more interesting links and daily update please subscribe our link resource website
    Todays link are follows: http://linkmentalist.wordpress.com/2011/08/11/link-resource-15-august-11-august-12/

Thanks,

Advertisements
Categories: WCF
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: