Calling uc4ws via .NET

Discussion created by Jim_McCracken_387 on May 20, 2014
Latest reply on May 20, 2014 by Jennifer_Jinhong_34

This is assuming you are using WCF to create a Service Reference to your Automic web service... and that you named the reference "uc4ws"... once you've done that, you're done in most .NET service references but not so for Automic!  Your code needs to track your session id and set some special HTTP headers to operate properly... the following helper class will assist you with that.  Because it uses IDisposable you won't need to track usage or explicitly logoff (though you should wrap your code in a using() {} block to ensure you don't wait for your program to finish before logging off)

    public class UC4Session : IDisposable
        public string _sessionid;
        private System.ServiceModel.OperationContextScope myContext;
        public uc4ws.uc4PortTypeClient uc4ws;
        public UC4Session(int client, string Username, string Department, string Password)
            uc4ws = new uc4ws.uc4PortTypeClient();
            _sessionid = uc4ws.logon(client, Username, Department, Password, (sbyte)'E');
            myContext = new System.ServiceModel.OperationContextScope((System.ServiceModel.IClientChannel)uc4ws.InnerChannel);
            System.ServiceModel.Web.WebOperationContext.Current.OutgoingRequest.Headers.Add("id", _sessionid);
        void IDisposable.Dispose()

Use it as follows:

using(UC4Session mySession = new UC4Session(clientNumber, userName, Department, Password)) {
                int i = mySession.uc4ws.executeObject(jobName, "EASTERN", new uc4ws.InputParameter[] {}, DateTime.Now, false);