我正在尝试注册一个新的 UPNP 设备(并试图了解它是如何工作的)。
现在,我只是想创建一个简单的测试,让一些东西工作并从那里构建。
我现在正在使用 UPnP 设备主机,我有以下代码:
IUPnPRegistrar registrar = (IUPnPRegistrar)new UPnPRegistrarClass();
String description = GetDescription();
String path = GetPath();
registrar.RegisterDevice(description, "Test1", "Init", "Container", path, 900);
GetDescription 方法返回:
<?xml version="1.0" encoding="utf-8" ?>
<root xmlns="urn:schemas-upnp-org:device-1-0">
<specVersion>
<major>1</major>
<minor>0</minor>
</specVersion>
<device>
<UDN>@@UDN@@</UDN>
<friendlyName>Test device</friendlyName>
<deviceType>urn:schemas-upnp-org:device:XmsHostService:1</deviceType>
<manufacturer>Test</manufacturer>
<manufacturerURL>http://www.test.com</manufacturerURL>
<modelName>Test model name</modelName>
<modelNumber>0.1</modelNumber>
<modelDescription>Test device</modelDescription>
<modelURL>http://www.test.com/test-model-url</modelURL>
<serialNumber>0000001</serialNumber>
<UPC>00000-00001</UPC>
<serviceList>
<service>
<serviceType>urn:schemas-upnp-org:service:XmsHost:1</serviceType>
<serviceId>urn:upnp-org:serviceId:XmsHost</serviceId>
<controlURL></controlURL>
<eventSubURL></eventSubURL>
<SCPDURL>serviceDescription.xml</SCPDURL>
</service>
</serviceList>
</device>
</root>
我的 C:\upnphost\service.xml 包含以下代码:
<?xml version="1.0" encoding="utf-8"?>
<scpd xmlns="urn:schemas-upnp-org:service-1-0">
<specVersion>
<major>1</major>
<minor>0</minor>
</specVersion>
<actionList>
<action>
<name>GetPort</name>
<argumentList>
<argument>
<name>_ReturnValue</name>
<direction>out</direction>
<retval />
<relatedStateVariable>A_ARG_TYPE_GetPort_RetType</relatedStateVariable>
</argument>
</argumentList>
</action>
</actionList>
<serviceStateTable>
<stateVariable sendEvents="no">
<name>A_ARG_TYPE_GetPort_RetType</name>
<dataType>ui4</dataType>
</stateVariable>
<stateVariable sendEvents="no">
<name>Port</name>
<dataType>ui4</dataType>
</stateVariable>
</serviceStateTable>
</scpd>
那么,首先,这个 xml 代码看起来对吗?
当我调用注册设备时,我得到了这个异常:
System.ArgumentException was unhandled
Message=Value does not fall within the expected range.
Source=UPnPHostLib
StackTrace:
at UPnPHostLib.IUPnPRegistrar.RegisterDevice(String bstrXMLDesc, String bstrProgIDDeviceControlClass, String bstrInitString, String bstrContainerId, String bstrResourcePath, Int32 nLifeTime)
at UPnPHosterTester.Hoster.Start() in E:\Dev\Discovery\XMS_SW_Discovery_B\Solution\UPnPHosterTester\Hoster.cs:line 32
at UPnPHosterTester.Program.Main(String[] args) in E:\Dev\Discovery\XMS_SW_Discovery_B\Solution\UPnPHosterTester\Program.cs:line 19
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
但我看不出有什么问题,900 是接受的最小值。其他链接的问题:我知道我必须实现 IUPnPDeviceControl 接口并插入我以前的代码而不是 Test1,但我不知道我应该在 GetServiceObject 中返回什么
编辑 这里是 GetDescription 方法:
private string GetDescription()
{
string xml = File.OpenText("DeviceDescription.xml").ReadToEnd();
xml = xml.Replace("@@UDN@@", "uuid:" +Guid.NewGuid().ToString());
return xml;
}
编辑 2 我尝试过,建议删除所有非强制性属性:
<?xml version="1.0" encoding="utf-8" ?>
<root xmlns="urn:schemas-upnp-org:device-1-0">
<specVersion>
<major>1</major>
<minor>0</minor>
</specVersion>
<device>
<UDN>@@UDN@@</UDN>
<friendlyName>Test device</friendlyName>
<deviceType>urn:test-com:device:HostDevice:1</deviceType>
<manufacturer>Test</manufacturer>
<modelName>Test model name</modelName>
</device>
</root>
但我仍然有完全相同的错误
编辑 3
我确实发现有一个实用程序可以检查服务 xml 文件的良好结构(不是设备 :() 这里:http: //msdn.microsoft.com/en-us/library/aa381938 (v=vs .85).aspx您可以将它与 VS studio 命令提示符一起使用:validatesd.exe
. 它验证我的服务