Click or drag to resize

C# Framework Code Sample

Sample code for C# .NET Development

Jcdi Web Service 2021 Example .Net Framework 4.8: console application

CustomHeader class with namespace Enerj.CDI

C#
1namespace Enerj.CDI
2{
3    [DataContract]
4    public class CustomHeader
5    {
6        [DataMember]
7        public string AccessToken { get; set; }
8    }
9}

Console application

C#
  1namespace TestToken
  2{
  3    public class Program
  4    {
  5        static void Main(string[] args)
  6        {
  7            try
  8            {
  9                //LOGIN
 10                CDIClient.JcdiClient cdi = new CDIClient.JcdiClient();
 11                CDIClient.AuthToken token = cdi.Login(#user#, #password#);
 12
 13                //REFRESH TOKEN
 14                ClientCustomHeaderContext.HeaderInformation.AccessToken = token.AccessToken;
 15                CDIClient.AuthToken token2 = cdi.AuthRefresh(token.AccessToken);
 16
 17                //DEPOSIT
 18                ClientCustomHeaderContext.HeaderInformation.AccessToken = token.AccessToken;
 19                CDIClient.JsdcDocument jsdcDocument = new CDIClient.JsdcDocument();
 20
 21                jsdcDocument.FileName = Path.GetFileName(@".\Example\testset_01_001.pdf");
 22                jsdcDocument.FileImage = File.ReadAllBytes(@".\Example\testset_01_001.pdf");
 23                jsdcDocument.XmlDocumentMetadata = File.ReadAllText(@".\Example\metadata_testset_01_001.pdf.xml");
 24                jsdcDocument.XmlFlowMetadata = File.ReadAllText(@".\Example\jsdcflow_testset_01_001.pdf.xml");
 25
 26                CDIClient.DepositResult f = cdi.Deposit(jsdcDocument);
 27                if (f.DocumentUuid != "")
 28                {
 29                    Console.WriteLine("DEPOSIT: "+ f.DocumentUuid);
 30                    Console.WriteLine("Press any key to continue...");
 31                    Console.ReadLine();
 32                }
 33                else
 34                {
 35                    Console.WriteLine("Empty DocumentUuid");
 36                    Console.WriteLine("Press any key to continue...");
 37                    Console.ReadLine();
 38                }
 39            }
 40            catch (Exception ex)
 41            {
 42                Console.WriteLine(ex.ToString());
 43                Console.WriteLine("Press any key to continue...");
 44                Console.ReadLine();
 45            }
 46        }
 47    }
 48
 49    public static class ClientCustomHeaderContext
 50    {
 51        public static Enerj.CDI.CustomHeader HeaderInformation;
 52
 53        static ClientCustomHeaderContext()
 54        {
 55            HeaderInformation = new Enerj.CDI.CustomHeader();
 56        }
 57    }
 58
 59    public class CustomInspectorBehaviorExtension : BehaviorExtensionElement
 60    {
 61        protected override object CreateBehavior()
 62        {
 63            return new CustomInspectorBehavior();
 64        }
 65
 66        public override Type BehaviorType
 67        {
 68            get { return typeof(CustomInspectorBehavior); }
 69        }
 70    }
 71
 72    [AttributeUsage(AttributeTargets.Class)]
 73    public class CustomInspectorBehavior : Attribute, IDispatchMessageInspector,
 74        IClientMessageInspector, IEndpointBehavior, IServiceBehavior
 75    {
 76        #region IDispatchMessageInspector
 77
 78        public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
 79        {
 80            if (!request.Headers.Action.EndsWith("Login"))
 81            {
 82                //Retrieve Inbound Object from Request
 83                var header = request.Headers.GetHeader<Enerj.CDI.CustomHeader>("custom-header", "s");
 84                if (header != null)
 85                    OperationContext.Current.IncomingMessageProperties.Add("CustomHeader", header);
 86            }
 87            return null;
 88        }
 89
 90        public void BeforeSendReply(ref Message reply, object correlationState)
 91        {
 92            //No need to do anything else
 93        }
 94
 95        #endregion
 96
 97        #region IClientMessageInspector
 98
 99        public object BeforeSendRequest(ref Message request, IClientChannel channel)
100        {
101            //Instantiate new HeaderObject with values from ClientContext;
102            var dataToSend = new Enerj.CDI.CustomHeader
103            {
104                AccessToken = ClientCustomHeaderContext.HeaderInformation.AccessToken
105            };
106
107            var typedHeader = new MessageHeader<Enerj.CDI.CustomHeader>(dataToSend);
108            var untypedHeader = typedHeader.GetUntypedHeader("custom-header", "s");
109
110            request.Headers.Add(untypedHeader);
111            return null;
112        }
113
114        public void AfterReceiveReply(ref Message reply, object correlationState)
115        {
116            //No need to do anything else
117        }
118        #endregion
119
120        #region IEndpointBehavior
121
122        public void Validate(ServiceEndpoint endpoint)
123        {
124        }
125
126        public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
127        {
128        }
129
130        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
131        {
132            var channelDispatcher = endpointDispatcher.ChannelDispatcher;
133            if (channelDispatcher == null) return;
134            foreach (var ed in channelDispatcher.Endpoints)
135            {
136                var inspector = new CustomInspectorBehavior();
137                ed.DispatchRuntime.MessageInspectors.Add(inspector);
138            }
139        }
140
141        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
142        {
143            var inspector = new CustomInspectorBehavior();
144            clientRuntime.MessageInspectors.Add(inspector);
145        }
146
147        #endregion
148
149        #region IServiceBehaviour
150
151        public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
152        {
153        }
154
155        public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
156        {
157        }
158
159        public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
160        {
161            foreach (ChannelDispatcher cDispatcher in serviceHostBase.ChannelDispatchers)
162            {
163                foreach (var eDispatcher in cDispatcher.Endpoints)
164                {
165                    eDispatcher.DispatchRuntime.MessageInspectors.Add(new CustomInspectorBehavior());
166                }
167            }
168        }
169        #endregion
170    }
171}
Jcdi Web Service 2021 Example with Enerj.CDI.TokenHeader.dll

Authentication Token

C#
1/// <summary>
2/// Authentication Token
3/// </summary>
4public static string token { get; set; }

Login to SDC system

C#
 1/// <summary>
 2/// Login example
 3/// </summary>
 4/// <returns>true if logged in</returns>
 5public bool Login()
 6{
 7    bool retVal = true;
 8    try
 9    {
10        JcdiClient jcdiClient = new JcdiClient();
11        AuthToken v = jcdiClient.Login(user, password);
12        token = v.AccessToken;
13    }
14    catch (FaultException<AuthOpFault> exc)
15    {
16        token = string.Empty;
17        return false;
18    }
19    catch (Exception exc)
20    {
21        token = string.Empty;
22        return false;
23    }
24    return retVal;
25}

Send document to SDC system

C#
 1/// <summary>
 2/// Send the file to the SDC system
 3/// </summary>
 4/// <param name="fileName">File Name</param>
 5/// <param name="metadataFileName">Metadata File Name</param>
 6/// <returns>Document object: UUID, Digital Safe, Container</returns>
 7public DocumentTicket Deposit(string fileName, string metadataFileName, string flowFileName)
 8{
 9    JsdcDocument jsdcDocument = new JsdcDocument();
10
11    jsdcDocument.FileName = Path.GetFileName(fileName);
12    jsdcDocument.FileImage = File.ReadAllBytes(fileName);
13    jsdcDocument.XmlDocumentMetadata = File.ReadAllText(metadataFileName);
14    jsdcDocument.XmlFlowMetadata = File.ReadAllText(flowFileName);
15
16    JcdiClient jcdiClient = new JcdiClient();
17    ClientCustomHeaderContext.HeaderInformation.AccessToken = token;
18    return jcdiClient.Deposit(jsdcDocument);
19}
C#
 1/// <summary>
 2/// Send the file to the SDC system
 3/// </summary>
 4/// <param name="fileName">File Name</param>
 5/// <param name="metadataFileName">Metadata File Name</param>
 6/// <returns>Document object: UUID, Digital Safe, Container</returns>
 7public DocumentTicket Deposit(string fileName, string metadataFileName, string flowFileName)
 8{
 9    if (Login())
10    {
11        try
12        {
13            return Deposit(fileName, metadataFileName, flowFileName);
14        }
15        catch (Exception exc)
16        {
17            Logout();
18            throw exc;
19        }
20    }
21    else
22    {
23        throw new Exception("Login failed");
24    }
25}

Check document status

C#
 1/// <summary>
 2/// Get Document
 3/// </summary>
 4/// <param name="documentUuid">UUID document</param>
 5/// <param name="digitalSafeId">Digital Safe</param>
 6/// <returns>Document info: status, pdv, arrivaldate </returns>
 7private JsdcTechMetadata ReadDocumentTechMetadata(string documentUuid, string digitalSafeId)
 8{
 9    JcdiClient jcdiClient = new JcdiClient();
10    ClientCustomHeaderContext.HeaderInformation.AccessToken = token;
11    return jcdiClient.ReadDocumentTechMetadata(documentUuid, digitalSafeId, null);
12}
C#
 1/// <summary>
 2/// Get Document
 3/// </summary>
 4/// <param name="documentUuid">UUID document</param>
 5/// <param name="digitalSafeId">Digital Safe</param>
 6/// <param name="containerId">Container ID</param>
 7/// <returns>Document info: status, pdv, arrivaldate </returns>
 8public JsdcTechMetadata ReadDocumentTechMetadata(string documentUuid, string digitalSafeUuid, long? containerId)
 9{
10    if (Login())
11    {
12        try
13        {
14            return ReadDocumentTechMetadata(documentUuid, digitalSafeUuid, containerId);
15        }
16        catch (Exception exc)
17        {
18            Logout();
19            throw exc;
20        }
21    }
22    else
23    {
24        throw new Exception("Login failed");
25    }
26}

Check PdV Status

C#
 1/// <summary>
 2/// Get Pdv Status
 3/// </summary>
 4/// <param name="id">PdV id</param>
 5/// <returns>return PdV Status</returns>
 6public PdvStatus GetPdvStatus(long id)
 7{
 8    JcdiClient jcdiClient = new JcdiClient();
 9    ClientCustomHeaderContext.HeaderInformation.AccessToken = token;
10    return pdvStatus = jcdiClient.GetPdvStatus(id, new SdcRequestInfo() { DigitalSafeUuid = "", ContainerId = null });
11}
C#
 1/// <summary>
 2/// Get Pdv Status
 3/// </summary>
 4/// <param name="id">PdV id</param>
 5/// <returns>return PdV Status</returns>
 6public PdvStatus GetPdVStatus(long pdvId)
 7{
 8    if (Login())
 9    {
10        try
11        {
12            return GetPdVStatus(pdvId);
13        }
14        catch (Exception)
15        {
16            Logout();
17            throw;
18        }
19    }
20    else
21    {
22        throw new Exception("Login failed");
23    }
24}

Logout to SDC system

C#
1/// <summary>
2/// Apply the logout invalidating the token
3/// </summary>
4public void Logout()
5{
6    token = string.Empty;
7}
Download C# sample project

That's all folks