I would like to know if there is a possibility to consume an external wcf service (by external wcf service i mean a service that is not part of my solution) using channel factory. I'm aware that we can consume by generating a proxy or adding service reference but i want to know if we can use channel factory. Since its an external service we will not be having the interface class with use so need to know how will the channel factory instance look like?
2 Answers
You would need to mimic the interface the service has by looking at the WSDL file(metadata file on the service)
Then you can use a few helper methods to initialise your service,
public static TChannel GetBasicHttpService<TChannel>(string serviceEndpoint) where TChannel : class
{
EndpointAddress myEndpoint = new EndpointAddress(serviceEndpoint);
ChannelFactory<TChannel> myChannelFactory = new ChannelFactory<TChannel>(DefaultHttpBinding(), myEndpoint);
// Create a channel.
return myChannelFactory.CreateChannel();
}
public static BasicHttpBinding DefaultHttpBinding()
{
BasicHttpBinding defaultBinding = new BasicHttpBinding();
defaultBinding.MaxReceivedMessageSize = 2147483647;
defaultBinding.MaxBufferPoolSize = 2147483647;
defaultBinding.MaxBufferSize = 2147483647;
defaultBinding.ReaderQuotas.MaxArrayLength = 2147483647;
defaultBinding.ReaderQuotas.MaxStringContentLength = 2147483647;
return defaultBinding;
}
where TChannel is the Mimicked interfaced
You should know the format of the service contract interface and endpoint, or we could not create the channel factory. The reason why the channel factory is used to invoke the service is that in order to protect the WCF service, server-side disable publishing service metadata. I have made a simple demo, wish it is useful to you.
Server-side.
class Program
{
static void Main(string[] args)
{
Uri uri = new Uri("http://localhost:1900");
BasicHttpBinding binding = new BasicHttpBinding();
using (ServiceHost sh=new ServiceHost(typeof(MyService),uri))
{
sh.AddServiceEndpoint(typeof(IService), binding, "");
sh.Open();
Console.WriteLine("Service is ready...");
Console.ReadLine();
sh.Close();
}
}
}
[ServiceContract(Namespace ="mydomain")]
public interface IService
{
[OperationContract(Name ="AddInt")]
int Add1(int x, int y);
}
public class MyService : IService
{
public int Add(int x, int y)
{
return x + y;
}
}
Client-side.
class Program
{
static void Main(string[] args)
{
Uri uri = new Uri("http://localhost:1900");
BasicHttpBinding binding = new BasicHttpBinding();
using (ChannelFactory<IService> factory = new ChannelFactory<IService>(binding, new EndpointAddress(uri)))
{
IService sv = factory.CreateChannel();
var result = sv.Add(34, 3);
try
{
Console.WriteLine(result);
}
catch (Exception ex)
{
throw;
}
}
}
}
[ServiceContract(Namespace = "mydomain")]
public interface IService
{
[OperationContract(Name = "AddInt")]
int Add2(int x, int y);
}
There is no need to make sure that the client and the server has a same service interface, but they at least need to ensure that the namespace and name property of the interface is consistent between the client and server. Feel free to let me know if there is anything I can help with.