8 Replies Latest reply on May 7, 2013 2:30 PM by Bhess

    Sample Code for "FindElement" when using AF Web Service




      I am new to AF Web Service and have checked the manual to write a C# program to call AF Web Service(PISoap), but it doesn't work.


      Can anyone provide C# codes that can call the "FindElements" through the PISoap?


      Thanks in advance.





        • Re: Sample Code for "FindElement" when using AF Web Service

          Hi Chung Wo,


          Assuming you have already added the service reference in your C# project. You can use the FindElements method something like:

          private static void FindElement()
                      PISoapServiceClient sClient = new PISoapServiceClient();
                      ElementSearchConstraints constraints = new ElementSearchConstraints();
                      constraints.AllowMatchOnDerivedTemplates = true;
                      constraints.NameMask = "*";
                      constraints.TemplateNames = new string[] { "Transformer" };
                      constraints.RootPath = @"af:\\afservername\afdatabasename";
                      ElementSearchManner manner = new ElementSearchManner();
                      manner.FlatHierarchy = true;
                      Element[] elements = sClient.FindElements(constraints, manner);
                      foreach (Element e in elements)

           Note: replace afservername and afdatabasename with your AF Server and Database that you are searching.

            • Re: Sample Code for "FindElement" when using AF Web Service

              Hi Han Yong,


              Yes, I have added the service reference in the .NET project and I tried your code, but it doesn't work. The following exception is thrown.


              Is it due to the security problem? What things should I set? I did set my account in the database security of the AF.


              I also attach my App.config.


              Please advise. Thanks in advance.


              Unhandled Exception: System.ServiceModel.FaultException`1[System.ServiceModel.Ex


              ceptionDetail]: WA051: AF Path '\\npspi-sspd02\CIM' could not be resolved (Fault


              Detail is equal to An ExceptionDetail, likely created by IncludeExceptionDetail


              InFaults=true, whose value is:


              System.Exception: WA051: AF Path '\\npspi-sspd02\CIM' could not be resolved


                at OSIsoft.PIWebServices.Impl.ElementSearchImpl.EndFindElements(IAsyncResult




                at AsyncInvokeEndEndFindElements(Object , Object[] , IAsyncResult )


                at System.ServiceModel.Dispatcher.AsyncMethodInvoker.InvokeEnd(Object instanc


              e, Object[]& outputs, IAsyncResult result)


                at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeEnd(MessageR


              pc& rpc)


                at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage7(Me


              ssageRpc& rpc)


                at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationConte




              <?xml version="1.0" encoding="utf-8"?>










                     <binding name="SoapEndpoint" closeTimeout="00:01:00" openTimeout="00:01:00"


                       receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"


                       bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"


                       maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"


                       messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"




                       <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"


                         maxBytesPerRead="4096" maxNameTableCharCount="16384" />


                       <security mode="TransportCredentialOnly">


                         <transport clientCredentialType="Ntlm" proxyCredentialType="None"


                           realm="" />


                         <message clientCredentialType="UserName" algorithmSuite="Default" />












                   <endpoint address="npsbmp-sspd01.corp.clp.com.hk/.../PISoap.svc"


                     behaviorConfiguration="ImpersonationLevel" binding="basicHttpBinding"


                     bindingConfiguration="SoapEndpoint" contract="IPISoapService"


                     name="SoapEndpoint" />








                     <behavior name="ImpersonationLevel">




                         <windows allowedImpersonationLevel="Impersonation"/>













                • Re: Sample Code for "FindElement" when using AF Web Service

                  Hi Chung Wo,


                  From the error message. It seems like PI Web Services cannot resolve the path that you have provided correctly. The path that is indicated in the error message is "'\\npspi-sspd02\CIM". Does this correspond to the AF server and database name that you have?

                    • Re: Sample Code for "FindElement" when using AF Web Service

                      Hi Han Yong,


                      Yes. I can use AF explorer ro view the CIM database from npspi-sspd02.


                      Is there anything wrong from the app.config?

                        • Re: Sample Code for "FindElement" when using AF Web Service

                          I don't think there is anything wrong with the app.config itself. You can try to change the security setting to test if this works at a lower security level. In the PI Web Services install folder, under the ..\Help\Samples folder, you can find various web.config sample for different security setting. You can copy the web_config_basic_no_security.config to test if it works without any security setting.


                          Something that is mentioned in the PI Web Services user guide is that the Process ID account of PI Web Services should be added to PI SQL (AF) Trusted Users group on the AF Server to allow it to access PI AF. If you are using PI Web Services IIS Edition, the Process ID must be the Windows account under which the Internet Information Services (IIS) application pool runs. If you are using PI Web Services Standalone Edition, the Process ID must be the Windows account under which the PI Web Services host service runs. At the same time, the user account of the application that access PI Web Services should have read access to PI AF. Are these in place?

                            • Re: Sample Code for "FindElement" when using AF Web Service

                              HI Hang Yong,




                              I tried to set up my account as the Procees ID account of Web Services under the IIS App Pool, it seems that it can connect the AF with the following codes




                               PISoapServiceClient soapClient = new PISoapServiceClient();


                               string[] templates = soapClient.FindElementTemplates("npspi-sspd02", "CIM", "Template3*", true);


                               for (int i = 0; i < templates.Length; i++)             {               








                              But it doesn't work when I run the code you provided. Another exception throws as the following. Any Hints?




                              Unhandled Exception: System.ServiceModel.FaultException`1[System.ServiceModel.Ex ceptionDetail]: [OSIsoft.AFSDK] Cannot connect to server 'NPSPI-SSPD02'.


                              Server stack trace:    at System.ServiceModel.Channels.ServiceChannel.ThrowIfFaultUnderstood(Message  reply, MessageFault fault, String action, MessageVersion version, FaultConverte r faultConverter)    at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRunt ime operation, ProxyRpc& rpc)    at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean on eway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan tim eout)    at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCall Message methodCall, ProxyOperationRuntime operation)    at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)


                              Exception rethrown at [0]:    at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage req Msg, IMessage retMsg)    at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgDa ta, Int32 type)    at IPISoapService.FindElements(ElementSearchConstraints Constraints, ElementS earchManner Manner)    at PISoapServiceClient.FindElements(ElementSearchConstraints Constraints, Ele mentSearchManner Manner) in C:\Users\CN12457\Documents\Visual Studio 2010\Projec ts\Test\WcfService1\ConsoleApplication2\PISoapService.cs:line 1966    at ConsoleApplication2.Program.Main(String[] args) in C:\Users\CN12457\Docume nts\Visual Studio 2010\Projects\Test\WcfService1\ConsoleApplication2\Program.cs: line 35 Press any key to continue . . .













                                • Re: Sample Code for "FindElement" when using AF Web Service

                                  I suspect this is a Kerberos delegation issue.


                                  From the PI Web Services 2012 Release Notes:


                                  "When using integrated Windows authentication (Kerberos), the process account for PI Web Services must be trusted for delegation when performing PI AF element, element template, or element attribute search or retrieval. See the topic 'Configure Kerberos authentication' in the PI Web Services user guide for configuration instructions."


                                  The fix is to ensure that the process account that is running PI Web Services is marked as 'Trusted for Delegation' in Active Directory, and to change the allowedImpersonationLevel in app.config to 'Delegation'.  For detailed instructions, consult pp. 134-137 in the PI Web Services user guide.

                                    • Re: Sample Code for "FindElement" when using AF Web Service

                                      And, oops, just read up and noticed that you're using NTLM for security, not Kerberos.  Scratch my previous suggestion; that's not going to help.


                                      Could you, as Han suggested, attempt to disable security as a troubleshooting step?  I wonder if specifically NTLM is causing you grief.  What may be happening is that NTLM is giving PI Web Services a Windows identity on the thread, but since NTLM authentication can't be used for impersonation, it's failing.  The reason you wouldn't see the same issues with your element template search is that templates are not secureable in AF, so no impersonation is needed or performed.