In the normal case when we want to consume a WCF service, we need to create proxy at client side. To create proxy, service must expose metadata endpoint.
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,
ServiceWithoutProxy.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace ServiceContractDll
{
[ServiceContract]
public interface ServiceWithoutProxy
{
[OperationContract]
string GetData(int value);
}
}
Assume we have created a service library called ServiceContractDll
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 ServiceContractDll;
namespace WcfService1
{
public class Service1 : ServiceWithoutProxy
{
public string GetData(int value)
{
return string.Format(“You entered: {0}”, value);
}
}
}
Add the EndPoint in config file.
<services>
<service name =”WcfService1.Service1″ >
<endpoint address =”” binding =”basicHttpBinding” contract =”ServiceContractDll.ServiceWithoutProxy”/>
<host>
<baseAddresses>
<add baseAddress =”http://localhost:8181/Service1.svc”/>
</baseAddresses>
</host>
</service>
</services>
In above config file
ServiceContractDll.ServiceWithoutProxy is name of the service contract exposed.
There is no metadata exchange endpoint in the config file.
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
Do not add the service reference.
Add the reference of System.ServiceModel.
Add the reference of class library created in step1.
Now we need to Create a ChannelFactory
ChannelFactory<ServiceWithoutProxy> factory = null;
Create a binding of the type exposed by service
BasicHttpBinding binding = new BasicHttpBinding();
Create EndPoint address
EndPointAddress address = new EndPointAddress(“http://localhost:4684/Service1.svc“);
Pass Binding and EndPoint address to ChannelFactory
factory = new ChannelFactory<ServiceWithoutProxy>(binding,address)
Now create the channel as below ,
ServiceWithoutProxy channel = factory.CreateChannel();
Call the service method on this channel as below ,
string returnmessage = channel.GetData(9);
Thanks,
Prashant
Leave a comment