Пример #1
0
 private XmlObjectSerializer GetXmlSerializer(Type type)
 {
     if (FrameworkUtility.GetDeclarativeAttribute <DataContractAttribute>(type) != null)
     {
         return(new DataContractSerializer(type));
     }
     else
     {
         return(new NetDataContractSerializer());
     }
 }
Пример #2
0
        /// <summary>
        /// Returns an instance of a trace provider for the specified type. This requires that the type supplies its Guid which will be used
        /// for registering it with the ETW infrastructure.
        /// </summary>
        /// <param name="componentType">The type which must be decarated with a GuidAttribute</param>
        /// <returns>An instance of a trace provider implementing the IComponentTraceProvider interface</returns>
        public static IComponentTraceProvider Create(Type componentType)
        {
            GuidAttribute guidAttribute = FrameworkUtility.GetDeclarativeAttribute <GuidAttribute>(componentType);

            if (guidAttribute != default(GuidAttribute))
            {
                return(new ComponentTraceProvider(componentType.FullName, new Guid(guidAttribute.Value)));
            }
            else
            {
                throw new MissingMemberException(componentType.FullName, typeof(GuidAttribute).FullName);
            }
        }
Пример #3
0
        private static ServiceHost CreateServiceBusHost(string serviceNamespace, string servicePath, string issuerName, string issuerSecret, Binding binding, Type serviceType)
        {
            Guard.ArgumentNotNullOrEmptyString(serviceNamespace, "serviceNamespace");
            Guard.ArgumentNotNullOrEmptyString(servicePath, "servicePath");
            Guard.ArgumentNotNullOrEmptyString(issuerName, "issuerName");
            Guard.ArgumentNotNullOrEmptyString(issuerSecret, "issuerSecret");
            Guard.ArgumentNotNull(binding, "binding");

            var callToken = TraceManager.DebugComponent.TraceIn(serviceNamespace, servicePath, binding.Name);

            var address = ServiceBusEnvironment.CreateServiceUri(WellKnownProtocolScheme.ServiceBus, serviceNamespace, servicePath);

            var credentialsBehaviour = new TransportClientEndpointBehavior();

            credentialsBehaviour.CredentialType = TransportClientCredentialType.SharedSecret;
            credentialsBehaviour.Credentials.SharedSecret.IssuerName   = issuerName;
            credentialsBehaviour.Credentials.SharedSecret.IssuerSecret = issuerSecret;

            var endpoint = new ServiceEndpoint(ContractDescription.GetContract(GetServiceContract(serviceType)), binding, new EndpointAddress(address));

            endpoint.Behaviors.Add(credentialsBehaviour);

            // Apply default endpoint configuration.
            ServiceEndpointConfiguration.ConfigureDefaults(endpoint);

            ServiceBehaviorAttribute serviceBehaviorAttr = FrameworkUtility.GetDeclarativeAttribute <ServiceBehaviorAttribute>(serviceType);
            ServiceHost host = null;

            if (serviceBehaviorAttr != null && serviceBehaviorAttr.InstanceContextMode == InstanceContextMode.Single)
            {
                host = new ServiceHost(Activator.CreateInstance(serviceType));
            }
            else
            {
                host = new ServiceHost(serviceType);
            }

            host.Description.Endpoints.Add(endpoint);
#if DEBUG
            var debugBehavior = new ServiceDebugBehavior();
            debugBehavior.IncludeExceptionDetailInFaults = true;

            host.Description.Behaviors.Remove <ServiceDebugBehavior>();
            host.Description.Behaviors.Add(debugBehavior);
#endif
            TraceManager.DebugComponent.TraceOut(callToken, endpoint.Address.Uri);

            return(host);
        }
Пример #4
0
        /// <summary>
        /// Reconstructs an instance of a <see cref="TraceEventRecord"/> object from its XML representation generated by <see cref="System.Runtime.Serialization.DataContractSerializer"/>.
        /// </summary>
        /// <param name="reader">The XML reader supplying the serialized representation of a <see cref="TraceEventRecord"/> object.</param>
        /// <returns>The initialized instance of a <see cref="TraceEventRecord"/> object containing trace event details.</returns>
        public static TraceEventRecord Create(XmlReader reader)
        {
            Guard.ArgumentNotNull(reader, "reader");

            DataContractAttribute dataContractAttr = FrameworkUtility.GetDeclarativeAttribute <DataContractAttribute>(typeof(TraceEventRecord));

            if (dataContractAttr != null)
            {
                XElement eventRecordXml = XElement.Load(reader, LoadOptions.None);

                if (eventRecordXml.Name.LocalName == dataContractAttr.Name && eventRecordXml.Name.Namespace == dataContractAttr.Namespace)
                {
                    XElement childElement;

                    TraceEventRecord eventRecord = new TraceEventRecord
                    {
                        DateTime = (childElement = (from child in eventRecordXml.Descendants(XName.Get(DateTimePropertyName, dataContractAttr.Namespace)) select child).FirstOrDefault()) != null?DateTime.Parse(childElement.Value, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal) : default(DateTime),
                                       ProcessId = (childElement = (from child in eventRecordXml.Descendants(XName.Get(ProcessIdPropertyName, dataContractAttr.Namespace)) select child).FirstOrDefault()) != null?Int32.Parse(childElement.Value, CultureInfo.InvariantCulture) : 0,
                                                       ThreadId = (childElement = (from child in eventRecordXml.Descendants(XName.Get(ThreadIdPropertyName, dataContractAttr.Namespace)) select child).FirstOrDefault()) != null?Int32.Parse(childElement.Value, CultureInfo.InvariantCulture) : 0,
                                                                      MachineName                           = (childElement = (from child in eventRecordXml.Descendants(XName.Get(MachineNamePropertyName, dataContractAttr.Namespace)) select child).FirstOrDefault()) != null ? childElement.Value : null,
                                                                      ProcessName                           = (childElement = (from child in eventRecordXml.Descendants(XName.Get(ProcessNamePropertyName, dataContractAttr.Namespace)) select child).FirstOrDefault()) != null ? childElement.Value : null,
                                                                      Timestamp                             = (childElement = (from child in eventRecordXml.Descendants(XName.Get(TimestampPropertyName, dataContractAttr.Namespace)) select child).FirstOrDefault()) != null?Int64.Parse(childElement.Value, CultureInfo.InvariantCulture) : 0,
                                                                                                    EventId = (childElement = (from child in eventRecordXml.Descendants(XName.Get(EventIdPropertyName, dataContractAttr.Namespace)) select child).FirstOrDefault()) != null?Int32.Parse(childElement.Value, CultureInfo.InvariantCulture) : 0,
                                                                                                                  EventSource   = (childElement = (from child in eventRecordXml.Descendants(XName.Get(EventSourcePropertyName, dataContractAttr.Namespace)) select child).FirstOrDefault()) != null ? childElement.Value : null,
                                                                                                                  EventSourceId = (childElement = (from child in eventRecordXml.Descendants(XName.Get(EventSourceIdPropertyName, dataContractAttr.Namespace)) select child).FirstOrDefault()) != null?Guid.Parse(childElement.Value) : Guid.Empty,
                                                                                                                                      EventType = (childElement = (from child in eventRecordXml.Descendants(XName.Get(EventTypePropertyName, dataContractAttr.Namespace)) select child).FirstOrDefault()) != null ? (TraceEventType)Enum.Parse(typeof(TraceEventType), childElement.Value, true) : default(TraceEventType),
                                                                                                                                      Message   = (childElement = (from child in eventRecordXml.Descendants(XName.Get(MessagePropertyName, dataContractAttr.Namespace)) select child).FirstOrDefault()) != null ? childElement.Value : null
                    };

                    return(eventRecord);
                }
                else
                {
                    throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, ExceptionMessages.CannotCreateInstanceFromXmlReader, typeof(TraceEventRecord).Name, dataContractAttr.Namespace, eventRecordXml.Name.LocalName, eventRecordXml.Name.Namespace), "reader");
                }
            }
            else
            {
                throw new ArgumentNullException(typeof(DataContractAttribute).FullName);
            }
        }
Пример #5
0
        /// <summary>
        /// Adds a new trace listener associated with an unique name and a type containing the implementation.
        /// </summary>
        /// <param name="name">The unique name under which a new trace listener will be added to the collection.</param>
        /// <param name="listenerType">The type implementing the new trace listener.</param>
        public void AddTraceListener(string name, Type listenerType)
        {
            Guard.ArgumentNotNullOrEmptyString(name, "name");
            Guard.ArgumentNotNull(listenerType, "listenerType");

            ConfigurationElementTypeAttribute configElementTypeAttr = FrameworkUtility.GetDeclarativeAttribute <ConfigurationElementTypeAttribute>(listenerType);

            if (configElementTypeAttr != null)
            {
                TraceListenerData listenerData = Activator.CreateInstance(configElementTypeAttr.ConfigurationType) as TraceListenerData;

                if (listenerData != null)
                {
                    listenerData.ListenerDataType = configElementTypeAttr.ConfigurationType;
                    listenerData.Name             = name;
                    listenerData.Type             = listenerType;

                    this.loggingSettings.TraceListeners.Add(listenerData);
                }
            }
        }
Пример #6
0
        private static Type GetServiceContract(Type serviceType)
        {
            Guard.ArgumentNotNull(serviceType, "serviceType");

            Type[] serviceInterfaces = serviceType.GetInterfaces();

            if (serviceInterfaces != null && serviceInterfaces.Length > 0)
            {
                foreach (Type serviceInterface in serviceInterfaces)
                {
                    ServiceContractAttribute serviceContractAttr = FrameworkUtility.GetDeclarativeAttribute <ServiceContractAttribute>(serviceInterface);

                    if (serviceContractAttr != null)
                    {
                        return(serviceInterface);
                    }
                }
            }

            return(null);
        }