Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Static Code Analysis

, 15 Mar 2010
A static code analyzer building method call networks + sample applications.
CodeAnalyzerAndToDoSample.zip
Generator
bin
.noclean
Arebis.CodeAnalysis.Static.dll
Arebis.CodeGeneration.dll
Arebis.CodeGeneration.VisualStudio.dll
Arebis.CodeGenerator.dll
Arebis.Common.dll
CGen.exe
Content.cst
Content.cst.gen
COperation.cst
COperation.cst.gen
files
GeneratorMain.cst
GeneratorMain.cst.gen
Index.cst
Index.cst.gen
IndexTop.cst
IndexTop.cst.gen
SOperation.cst
SOperation.cst.gen
UIOperation.cst
UIOperation.cst.gen
Output
SampleApp
LocalTestRun.testrunconfig
ToDoApplication
Infrastructure
Properties
Service References
ToDoContract
Properties
ToDoSample.vsmdi
ToDoService
Entities.edmx
Infrastructure
Properties
ToDoSample.Service.csproj.user
ToDoSampleDb.mdf
ToDoSampleDb_log.ldf
ToDoUnitTests
Properties
Testing
ToDoSample.UnitTests.csproj.user
UnitTests
StaticCodeAnalyzer
Arebis.CodeAnalysis.Static
Arebis.CodeAnalysis.Static.csproj.user
CodeModel.cd
Processors
Rules
Properties
Arebis.Common
Arebis
Collections
Generic
Reflection
Properties
System
Collections
Generic
Net2
Threading
Net2
using System;
using System.Collections.Generic;
using System.ServiceModel;

namespace ToDoSample.Application.Infrastructure
{
    /// <summary>
    /// A common, generic WCF service agent.
    /// </summary>
    public class ServiceAgent<T> : IDisposable
        where T : class
    {
        private static Dictionary<string, ChannelFactory<T>> factoryCache = new Dictionary<string, ChannelFactory<T>>();

        private T channel;

        /// <summary>
        /// Initializes a new ServiceAgent instance.
        /// </summary>
        public ServiceAgent(string endpointConfigurationName)
            : this(GetChannelFactory(endpointConfigurationName))
        { }

        /// <summary>
        /// Initializes a new ServiceAgent instance.
        /// </summary>
        protected internal ServiceAgent(ChannelFactory<T> channelFactory)
        {
            this.channel = channelFactory.CreateChannel();
        }

        /// <summary>
        /// Gets a channel factory for the given endpointConfiguration.
        /// Channel factories made by endpointConfiguration are cached.
        /// </summary>
        internal static ChannelFactory<T> GetChannelFactory(string endpointConfigurationName)
        {
            lock (factoryCache)
            {

                ChannelFactory<T> result;
                if (!factoryCache.TryGetValue(endpointConfigurationName, out result))
                {
                    result
                        = factoryCache[endpointConfigurationName]
                        = new ChannelFactory<T>(endpointConfigurationName);
                }

                return result;
            }
        }

        /// <summary>
        /// The service agent invoker.
        /// </summary>
        public virtual T Invoke
        {
            get { return this.channel; }
        }

        /// <summary>
        /// Communication state of the channel.
        /// </summary>
        public CommunicationState ChannelState
        {
            get { return ((IClientChannel)this.channel).State; }
        }

        /// <summary>
        /// Disposes the service agent and closes the WCF channel.
        /// </summary>
        public virtual void Dispose()
        {
            if (this.channel != null)
            {
                try
                {
                    if (this.ChannelState == CommunicationState.Faulted)
                        ((IClientChannel)this.channel).Abort();
                    else
                        ((IClientChannel)this.channel).Close();
                }
                finally
                {
                    this.channel = null;
                }
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Rudi Breedenraedt
Architect Wolters Kluwer Belgium
Belgium Belgium
Rudi is a Software Architect at Wolters Kluwer Belgium.

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 15 Mar 2010
Article Copyright 2010 by Rudi Breedenraedt
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid