我尝试以这种方式启动 OPC UA 服务器:http ://documentation.unified-automation.com/uasdkdotnet/2.1.0/html/L3ServerTutGSLess01.html
ApplicationLicenseManager.AddProcessLicenses(Assembly.GetExecutingAssembly(), "License.lic");
MyServerManager server = new MyServerManager();
ApplicationInstance.Default.Start(server, null, server); //Start the server
出现
ApplicationInstance.Default.Start(server, null, server)"
以下错误:System.NullReferenceException:对象引用未设置为对象实例。在 UnifiedAutomation.UaServer.ServerSettings..ctor(ApplicationInstance 应用程序) 在 UnifiedAutomation.UaServer.ServerManager.OnServerStarting(ApplicationInstance 应用程序) 在 UnifiedAutomation.UaBase.ServerBase.Start(ApplicationInstance 应用程序) 在 UnifiedAutomation.UaServer.ServerManager.Start(ApplicationInstance 应用程序) 在UnifiedAutomation.UaBase.ApplicationInstance.Start(ServerBase 服务器,WaitCallback 回调,对象 userData)在 TapakoServerStarter.cs 中 VeitsServer.TapakoServerStarter.StartAkomiServer(IDevice testDeviceToLink):行。39 在 OpcUaServerTest.cs 中的 Implementationstests.OpcUaServerTest.ServerShouldRun ():第 44 行
如果从Main()
. 但是,一旦我尝试通过同一项目图中的外部项目(例如测试项目)调用 OpcUaServerStarter,就会出现 NullReferenceException。
也许项目必须编译为 .dll 或者我必须添加一些引用?或者它的可见性在 OPC-UA 网站上MyServerManager
是有原因的。internal
MyServerManager
类(工作的唯一关键区别MyServerManager
可能是public
封装):
public class MyServerManager : ServerManager
{
private NodeManager _nodeManager;
private ObjectModel _objectModel;
/// <summary>
/// Method is called (from SDK) when NodeManager starts up.
/// </summary>
/// <param name="rootNodeManager"></param>
protected override void OnRootNodeManagerStarted(RootNodeManager rootNodeManager)
{
Console.WriteLine("Creating Node Manager.");
_nodeManager = new NodeManager(this);
_nodeManager.Startup();
_objectModel = new ObjectModel(_nodeManager);
}
/// <summary>
/// Creates an internal model of the given device and automatically creates nodes and callbacks
/// </summary>
/// <param name="device">AKOMI Device that will be shown on the Server</param>
public void LinkObjectToModel(IDevice device)
{
if (_objectModel == null)
{
throw new NullReferenceException("hv: objectModel is not initilized, try starting the server first.");
}
Console.WriteLine("Register Device: " + device.GetType().Name);
_objectModel.RegisterAkomiDevice(device, 0, 4);
}
/// <summary>
/// Creates an internal model of the given entity and automatically creates nodes and callbacks
/// </summary>
public void LinkObjectToModel(object entity, string name, int curLvl, int maxLvl)
{
if (_objectModel == null)
{
throw new NullReferenceException("hv: objectModel is not initilized, try starting the server first.");
}
Console.WriteLine("Register Entity: " + name);
_objectModel.RegisterEntity(entity, name, curLvl, maxLvl);
}
}
谢谢!