一个WCF服务和多个数据库连接

时间:2011-06-14 20:35:45

标签: wcf dependency-injection

在启动WCF客户端之后,让WCF主机访问多个数据库而不传递名称的最佳方法是什么?我正在使用DI并想做类似的事情:

wcfCourseManagement.CourseManagementClient _wcfclient = null;

public WCFCourseRepository(ISystemService systemService): base(systemService)
{
    _wcfclient = new wcfCourseManagement.CourseManagementClient();    
    ServiceEndpoint endpoint = systemService.GetServiceEndpoint("CourseManagement");
    if (endpoint != null)
        _wcfclient.Endpoint.Address = new EndpointAddress(new Uri(endpoint.Endpoint));
    if (_wcfclient.GetRemoteSystemID != systemService.ClientSystem.PreferredRemoteSystemID)
        _wcfclient.SetCurrentSystem(systemService.ClientSystem.PreferredRemoteSystemID);
}

如果需要,SetCurrentSystem将告诉WCF主机它应该重建数据存储库。系统ID只是服务器用于加载数据库配置信息的配置存储中的ID。但是,由于WCF主机接线的设计,在主机的无参数构造期间注入“默认”SystemID。我想覆盖它,但我不希望在每次调用中都传递它,因为这会破坏客户端和服务器代码共享的当前存储库接口。实现这种行为的最直接方法是什么?

1 个答案:

答案 0 :(得分:1)

执行此操作的一种方法是使用每个会话实例管理。您可以在第一次调用会话时设置要使用的数据库,然后将该数据库用于整个会话。

请参阅:http://msdn.microsoft.com/en-us/magazine/cc163590.aspx

相关问题