Click or drag to resize

C# .Net Code Sample

Sample code for C# .NET Development

Jcdi Web Service 2021 Example .NetCore and .Net5

CustomHeader class

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

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>
 5private bool Login(string username, string password)
 6{
 7    try
 8    {
 9        JcdiClient jcdiClient = new JcdiClient();
10        AuthToken v = jcdiClient.LoginAsync(username, password);
11        token = v.AccessToken;
12    }
13    catch (FaultException<AuthOpFault>)
14    {
15        token = string.Empty;
16        return false;
17    }
18    catch (Exception ex)
19    {
20        token = string.Empty;
21        return false;
22    }
23}

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    jsdcDocument.FileName = Path.GetFileName(fileName);
11    jsdcDocument.FileImage = File.ReadAllBytes(fileName);
12    jsdcDocument.XmlDocumentMetadata = File.ReadAllText(metadataFileName);
13    jsdcDocument.XmlFlowMetadata = File.ReadAllText(flowFileName);
14
15    JcdiClient jcdiClient = new JcdiClient();
16    using (new OperationContextScope(jcdiClient.InnerChannel))
17    {
18        //Impostazione del token nell'header della chiamata
19        MessageHeader<CustomHeader> typedHeader = new MessageHeader<CustomHeader>(new CustomHeader { AccessToken = token });
20        MessageHeader untypedHeader = typedHeader.GetUntypedHeader("custom-header", "s");
21        OperationContext.Current.OutgoingMessageHeaders.Add(untypedHeader);
22
23        return jcdiClient.Deposit(jsdcDocument);
24    }
25}
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    using (new OperationContextScope(jcdiClient.InnerChannel))
11    {
12        //Impostazione del token nell'header della chiamata
13        MessageHeader<CustomHeader> typedHeader = new MessageHeader<CustomHeader>(new CustomHeader { AccessToken = token });
14        MessageHeader untypedHeader = typedHeader.GetUntypedHeader("custom-header", "s");
15        OperationContext.Current.OutgoingMessageHeaders.Add(untypedHeader);
16
17        return jcdiClient.ReadDocumentTechMetadata(documentUuid, digitalSafeId, null);
18    }
19}
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 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}
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    using (new OperationContextScope(client.InnerChannel))
10    {
11        //Impostazione del token nell'header della chiamata
12        MessageHeader<CustomHeader> typedHeader = new MessageHeader<CustomHeader>(new CustomHeader { AccessToken = token });
13        MessageHeader untypedHeader = typedHeader.GetUntypedHeader("custom-header", "s");
14        OperationContext.Current.OutgoingMessageHeaders.Add(untypedHeader);
15
16        return pdvStatus = jcdiClient.GetPdvStatus(id, new SdcRequestInfo() { DigitalSafeUuid = "", ContainerId = null });
17    }
18}

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}

That's all folks