0

I am not sure this is even possible to be honest,

I am wondering if there is a way of removing the use of the config file without having to override the creation of the client proxy. Let me give an example:

In a client app we have a WCF DAL project. This is a wrapper for their WCF Server for the client app to consume. At present the client app would need all the bindings and endpoints given in the config file and would normally (in our projects) do something like the following to wrap the WCF service:

public MyObject GetMyObject(int id)
{
    using(var service = new MyObjectDataServiceClient())
    {
         return service.GetMyOBject(id);
    }
}

This would create the call to the server and get an object back. If the client app didn't have the bindings and endpoints it would blow up. We could change each creation of the data service client to create the binding and endpoint, or create our own chanelfactory to do this for us but this means changing the current WCF DAL layer code.

My goal is to try and create a way of inserting a process into the WCF DAL layer that will handle the bindings and endpoints without the consuming code having to change, whilst removing the need for the config file.

My thoughts so far were to try and use a TT file so that it would create a partial class of the data service client and override the channel factory part. This failed because of the constructor call for the data service client goes straight into the abstract class (System.ServiceModel.ClientBase<T>) and tries to get the config stuff out. I could not find a way of stopping it looking in the config via this partial class and not changing the WCF DAL service layer.

Jon
  • 15,110
  • 28
  • 92
  • 132

1 Answers1

1

If you have the binding and the endpoint at the DAL, you can use a different constructor of the client class (one which takes the binding + endpoint address). That constructor completely bypasses configuration, so you don't need to have anything in config.

carlosfigueira
  • 85,035
  • 14
  • 131
  • 171
  • Hi, I did mention in my post that I do want to avoid any changes in the DAL layer. The reason for this is to try and create a utility that can be used over multiple projects to remove the configuration element as well as projects that already have their service clients setup. My half way house solution has been to do pretty much as you suggested or to use a channelfactory with the binding and endpoint, but this does require alterations to the client DAL layers. Thanks – Jon Jun 30 '11 at 21:36
  • 1
    You'll need to pass the binding / address somewhere, so if you cannot change the existing clients, it needs to be done in the layer which creates the WCF proxies. – carlosfigueira Jun 30 '11 at 21:39
  • yep, which is why I tried to use TT files to generate a partial class for each client proxy and override it in there. But I failed to get it to stop checking in there and use my configuration instead – Jon Jun 30 '11 at 21:45
  • Yes, that won't work. You cannot change the behavior of a constructor in a partial class (the current client class) by using a partial class. Unfortunately you have two choices, which are either changing the DAL (you can even add some logic to detect whether there is configuration, and if so, continue using it by passing '*' to the constructor that takes an endpoint configuration name) so pre-existing clients won't be broken, or changing the clients themselves. – carlosfigueira Jun 30 '11 at 22:07
  • This is the conclusion I had come to. I was hoping there might have been something within the servicemodel.clientbase class that I might have missed that I could some how set (no idea how, and was a long shot) to avoid this. Thanks for your help and advice. – Jon Jul 01 '11 at 07:15