使用通用配置文件拆分WCF服务

时间:2011-11-09 07:47:32

标签: .net wcf configuration web-config configgen

我有一个WCF服务。

其中有3份服务合同。

    [ServiceContract(Name = "Utility"]

public interface IService { //method defination }

[ServiceContract(Name="Documents"]

public interface IDocumentService { //method defination }

[ServiceContract(Name = "Lists"]

public interface IListService  { //method defination }

它由部分类实现。

Service.cs:

[ServiceBehavior(Name="SharePoint")]
public partial class Service : IService
{

*Implementation of IService Methods*

}

DocumentService.cs:

public partial class Service : IDocumentService
{

*Implementation of IDocumentService Methods*

}

ListService.cs:

public partial class Service : IListService
{

*Implementation of IListService Methods*

}

现在在这个项目中我有配置文件。

其中包含数据库连接字符串和其他应用程序设置

我创建了解决方案并添加了3个WCF服务应用程序。

现在我将服务拆分为3个不同的服务应用程序。喜欢

  [ServiceBehavior(Name="SharePoint")]
public class Service : IService
{

*Implementation of IService Methods*

}

DocumentService.cs:

public class DocumentService: IDocumentService
{

*Implementation of IDocumentService Methods*

}

ListService.cs:

public class ListService: IListService
{

*Implementation of IListService Methods*

}

所有3 服务应用都包含自己的配置文件

我想在所有的服务应用程序中制作通用配置文件。怎么分享他们?

3 个答案:

答案 0 :(得分:2)

您可以使用ConfigGen执行此操作。如果将模板和设置文件配置为指向网络共享,则将在构建时(或在打包时从命令行)从同一源生成所有配置。

额外的好处是,您可以使用它来同时为所有环境生成配置。

答案 1 :(得分:0)

您只需使用外部配置文件即可。将解决方案文件夹添加到项目中并创建所需的共享配置文件(连接字符串,应用程序设置等)。您可以看到this post(或MSDN)用于管理外部应用设置(文件属性),this post用于管理外部连接字符串( configSource属性) )。

答案 2 :(得分:0)

在其中一个服务应用程序项目中创建共享应用程序配置文件,或者(甚至更好)创建包含应用程序配置文件的“公共”项目。现在查看Adding an Existing Item as a Link部分,将该文件作为链接添加到不同的项目中。它将引用共享的同一物理文件。