Interviews2 answers
How to call a WCF Service without using Service Reference or svcutil tool?
2 Answers
Use the ChannelFactory
BasicHttpBinding myBinding = new BasicHttpBinding();
EndpointAddress myEndpoint = new EndpointAddress("http://localhost/MathService/Ep1");
ChannelFactory myChannelFactory = new ChannelFactory(myBinding, myEndpoint);
// Create a channel.
IMath wcfClient1 = myChannelFactory.CreateChannel();
double s = wcfClient1.Add(3, 39);
Console.WriteLine(s.ToString());
((IClientChannel)wcfClient1).Close();
Pradeep SahooPosted Jan 20, 2015, 9:54 AM EST
Your service needs to have a reference of the library where interface i.e. Contract and service implementation is defined. Once you have referenced the library. Just create your own class and inherit it from ClientBase.
public class WCFServiceProxy:ClientBase,ISampleService
{
public string SampleOperation()
{
return base.Channel.SampleOperation();
}
}
Here SampleOperation is the OperationContract i declared in my ServiceContract ISampleService.
If you have noticed when you import metadata and add service reference or create proxy class via svcutil command the proxy class so created inherits from same ClientBase class and implements your contract. Here we are just doing it by coding it.
Please note calling the WCFService in such a way is cool only if client and service are both in your control because you are referencing the library.
I hope this is valuable.......Enjoy and keep sharing knowledge.
Ashish KalraPosted Jan 4, 2015, 11:43 AM EST

