Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#if CoreWCF
#if CoreWCF
namespace CoreWCF.Description
#else
namespace System.ServiceModel.Description
#endif
{
using System;
using System.Linq;
using System.Reflection;

internal static class OperationDescriptionExtensions
Expand All @@ -18,7 +19,17 @@ public static Type GetReturnType(this OperationDescription operationDescription)

Type outputType = null;
#if NET45_OR_GREATER || NETCOREAPP
outputType = operationDescription.TaskMethod?.ReturnType.GetGenericArguments()[0];
// outputType = operationDescription.TaskMethod?.ReturnType.GetGenericArguments()[0];
if (operationDescription.TaskMethod != null &&
operationDescription.TaskMethod.ReturnType.GetGenericArguments().Any())
{
outputType = operationDescription.TaskMethod?.ReturnType.GetGenericArguments()[0];
}
else
{
outputType = operationDescription.TaskMethod?.ReturnType;
}

#endif
#if NET45_OR_GREATER
if (outputType == null) {
Expand Down Expand Up @@ -115,4 +126,4 @@ public static T GetOperationContract<T>(this OperationDescription operationDescr
return operationContract;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// <copyright file="HL7QueryByParameterPayloadSerializerDefaults.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
Expand Down Expand Up @@ -36,14 +36,18 @@ internal static XmlObjectSerializer CreateSerializer(Type serializerType, Type t
{
if (serializerType == typeof(XElementObjectSerializer))
{
return new XElementObjectSerializer(HL7Constants.Elements.QueryByParameterPayload, HL7Constants.Namespace);
return HL7SerializerCache.GetXElementObjectSerializer(
rootName: HL7Constants.Elements.QueryByParameterPayload,
rootNamespace: HL7Constants.Namespace);
}

// if (serializerType == typeof(QuerySerializer))
// {
// return new QuerySerializer();
// }
return (XmlObjectSerializer)Activator.CreateInstance(serializerType, type, HL7Constants.Elements.QueryByParameterPayload, HL7Constants.Namespace);
return HL7SerializerCache.GetXmlObjectSerializer<XmlObjectSerializer>(
type: type,
serializerType: serializerType,
rootName: HL7Constants.Elements.QueryByParameterPayload,
rootNamespace: HL7Constants.Namespace,
serializerFactory: (_type, _serializerType, _rootName, _rootNamespace) =>
(XmlObjectSerializer)Activator.CreateInstance(_serializerType, _type, _rootName, _rootNamespace) );
}

/// <summary>
Expand All @@ -56,15 +60,23 @@ internal static XmlObjectSerializer CreateSerializer(Type type)
// Generated by XSD tools
if (type.GetCustomAttributes(typeof(XmlTypeAttribute), true).Length > 0)
{
return new XmlSerializerObjectSerializer(type, HL7Constants.Elements.QueryByParameterPayload, HL7Constants.Namespace);
return HL7SerializerCache.GetXmlSerializerObjectSerializer(
type: type,
rootName: HL7Constants.Elements.QueryByParameterPayload,
rootNamespace: HL7Constants.Namespace);
}

if (type == typeof(XElement))
{
return new XElementObjectSerializer(HL7Constants.Elements.QueryByParameterPayload, HL7Constants.Namespace);
return HL7SerializerCache.GetXElementObjectSerializer(
rootName: HL7Constants.Elements.QueryByParameterPayload,
rootNamespace: HL7Constants.Namespace);
}

return new DataContractSerializer(type, HL7Constants.Elements.QueryByParameterPayload, HL7Constants.Namespace);
return HL7SerializerCache.GetDataContractSerializer(
type: type,
rootName: HL7Constants.Elements.QueryByParameterPayload,
rootNamespace: HL7Constants.Namespace);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// <copyright file="HL7QueryContinuationPayloadSerializerDefaults.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
Expand Down Expand Up @@ -36,14 +36,18 @@ internal static XmlObjectSerializer CreateSerializer(Type serializerType, Type t
{
if (serializerType == typeof(XElementObjectSerializer))
{
return new XElementObjectSerializer(HL7Constants.Elements.QueryContinuation, HL7Constants.Namespace);
return HL7SerializerCache.GetXElementObjectSerializer(
rootName: HL7Constants.Elements.QueryContinuation,
rootNamespace: HL7Constants.Namespace);
}

// if (serializerType == typeof(QuerySerializer))
// {
// return new QuerySerializer();
// }
return (XmlObjectSerializer)Activator.CreateInstance(serializerType, type, HL7Constants.Elements.QueryContinuation, HL7Constants.Namespace);
return HL7SerializerCache.GetXmlObjectSerializer<XmlObjectSerializer>(
type: type,
serializerType: serializerType,
rootName: HL7Constants.Elements.QueryContinuation,
rootNamespace: HL7Constants.Namespace,
serializerFactory: (_type, _serializerType, _rootName, _rootNamespace) =>
(XmlObjectSerializer)Activator.CreateInstance(_serializerType, _type, _rootName, _rootNamespace));
}

/// <summary>
Expand All @@ -56,15 +60,23 @@ internal static XmlObjectSerializer CreateSerializer(Type type)
// Generated by XSD tools
if (type.GetCustomAttributes(typeof(XmlTypeAttribute), true).Length > 0)
{
return new XmlSerializerObjectSerializer(type, HL7Constants.Elements.QueryContinuation, HL7Constants.Namespace);
return HL7SerializerCache.GetXmlSerializerObjectSerializer(
type: type,
rootName: HL7Constants.Elements.QueryContinuation,
rootNamespace: HL7Constants.Namespace);
}

if (type == typeof(XElement))
{
return new XElementObjectSerializer(HL7Constants.Elements.QueryContinuation, HL7Constants.Namespace);
return HL7SerializerCache.GetXElementObjectSerializer(
rootName: HL7Constants.Elements.QueryContinuation,
rootNamespace: HL7Constants.Namespace);
}

return new DataContractSerializer(type, HL7Constants.Elements.QueryContinuation, HL7Constants.Namespace);
return HL7SerializerCache.GetDataContractSerializer(
type: type,
rootName: HL7Constants.Elements.QueryContinuation,
rootNamespace: HL7Constants.Namespace);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
namespace Abc.ServiceModel.Protocol.HL7
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Security;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Serialization;

internal static class HL7SerializerCache
{
private static ReaderWriterLockSlim _lock = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion);
private static Dictionary<string, XmlObjectSerializer> _xmlSerializers = new Dictionary<string, XmlObjectSerializer>(StringComparer.OrdinalIgnoreCase);

internal static DataContractSerializer GetDataContractSerializer(Type type, string rootName, string rootNamespace) =>
GetXmlObjectSerializer<DataContractSerializer>(
type: type,
serializerType: null,
rootName: rootName,
rootNamespace: rootNamespace,
serializerFactory: (_type, _serializerType, _rootName, _rootNamespace) =>
new DataContractSerializer(
type: _type,
rootName: _rootName,
rootNamespace: _rootNamespace));

internal static XElementObjectSerializer GetXElementObjectSerializer(string rootName, string rootNamespace) =>
GetXmlObjectSerializer<XElementObjectSerializer>(
type: typeof(XElementObjectSerializer),
serializerType: null,
rootName: rootName,
rootNamespace: rootNamespace,
serializerFactory: (_type, _serializerType, _rootName, _rootNamespace) =>
new XElementObjectSerializer(
rootName: _rootName,
rootNamespace: _rootNamespace));


internal static XmlSerializerObjectSerializer GetXmlSerializerObjectSerializer(Type type, string rootName, string rootNamespace) =>
GetXmlObjectSerializer<XmlSerializerObjectSerializer>(
type: type,
serializerType: null,
rootName: rootName,
rootNamespace: rootNamespace,
serializerFactory: (_type, _serializerType, _rootName, _rootNamespace) =>
new XmlSerializerObjectSerializer(
type: _type,
rootName: _rootName,
rootNamespace: _rootNamespace));

internal static T GetXmlObjectSerializer<T>(Type type, Type serializerType, string rootName, string rootNamespace, Func<Type, Type, string, string, T> serializerFactory)
where T : XmlObjectSerializer
{
XmlObjectSerializer xmlObjectSerializer = null;

string key = typeof(T).Name + type?.FullName + ":" + rootNamespace + ":" + rootName + ":True:";
_lock.EnterUpgradeableReadLock();
try
{
if (!_xmlSerializers.TryGetValue(key, out xmlObjectSerializer))
{
_lock.EnterWriteLock();
try
{
if (!_xmlSerializers.TryGetValue(key, out xmlObjectSerializer))
{
if (xmlObjectSerializer == null)
{
xmlObjectSerializer = serializerFactory(type, serializerType, rootName, rootNamespace);
}

_xmlSerializers.Add(key, xmlObjectSerializer);
}
}
finally
{
_lock.ExitWriteLock();
}
}
}
finally
{
_lock.ExitUpgradeableReadLock();
}

return xmlObjectSerializer as T;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Abc.ServiceModel.Protocol.HL7
namespace Abc.ServiceModel.Protocol.HL7
{
using System;
using System.Diagnostics.Contracts;
Expand Down Expand Up @@ -31,14 +31,18 @@ internal static XmlObjectSerializer CreateSerializer(Type serializerType, Type t
{
if (serializerType == typeof(XElementObjectSerializer))
{
return new XElementObjectSerializer(HL7Constants.Elements.Subject, HL7Constants.Namespace);
return HL7SerializerCache.GetXElementObjectSerializer(
rootName: HL7Constants.Elements.Subject,
rootNamespace: HL7Constants.Namespace);
}

// if (serializerType == typeof(QuerySerializer))
// {
// return new QuerySerializer();
// }
return (XmlObjectSerializer)Activator.CreateInstance(serializerType, type, HL7Constants.Elements.Subject, HL7Constants.Namespace);
return HL7SerializerCache.GetXmlObjectSerializer<XmlObjectSerializer>(
type: type,
serializerType: serializerType,
rootName: HL7Constants.Elements.Subject,
rootNamespace: HL7Constants.Namespace,
serializerFactory: (_type, _serializerType, _rootName, _rootNamespace) =>
(XmlObjectSerializer)Activator.CreateInstance(_serializerType, _type, _rootName, _rootNamespace));
}

/// <summary>
Expand All @@ -48,20 +52,28 @@ internal static XmlObjectSerializer CreateSerializer(Type serializerType, Type t
/// <returns>The HL7 Subject serializer.</returns>
internal static XmlObjectSerializer CreateSerializer(Type type)
{
if (type == null) { throw new ArgumentNullException("type", "type != null"); }
if (type == null) { throw new ArgumentNullException("type", "type != null"); }

// Generated by XSD tools
if (type.GetCustomAttributes(typeof(XmlTypeAttribute), true).Length > 0)
{
return new XmlSerializerObjectSerializer(type, HL7Constants.Elements.Subject, HL7Constants.Namespace);
return HL7SerializerCache.GetXmlSerializerObjectSerializer(
type: type,
rootName: HL7Constants.Elements.Subject,
rootNamespace: HL7Constants.Namespace);
}

if (type == typeof(XElement))
{
return new XElementObjectSerializer(HL7Constants.Elements.Subject, HL7Constants.Namespace);
return HL7SerializerCache.GetXElementObjectSerializer(
rootName: HL7Constants.Elements.Subject,
rootNamespace: HL7Constants.Namespace);
}

return new DataContractSerializer(type, HL7Constants.Elements.Subject, HL7Constants.Namespace);
return HL7SerializerCache.GetDataContractSerializer(
type: type,
rootName: HL7Constants.Elements.Subject,
rootNamespace: HL7Constants.Namespace);
}
}
}
}
Loading