How to access WCF service without creating Proxy

No.of Views931
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  31 Oct 2010 09:10:29
Tag : WCF , General
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.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

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 

Image Loading

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]publicinterface 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.

Image Loading

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

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service name ="WcfService1.Service1" >
        <endpoint address ="" binding ="basicHttpBinding" contract ="ContractDll.MyServiceContract"/>       
        <host>
          <baseAddresses>
            <add baseAddress ="http://localhost:8181/Service1.svc"/>
          </baseAddresses>
        </host>
      </service>      
    </services>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
  
</configuration>

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

Image Loading

2.    Create a binding of the type exposed by service 

Image Loading

3.    Create  EndPoint address

Image Loading

4.    Pass Binding and EndPoint address to ChannelFactory

Image Loading

5.    Now create the channel as below ,

Image Loading

6.    Call the service method on this channel as below 

Image Loading

,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<MyServiceContract> factory = null;try{               

                BasicHttpBinding binding = new BasicHttpBinding();
                EndpointAddress address = new EndpointAddress("http://localhost:4684/Service1.svc");
                factory = new ChannelFactory<MyServiceContract>(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 

Image Loading

That's all. happy coding.

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top