Line data Source code
1 : using System;
2 : using System.Runtime.Serialization;
3 : using System.ServiceModel.Description;
4 : using Cqrs.Services;
5 : using Ninject.Extensions.Wcf;
6 :
7 : namespace Cqrs.Ninject.ServiceHost
8 : {
9 : public class NinjectWcfServiceHostFactory<TServiceType> : NinjectServiceHostFactory
10 0 : {
11 0 : protected override System.ServiceModel.ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
12 : {
13 : System.ServiceModel.ServiceHost host = base.CreateServiceHost(serviceType, baseAddresses);
14 :
15 : foreach (ServiceEndpoint serviceEndpoint in host.Description.Endpoints)
16 : {
17 : foreach (OperationDescription operationDescription in serviceEndpoint.Contract.Operations)
18 : {
19 : Type dataContractType = WcfDataContractResolverConfiguration.Current.GetDataContracts<TServiceType>(operationDescription.Name);
20 : if (dataContractType == null)
21 : continue;
22 : DataContractSerializerOperationBehavior serializerBehavior = operationDescription.Behaviors.Find<DataContractSerializerOperationBehavior>();
23 : if (serializerBehavior == null)
24 : operationDescription.Behaviors.Add(serializerBehavior = new DataContractSerializerOperationBehavior(operationDescription));
25 : serializerBehavior.DataContractResolver = (DataContractResolver)Activator.CreateInstance(AppDomain.CurrentDomain, dataContractType.Assembly.FullName, dataContractType.FullName).Unwrap();
26 : }
27 : }
28 :
29 : return host;
30 : }
31 : }
32 : }
|