QuickOPC User's Guide and Reference
Installed Examples - Web - WcfService1
View with Navigation Tools

A simple Web service using WCF technology. Provides a GetData method to read a value of an OPC item. Use WcfClient1 project (under Console folder) to test this Web service.

The service class:

// WcfService1: A simple Web service using WCF technology. Provides a GetData method to read a value of an OPC item.
// Use WcfClient1 project (under Console folder) to test this Web service.

using OpcLabs.EasyOpc.DataAccess;
using OpcLabs.EasyOpc.DataAccess.Extensions;

// ReSharper disable ArrangeModifiersOrder

namespace WcfService1
{
    // NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in Web.config and in the associated .svc file.
    public class Service1 : IService1
    {
        static Service1()
        {
            // Enable auto-subscribing optimization (not necessary), which can improve performance with repeated Read requests.
            Client.TryEnableAutoSubscribingOptimization();
        }

        // Use a shared client instance to allow for better optimization.
        static private readonly EasyDAClient Client = new EasyDAClient();

        public string GetData()
        {
            // The following call may throw an OpcException, which would be propagated to the client as a FaultException.
            // Production-quality code may need to catch the OpcException and handle it differently.
            object value = Client.ReadItemValue("", "OPCLabs.KitServer.2", "Demo.Ramp");
            return value?.ToString() ?? "";
        }
    }
}

 

Service contract:

using System.ServiceModel;

namespace WcfService1
{
    // NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in Web.config.
    [ServiceContract]
    public interface IService1
    {

        [OperationContract]
        string GetData();
    }
}

 

See Also

Conceptual

Installed Examples - Console