Can several WCF services share a common BaseAddress?

前端 未结 3 1190
感动是毒
感动是毒 2020-12-28 09:38

I\'ve got an assembly containing several WCF services, each with its own contract. It all works nicely. The service config in the app.config for the service looks like this:

3条回答
  •  梦谈多话
    2020-12-28 09:42

    You can also set the base addresses in code if you use a custom ServiceHostFactory.

    In config you can have some app setting:

    
      
        
      
    
    

    Then make a custom ServiceHostFactory:

    public sealed class MyServiceHostFactory : ServiceHostFactory
    {
        protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {
            var baseAddy = ConfigurationManager.AppSettings["BaseAddress"];
            baseAddresses.Add(new Uri(baseAddy));
            return new ServiceHost(serviceType, baseAddresses);
        }
    
    }
    

    Then you also have to change your .svc files to use that factory:

    <%@ ServiceHost Language="C#" Debug="true" Service="MyApp.MyService" CodeBehind="MyService.svc.cs" Factory="MyApp.MyServiceHostFactory" %>
    

提交回复
热议问题