8 Replies Latest reply on Jun 7, 2016 6:15 PM by pmmcann

    Module DB PI Unit Search COM object trouble


      I am writing a .NET Web API for mobile devices to communicate with our PI and SQL systems.


      I have a get method that takes in some parameters, and attempts to search for Unit batches using the PiModuleDB.PIUnitBatchSearch.


      I have created the modules and units in the module database and can search using the UI in PI System Management Tools to find the PIUnitBatches without issue.


      When stepping through the resulting PiUnitBatchList after the search in my code however, I am not able to access any properties of the PIUnitBatch objects. In debug, I see only COM object threads that state the function evaluation requires all threads to run, and when I force it to execute, the resulting message is " the type name "PIUnitBatch" does not exist in the context PISDK.PISDK.


      It is as if there is no access to the ModuleDB or the COM translation is in some way broken. I have a hunch it is due to how I am creating the connection tot he PI Server.


      The PIServerConnection is an object of a class i created that handles cache, creating new ServerManagers, opening the connection to the PiServers, and storing the current server name for this instance of PI Server connection.


      has anyone done PIUnitBatchSearch before in C# with the PI SDK? I also get similar results when trying to use the PIBatchDB.PIBatchSearch();


      Other methods in the API work find when getting tag snapshots, creating tags, and modifying tag data.


      Code below:


                  PIModuleDB pmdb = PiServerConnection.ModuleDb;
                  PIUnitBatchList unitBatchList = pmdb.PIUnitBatchSearch(userProvidedStart, userProvidedEnd);
                  foreach (PIUnitBatch batch in unitBatchList)
                      string tempStart = Convert.ToString(batch.StartTime);
                      string tempEnd = Convert.ToString(batch.EndTime);
                      string batchID = batch.BatchID;



      if anyone has created a web api to perform actions similar to this, please let me know what you did. Thanks!

        • Re: Module DB PI Unit Search COM object trouble

          Hello Peter,


          Here is a working sample that demonstrates a UnitBatchSearch in C#.

          Re: C# PISDK Batch List with Start and Stop Times


          On top of my head, for the PI-SDK to work best in multi threaded applications ( typical web APIs are multi threaded) you need to set you application as STA.


          There is seems to have a beginning of explanation here on this topic.


          Sean Driscoll or Arnold Woodall may have more ideas.  Let see if they can provide us more info on this.

            • Re: Module DB PI Unit Search COM object trouble

              Perhaps there are type resolution issues because the PISDK interop provided by the install kit wasn't used? (Like if you referenced PISDK from the COM list instead of the .Net list)


              I can't tell if this is just an issue while operating in the debugger to view the batches. Is the error also occurring in a non-debug situation?


              The version of PISDK wasn't mentioned but is useful to know.


              It would be easier if it could be reproduced in a standalone test app with just PISDK then we can run it on our end to see what is wrong.


              Taking a step back, we now recommend using Event Frames to store batch data over the older PI Batch DB going forward. I'm not sure how established this PI server is but is it a possibility to consider a Batch to EF migration? Of course not all situations allow for this because not all existing tools have been migrated to Event Frames such as PI BatchView or RtReports. Developing for Event Frames though would avoid any issues with having to use PI SDK and COM and utilize a pure .Net stack in the application.

              1 of 1 people found this helpful
              • Re: Module DB PI Unit Search COM object trouble

                The problem was Resharper had, for some reason, created a using PISDK.PISDK; for the PiAccess class I created, and on the public properties like PIServer which while normally are PISDK.Servers.Server, in this case had a bunked up reference to the correct level of  nesting in the SDK.


                This was fixed and everything matched what I was able to do in my client test application.

                1 of 1 people found this helpful
                • Re: Module DB PI Unit Search COM object trouble

                  As for a STA application Patrice, web API, to my knowledge, doesn't support it. The real question unrelated to this problem, but still important to my mind now that you bring up the subject of STA is whether or not the COM object from the SDK is capable of it's own synchronization or not. I am new to PI, and even more so tot he SDK, but I assumed it would implement members that were thread safe. Thinking about this again, and how these are COM objects, my guess is they are not.



                  To your knowledge, has anyone implemented a MTA style .NET web API 2 using the PI-SDK without problems related to thread synchronization?

                    • Re: Module DB PI Unit Search COM object trouble

                      Hi Peter,


                      For completeness and other users stumbling across this thread, PI SDK is announced to become deprecated sometime in future. Also we suggest migrating PI Batches to PI Event Frames but as well understand that there are reasons you are forced to stick with PI Batch (and PI SDK).


                      PI SDK Programming Reference (%PIHOME%\Help\pisdk.chm) has an article about Threading. In the contents table please navigate PI SDK Documentation -> PI SDK Manual -> Programming Topics -> Programming Concepts -> Threading or use the following link to PI Live Library https://techsupport.osisoft.com/Documentation/PI-SDK/Introducing_the_PI_SDK/PI-SDK_Overview/threading.htm

                      • Re: Module DB PI Unit Search COM object trouble

                        Hello Peter,


                        I am not a COM master, but let me try to clarify.


                        As Gregor mentioned, today we have two technologies sitting aside: PI-SDK and AF-SDK.  AF-SDK was writtent fully in .NET and for the major parts is thread safe (there maybe places where it is not, but let's keep it simple).  AF-SDK can do same thing as PI-SDK does, and event more and we do not recommend PI-SDK anymore if you are developing a new application with PI, use AF-SDK instead.


                        As for PI-SDK, this is a COM based technology, and I recall that to do multi-threading with it this was not a trivial task.  At least not as trivial as now with the AF-SDK.  And as you are building a .NET Web API, this type of application at its core, uses multi-threading, so this is why I was raising a warning here.  If you need more info on this topic we will need to investigate a bit more. 

                        the documentation Gregor shared on threading is undoubtedly a beginning of explanation.  but simply. if your application allows, you could implement a singleton class, with a queue mechanism ( concurrent queue) that would process the data requested from PI-SDK, that would limit the possibilities of synchronization errors.  But this will work only if your application is not too much data intensive.


                        Ideally you would be using AF-SDK. but as you are using batch, this is a scenario where PI-SDK has to be used.


                        Let us know if you need more info on that topic,


                        have a nice day,

                        • Re: Module DB PI Unit Search COM object trouble
                          Eugene Lee

                          Hi Peter,


                          This is very interesting. I found this link where someone was able to come up with a method to support STA threads in web api. You might want to take a look!


                          Supporting STA Threads in Web API - Ryan Haugh


                          Another thing I would like to point out is that some objects in the PI SDK are thread safe. For example, MTNVS.

                          1 of 1 people found this helpful
                            • Re: Module DB PI Unit Search COM object trouble

                              Eugene, this was actually really helpful for solving another problem.


                              Let me explain:


                              What I observed was the pi message logs were showing the application pool account connecting to the pi server, and then failing to authenticate because it was purposefully left out of the security group mapping to our NT group for this project. The intent was to sit on top of the existing security model which was to map pi groups to NT groups that needed access to vertain tags, etc.


                              To do this, the web API needed to have impersonation and delegation. I first thought we had a delegation problem, but all the other data sources like the SQL servers were working fine, no problems were observed with SPNs, and the delegation was wide open for the application pool account.


                              What I discovered was that the request from the IIS server where impersonation was configured as Enabled,  was getting put on a separate thread than the thread used to spin up a PiServerManagerConnection. Thus, when the code attempted to access the PI server it was defaulting to the app pool. To remedy this, I was able to use the code in this blog. The attribute I created and applied to my API methods that need to touch PI forces the method to run single threaded. This is obviously bad for performance, but I can limit the single threaded behavior to just pi access calls and it remedied the problem. Thanks!

                              1 of 1 people found this helpful