找不到媒体和设备对象或命名空间

Media & Device object or namespace could not be found

我是 c# 的新手,正在尝试编写一个基本程序以通过 Onvif 摄像头设备流式传输视频。我基本上能够理解和理解 this 8 min tutorial.

但有些事情我想不通。即使在添加了 Vlc 引用和两个 Onvif 服务引用之后,仍有几段代码无法编译。该教程很难理解,因为他跳过了主要步骤。我试图确定 Device & Media & EndPointAddress 在以下代码中代表什么(根据他的教程)。

public partial class MainWindow : Window
{
   UriBuilder deviceUri;
   Media.Media2Client media;
   Media.MediaProfile[] profiles;

   public MainWindow()
   {
      InitializeComponent();
   }


   private void button1_Click(object sender, EventArgs e)
   {
      //Constructor for the custom Uri requires the onvif service address. 
      deviceUri = new UriBuilder("http:/onvif/device_service");

      //Sting manipulation: Need to check if our address contains a port. (Input validation)
      string[] addr = Adress.Text.Split(':');

      deviceUri.Host = addr[0];

      if (addr.Length == 2)
         deviceUri.Port = Convert.ToInt16(addr[1]);

      System.ServiceModel.Channels.Binding binding;
      HttpTransportBindingElement httpTransport = new HttpTransportBindingElement();
      httpTransport.AuthenticationScheme = System.Net.AuthenticationSchemes.Digest;
      binding = new CustomBinding(new TextMessageEncodingBindingElement(MessageVersion.Soap12WSAddressing10, Encoding.UTF8), httpTransport);

      Device.DeviceClient device = new Device.DeviceClient(binding, new EndpointAddress(deviceUri.ToString()));
      Device.Service[] services = device.GetServices(false);

      Device.Service xmedia = services.FirstOrDefault(s => s.Namespace == "http://www.onvif.org/ver20.media/wsdl");

      if (xmedia != null)
      {
         media = new Media.Media2Client(binding, new EndPointAddress(deviceUri.ToString()));
         media.ClientCredentials.HttpDigest.ToString.ClientCredential.UserName = Login.Text;
         media.ClientCredentials.HttpDigest.ToString.ClientCredential.UserName = Login.Text;
         media.ClientCredentials.HttpDigest.AllowedImpresonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;

         profiles = media.GetProfiles(null, null);
         if (profiles != null)
            foreach (var p in profiles)
               listBox1.Items.Add(p.Name);

      }

      listBox1.SelectionChanged += OnSelectionChanged;
      video.MediaPlayer.VlcLibDirectoryneeded += MediaPlayer_VlcLibDirectoryNeeded;
      video.MediaPlayer.EndInit();


   }

   private void MediaPlayer_VlcLibDirectoryNeeded(object sender, Vlc.DotNet.Forms.VlcLibDirectoryNeededEventArgs e)
   {
      if (IntPtr.Size == 4)
      {
         e.VlcLibDirectory = new System.IO.DirectoryInfo(@"C:\Program Files\VideoLAN\VLC");
      }
   }

   private void OnSelectionChanged(object sender, RoutedEventArgs e)
   {
      if (profiles != null && listBox1.SelectedIndex >=)
      {
         UriBuilder uri = new UriBuilder(media.GetStreamUri("RtspOverHttp", profiles[listBox1.SelectedIndex].token));
         uri.Host = deviceUri.Host;
         uri.Port = deviceUri.Port;
         uri.Scheme = "rtsp";
         information.Text = uri.Path; ;
         string[] options = { ":rtsp-http", ":rtps-http-port=" + uri.Port, ":rtsp-user=" + Login.Text, ":rtsp-pwd=" + passwordBox.Password };

         video.MediaPlayer.Play(uri.Uri, options);

      }
   }
}

DeviceMedia 都是您项目中的名称空间,其中 class 是您引用的 Web 服务中的 auto-generated。当您通过 References > Add Service Reference 添加服务引用时,Visual Studio 将使用 WCF Web 从 WSDL 元数据为 WCF 客户端代理创建源代码Service Reference 工具,以便可以访问该服务。

WSDL 或 Web 服务描述语言 是一种基于 XML 的语言,用于描述 Web 服务的界面,您可以在此处找到教程中的语言:

EndpointAddress class 位于 System.ServiceModel 程序集中,您可能在那里输入错误。但是,还有一个 NuGet 包,名为 System.Private.ServiceModel, but you won't need that. From the documentation of EndpointAddress:

Provides a unique network address that a client uses to communicate with a service endpoint.