Hi Mahmoud_Gamal
If you don't want to add a service reference (why?) you could solve this by managing a WcfProxy and a communication channel for each Service by yourself. But you will have to "know" the service contract with this solution (means you can reference the assembly where the contract is defined) - I don't know if this is possible for you...
Example from my codebase: (IService is just a base interface for all my Services to identify them as service, it implements nothing)
public interface ICommandDispatcher
{
TResult ExecuteCommand<TService, TResult>(Func<TService, TResult> command)
where TResult : class
where TService : class, IService;
}
A possible implementation could look like this:
public class WcfCommandDispatcher : ICommandDispatcher
{
class WcfProxy<TService> : ClientBase<TService>
where TService : class, IService
{
public TService WcfChannel
{
get { return Channel; }
}
}
public TResult ExecuteCommand<TService, TResult>(Func<TService, TResult> command)
where TResult : class
where TService : class, IService
{
WcfProxy<TService> proxy = new WcfProxy<TService>();
try
{
TResult result = command.Invoke(proxy.WcfChannel);
proxy.InnerChannel.Close();
return result;
}
catch (Exception)
{
proxy.Abort();
throw;
}
}
}
With this helper in place you can call any service by "knowing" it's contract.
Example from my codebase where I get some list of "Cultures" from my
ICultureService
WcfCommandDispatcher dispatcher = new WcfCommandDispatcher();
IEnumerable<Culture> cultures = dispatcher.ExecuteCommand<ICultureService, IEnumerable<Culture>>(s =>
{
return s.GetAll();
}
So you see, you can just use the service inside the ExecuteCommand function and can do whatever you want. Don't Forget to add a reference to System.ServiceModel and your Service-Contract assembly. Configure your service in the same way you would do with a service reference. (App.config)
I hope this helps.
Kind regards
Johannes