.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> WCF >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Access WCF service without creating Proxy

Posted By:Dhananjay Kumar       Posted Date: November 01, 2010    Points: 50    Category: WCF    URL: http://www.dotnetspark.com  

Access WCF service without creating Proxy. Each time when we want to consume a WCF service, we need to create proxy at client side. To create proxy, service must expose metadata endpoint.
 

Accessing WCF service without creating Proxy

Each time when we want to consume a WCF service, we need to create proxy at client side. To create proxy, service must expose metadata endpoint.

Normally

1.  We create a WCF service

2.  Expose metadata endpoint

3.  Add service reference at client side to create the proxy.

4.  Using the proxy calls the service operation contracts.

Normally we call the service as



Let us assume we want to call the service using channel without creating proxy or adding the service reference.  We need to follow the below steps

Step 1

Put all the DataContract or ServiceContract in a separate DLL or class library. Add the reference of System.ServiceModel in class library.  And create the service contract as below,

MyServiceContract.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace ContractDll
{
  [ServiceContract]
    public  interface MyServiceContract
    {

      [OperationContract]
      string GetData(int value);

    }
}

Assume we have created a service library called ContractDLL

Step 2

Create a WCF Service application and implement the service contract created in step 1. For that add the reference of project created in step 1 in WCF service application.

Service1.svc.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using ContractDll;

namespace WcfService1
{
    
    public class Service1 : MyServiceContract 
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        
    }
}

Add the EndPoint in config file.


In above config file

1.  ContractDLL.MyServiceContract  is name of the service contract exposed.

2.  There is no metadata exchange endpoint in the config file.

Full Web.Config is as below,

Web.Config




  
    
  
  
    
      
        
          
          
          
          
        
      
    
    
      
               
        
          
            
          
        
            
    
    
  
 
    
  
  

Step 3

Create the client.  We are creating a console client to consume the service with channel or without creating proxy.  So follow the below steps

1.  Do not add the service reference.

2.  Add the reference of System.ServiceModel.

3.  Add the reference of class library created in step1.

Now we need to

1.  Create a ChannelFactory


2.  Create a binding of the type exposed by service


3.  Create  EndPoint address



4.  Pass Binding and EndPoint address to ChannelFactory



5.  Now create the channel as below ,

 
6.  Call the service method on this channel as below ,



So full code of client will be like below

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using ContractDll;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            ChannelFactory factory = null;
            try
            {               

                BasicHttpBinding binding = new BasicHttpBinding();
                EndpointAddress address = new EndpointAddress("http://localhost:4684/Service1.svc");
                factory = new ChannelFactory(binding, address);
                MyServiceContract channel = factory.CreateChannel();
                string resturnmessage = channel.GetData(9);
                Console.WriteLine(resturnmessage);
                Console.ReadKey(true);
            }
            catch (CommunicationException)
            {
                if (factory != null)
                    factory.Abort();

            }
            catch (TimeoutException)
            {
                if (factory != null)
                    factory.Abort();
            }
            catch (Exception ex)
            {
                if (factory != null)
                    factory.Abort();
                Console.WriteLine(ex.Message);
            }
            Console.WriteLine("Proxy closed");
            Console.ReadKey(true);
        }
    }
}

And we will get output as




 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend