配置 WCF 而不使用配置文件并使用默认构造函数实例化代理客户端
说实话,我不确定这是否可能,
我想知道是否有一种方法可以删除配置文件的使用,而不必覆盖客户端代理的创建。让我举个例子:
在客户端应用程序中,我们有一个 WCF DAL 项目。这是 WCF 服务器的包装器,供客户端应用程序使用。目前,客户端应用程序需要配置文件中给出的所有绑定和端点,并且通常(在我们的项目中)会执行如下操作来包装 WCF 服务:
public MyObject GetMyObject(int id)
{
using(var service = new MyObjectDataServiceClient())
{
return service.GetMyOBject(id);
}
}
这将创建对服务器的调用并获取一个对象。如果客户端应用程序没有绑定和端点,它就会崩溃。我们可以更改数据服务客户端的每个创建来创建绑定和端点,或者创建我们自己的 chanelfactory 来为我们执行此操作,但这意味着更改当前的 WCF DAL 层代码。
我的目标是尝试创建一种将进程插入 WCF DAL 层的方法,该方法将处理绑定和端点,而无需更改使用代码,同时消除对配置文件的需要。
到目前为止,我的想法是尝试使用 TT 文件,以便它创建数据服务客户端的部分类并覆盖通道工厂部分。此操作失败,因为数据服务客户端的构造函数调用直接进入抽象类 (System.ServiceModel.ClientBase
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您在 DAL 上有绑定和端点,则可以使用客户端类的不同构造函数(采用绑定 + 端点地址的构造函数)。该构造函数完全绕过配置,因此您不需要在 config 中包含任何内容。
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.