Line data Source code
1 : using System;
2 : using System.Runtime.Serialization;
3 : using System.ServiceModel;
4 : using System.ServiceModel.Description;
5 :
6 : namespace Cqrs.Services
7 : {
8 : public class ServiceChannelFactory<TService> : ChannelFactory<TService>
9 0 : {
10 : /// <summary>
11 : /// Instanciates a new instance of the <see cref="ServiceChannelFactory{TService}"/> class with a specified endpoint configuration name.
12 : /// </summary>
13 : /// <param name="endpointConfigurationName">The configuration name used for the endpoint.</param>
14 1 : public ServiceChannelFactory(string endpointConfigurationName)
15 : : base(endpointConfigurationName)
16 : {
17 : RegisterDataContracts();
18 : AttachDataContractResolver(Endpoint);
19 : }
20 :
21 0 : protected virtual void AttachDataContractResolver(ServiceEndpoint endpoint)
22 : {
23 : ContractDescription contractDescription = endpoint.Contract;
24 :
25 : foreach (OperationDescription operationDescription in contractDescription.Operations)
26 : {
27 : Type dataContractType = WcfDataContractResolverConfiguration.Current.GetDataContracts<TService>(operationDescription.Name);
28 : if (dataContractType == null)
29 : continue;
30 : DataContractSerializerOperationBehavior serializerBehavior = operationDescription.Behaviors.Find<DataContractSerializerOperationBehavior>();
31 : if (serializerBehavior == null)
32 : operationDescription.Behaviors.Add(serializerBehavior = new DataContractSerializerOperationBehavior(operationDescription));
33 : serializerBehavior.DataContractResolver = (DataContractResolver)Activator.CreateInstance(AppDomain.CurrentDomain, dataContractType.Assembly.FullName, dataContractType.FullName).Unwrap();
34 : }
35 : }
36 :
37 0 : protected virtual void RegisterDataContracts() { }
38 : }
39 : }
|