Updates to the WCF Integrations facility to make client and service support consistent.
[castle.git] / Facilities / Wcf / Castle.Facilities.WcfIntegration / Service / AbstractServiceHostBuilder.cs
blob4764bb1fb3602bf39e2c6eeec19775fb12114c66
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.Facilities.WcfIntegration
17 using System;
18 using System.ServiceModel;
19 using System.ServiceModel.Description;
20 using Castle.Core;
22 public abstract class AbstractServiceHostBuilder : IServiceHostBuilder, IWcfEndpointVisitor
24 private ServiceHost serviceHost;
25 private ServiceEndpoint serviceEndpoint;
27 #region IServiceHostBuilder Members
29 /// <summary>
30 /// Builds a new <see cref="ServiceHost"/> for the <see cref="ComponentModel"/>.
31 /// </summary>
32 /// <param name="model">The component model.</param>
33 /// <param name="serviceModel">The service model.</param>
34 /// <returns>The correpsonding service host.</returns>
35 public ServiceHost Build(ComponentModel model, WcfServiceModel serviceModel)
37 ServiceHost serviceHost = CreateServiceHost(model, serviceModel);
39 foreach (IWcfEndpoint endpoint in serviceModel.Endpoints)
41 AddServiceEndpoint(serviceHost, endpoint);
44 return serviceHost;
47 #endregion
49 protected abstract ServiceHost CreateServiceHost(ComponentModel model,
50 WcfServiceModel serviceModel);
52 protected virtual ServiceEndpoint AddServiceEndpoint(ServiceHost serviceHost,
53 IWcfEndpoint endpoint)
55 this.serviceHost = serviceHost;
56 endpoint.Accept(this);
57 return serviceEndpoint;
60 #region IWcfEndpointVisitor Members
62 void IWcfEndpointVisitor.VisitServiceEndpointModel(ServiceEndpointModel model)
64 serviceHost.Description.Endpoints.Add(model.ServiceEndpoint);
65 serviceEndpoint = model.ServiceEndpoint;
68 void IWcfEndpointVisitor.VisitConfigurationEndpointModel(ConfigurationEndpointModel model)
70 throw new InvalidOperationException("The ServiceEndpoint for a ServiceHost " +
71 "cannot be created from an endpoint name.");
74 void IWcfEndpointVisitor.VisitBindingEndpointModel(BindingEndpointModel model)
76 serviceEndpoint = serviceHost.AddServiceEndpoint(
77 model.Contract, model.Binding, string.Empty);
80 void IWcfEndpointVisitor.VisitBindingAddressEndpointModel(BindingAddressEndpointModel model)
82 if (model.HasViaAddress)
84 serviceEndpoint = serviceHost.AddServiceEndpoint(
85 model.Contract, model.Binding, model.Address, model.ViaAddress);
87 else
89 serviceEndpoint = serviceHost.AddServiceEndpoint(
90 model.Contract, model.Binding, model.Address);
94 #endregion