c#XamlParseException中套接字的InnerException
本文关键字:InnerException 套接字 c#XamlParseException | 更新日期: 2023-09-27 18:19:55
我一直在开发一个基本的c#chat应用程序,用于在c#中练习网络,并为该应用程序构建了一个相当稳定的构建。然而,当我在visual studio调试器中启动该应用程序的第二个实例时,我遇到了一件非常奇怪的事情。当第一个实例完美运行时,第二个实例在InitializeComponents()
函数中给出了XamlParseException
。它有一个内部异常每个套接字地址(协议/网络地址/端口)通常只允许使用一次。
-
让我困惑的是,套接字的所有工作、创建和管理都是在后端的后续代码行中完成的,那么xaml的简单解析是如何导致这个异常的(在应用程序的第一个实例已经运行的情况下,它的解析是完美的)。
-
即使程序由于套接字冲突而需要抛出异常,也应该稍后抛出(在绑定TCP/发送UDP时),这样我就可以调试抛出异常的传输,但在这种情况下,它只在第一行
InitializeComponents()
失败。
提前感谢,如果需要,请将标题/内容编辑为更合适的内容。如果需要代码,请评论,我会提供它。
EDIT-谷歌搜索没有给出两个XamlParseException
和唯一一个使用套接字异常之间的可能关系。XamlParseException
被链接到xaml中的错误,这是显而易见的,而在套接字异常的情况下,我是由于同一端口上的套接字冲突
这是XAML代码:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ChatApp"
xmlns:Conv="clr-namespace:Converters" xmlns:commands="clr-namespace:custom_commands"
xmlns:System="clr-namespace:System;assembly=mscorlib" x:Name="wnd" x:Class="ChatApp.MainWindow"
mc:Ignorable="d"
Title="MainWindow" Height="600" Width="700" MinWidth="550" MinHeight="450" ContentRendered="sort">
<Window.Resources>
<Conv:sub x:Key="converter_sub" />
<Conv:margin_sub x:Key="converter_margin_sub" />
<Conv:button_margin_sub x:Key="converter_button_margin_sub" />
<Conv:mul x:Key="converter_mul" />
<Conv:mul_and_sub x:Key="converter_mul_and_sub" />
<Conv:sub_and_mul x:Key="converter_sub_and_mul" />
<Conv:tab_width_calc x:Key="tab_calc" />
<Conv:messagebox_width_calc x:Key="messagebox_calc" />
<Conv:join_nick_and_ip x:Key="join_nick_and_ip" />
<Conv:subtract_two_multibindings x:Key="subtract_two_multibindings" />
<Conv:status_bar_text x:Key="status_bar_text" />
<Storyboard x:Key="show_log" >
<ThicknessAnimation Storyboard.TargetProperty="Margin" To="0,0,0,0" DecelerationRatio=".9" Duration="0:0:0.8"/>
</Storyboard>
<Storyboard x:Key="hide_log" >
<ThicknessAnimation Storyboard.TargetProperty="Margin" To="0,0,-210,0" DecelerationRatio=".9" Duration="0:0:0.8"/>
</Storyboard>
<Storyboard x:Key="show_rect" >
<DoubleAnimation Storyboard.TargetProperty="Opacity" To="0.1" Duration="0:0:0.5"/>
</Storyboard>
<Storyboard x:Key="hide_rect" >
<DoubleAnimation Storyboard.TargetProperty="Opacity" To="0" Duration="0:0:0.5" />
</Storyboard>
</Window.Resources>
<Window.CommandBindings>
<CommandBinding Command="commands:custom_commands.exit" Executed="ExitCommand_Executed"/>
<CommandBinding Command="commands:custom_commands.connect_new" Executed="manuallyConnectCommand_Executed"/>
</Window.CommandBindings>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="25"/>
<RowDefinition/>
<RowDefinition Height="20"/>
</Grid.RowDefinitions>
<Rectangle Fill="Gray" Opacity="0" Panel.ZIndex="-1" Name="hide_log_rect" HorizontalAlignment="Left" Height="{Binding ActualHeight, ConverterParameter=25, Converter={StaticResource converter_sub}, ElementName=wnd}" Stroke="Transparent" VerticalAlignment="Top" Width="{Binding ActualWidth, ElementName=wnd}" Grid.RowSpan="2" Mouse.MouseUp="hide_log"/>
<Rectangle Fill="Transparent" Opacity="0" Panel.ZIndex="-1" Name="lose_searcharea_focus_rect" HorizontalAlignment="Right" Height="{Binding ActualHeight, ConverterParameter=25, Converter={StaticResource converter_sub}, ElementName=wnd}" Stroke="Transparent" VerticalAlignment="Top" Grid.RowSpan="2" Mouse.MouseUp="lose_searcharea_focus_rect_MouseUp">
<Rectangle.Width>
<MultiBinding Converter="{StaticResource subtract_two_multibindings}" ConverterParameter="10">
<Binding Path="ActualWidth" ElementName="wnd"/>
<Binding Path="ActualWidth" ElementName="status_area"/>
</MultiBinding>
</Rectangle.Width>
</Rectangle>
<Menu IsMainMenu="True" Panel.ZIndex="1" x:Name="menu" HorizontalAlignment="Left" Height="25" VerticalAlignment="Top" Width= "{Binding ActualWidth, ElementName=wnd}">
<MenuItem Header="File">
<MenuItem Header="Exit" Command="commands:custom_commands.exit" />
</MenuItem>
<MenuItem Header="Options">
<MenuItem Header="Connect Manually" Command="commands:custom_commands.connect_new"/>
<MenuItem x:Name="toggle_server" Header="Server Running" Click="toggle_server_status" IsCheckable="True" IsChecked="True"/>
<MenuItem x:Name="toggle_broadcast" Header="Server Broadcasting" Click="toggle_broadcasting" IsCheckable="True" IsChecked="True"/>
</MenuItem>
</Menu>
<StatusBar Panel.ZIndex="51" Height="20" Grid.Row="2" VerticalAlignment="Top" Width="{Binding ActualWidth, ElementName=wnd}">
<StatusBarItem Margin="{Binding ElementName=wnd, Path=ActualWidth, Converter={StaticResource converter_margin_sub}, ConverterParameter=260}">
<TextBlock FontSize="11.5" Text="{Binding ElementName=toggle_server, Path=IsChecked, Converter={StaticResource status_bar_text}, ConverterParameter=0}"/>
</StatusBarItem>
<Separator/>
<StatusBarItem>
<TextBlock FontSize="11.5" Text="{Binding ElementName=toggle_broadcast, Path=IsChecked, Converter={StaticResource status_bar_text},ConverterParameter= 1}"/>
</StatusBarItem>
</StatusBar>
<Grid Panel.ZIndex="3" x:Name="grid1" HorizontalAlignment="Left" Height="{Binding ActualHeight, ConverterParameter=25, Converter={StaticResource converter_sub}, ElementName=wnd}" Grid.Row="1" VerticalAlignment="Top" Width="{Binding ActualWidth, ElementName=wnd}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100*" MaxWidth="228.61272" MinWidth="140.63584"/>
<ColumnDefinition Width="250*"/>
</Grid.ColumnDefinitions>
<Canvas Grid.Column="0" Grid.Row="0" Name="status_area" FocusManager.GotFocus="search_area_GotFocus">
<ListView x:Name="listView" Height="{Binding ActualHeight, ConverterParameter=103, Converter={StaticResource converter_sub}, ElementName=grid1}" Canvas.Left="10" Canvas.Top="43" MaxWidth="210" Width="{Binding ActualWidth, ConverterParameter=0.2857143-20, Converter={StaticResource converter_mul_and_sub}, ElementName=grid1}">
<ListView.Resources>
<ContextMenu x:Key="context_menu">
<MenuItem Header="Connect" Click="broadcast_list_connect">
<MenuItem.Tag>
<MultiBinding Converter="{StaticResource join_nick_and_ip}">
<Binding Path="nick"/>
<Binding Path="ip"/>
</MultiBinding>
</MenuItem.Tag>
</MenuItem>
</ContextMenu>
</ListView.Resources>
<ListView.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="ContextMenu" Value="{StaticResource context_menu}">
</Setter>
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn Width="{Binding ActualWidth, ConverterParameter=10, Converter={StaticResource converter_sub}, ElementName=listView}" DisplayMemberBinding="{Binding nick}">
<GridViewColumnHeader x:Name="sort_header" Tag="Name" Click="sort" >
<TextBlock HorizontalAlignment="Center"><Run Text="Nick "/></TextBlock>
</GridViewColumnHeader>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<TextBox x:Name="search_textbox" Height="28" Canvas.Left="10" TextWrapping="Wrap" Text="Search.." Canvas.Top="5" Width="{Binding ActualWidth, ConverterParameter=0.2857143-20, Converter={StaticResource converter_mul_and_sub}, ElementName=grid1}" MaxWidth="210" KeyUp="search_textbox_KeyUp"/>
</Canvas>
<Canvas Grid.Column="1" Grid.Row="0">
<TabControl SelectionChanged="tab_SelectionChanged" x:Name="tabControl" Height="{Binding ActualHeight, ConverterParameter=65, Converter={StaticResource converter_sub}, ElementName=grid1}" Width="{Binding ActualWidth, ConverterParameter=0.7142857-228.61272, Converter={StaticResource tab_calc}, ElementName=grid1}" Canvas.Top="5"/>
</Canvas>
</Grid>
<Canvas Panel.ZIndex="51" x:Name="log_canvas" Background="CornflowerBlue" HorizontalAlignment="Right" Height="{Binding ActualHeight, ConverterParameter=35, Converter={StaticResource converter_sub}, ElementName=grid1}" Margin="0,0,-210,0" VerticalAlignment="Top" Width="210" Grid.RowSpan="2" Opacity="0.9">
<Menu Height="25">
<MenuItem Width="31" Height="25" HorizontalAlignment="Right" Margin="-210,0,0,0" x:Name="log_menu_button" Click="toggle_log" >
<MenuItem.Effect>
<DropShadowEffect BlurRadius="2" ShadowDepth="2"/>
</MenuItem.Effect>
<MenuItem.Icon>
<Image Width="20" Height="20" Source="Resources/menu.png" Margin="3,0,0,2"/>
</MenuItem.Icon>
</MenuItem>
</Menu>
<ListBox x:Name="log" Background="White" Height="{Binding ActualHeight, ConverterParameter=25, Converter={StaticResource converter_sub}, ElementName=log_canvas}" Canvas.Top="25" Width="210"/>
</Canvas>
</Grid>
和一个小的,我想是c#主类构造函数代码的相关部分:
public MainWindow()
{
InitializeComponent();
get_machine_name();
server_start();
receive_broadcasts();
Thread thread = new Thread(() => update_available_clients());
thread.Name = "Bradcast List Update";
thread.IsBackground = true;
thread.Start();
.
.
.
.
}
在这里,异常只在第1行InitializeComponents()中抛出,甚至没有继续到后面的行,在这些行中,实际与套接字交互的实际函数(如server_start())被调用。
调用堆栈:
PresentationFramework.dll!System.Windows.Markup.WpfXamlLoader.Load(System.Xaml.XamlReader xamlReader, System.Xaml.IXamlObjectWriterFactory writerFactory, bool skipJournaledProperties, object rootObject, System.Xaml.XamlObjectWriterSettings settings, System.Uri baseUri) Unknown
PresentationFramework.dll!System.Windows.Markup.WpfXamlLoader.LoadBaml(System.Xaml.XamlReader xamlReader, bool skipJournaledProperties, object rootObject, System.Xaml.Permissions.XamlAccessLevel accessLevel, System.Uri baseUri) Unknown
PresentationFramework.dll!System.Windows.Markup.XamlReader.LoadBaml(System.IO.Stream stream, System.Windows.Markup.ParserContext parserContext, object parent, bool closeStream) Unknown
PresentationFramework.dll!System.Windows.Application.LoadBamlStreamWithSyncInfo(System.IO.Stream stream, System.Windows.Markup.ParserContext pc) Unknown
PresentationFramework.dll!System.Windows.Application.LoadComponent(System.Uri resourceLocator, bool bSkipJournaledProperties) Unknown
PresentationFramework.dll!System.Windows.Application.DoStartup() Unknown
PresentationFramework.dll!System.Windows.Application._ctor.AnonymousMethod__0(object unused) Unknown
WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate callback, object args, int numArgs) Unknown
WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.TryCatchWhen(object source, System.Delegate callback, object args, int numArgs, System.Delegate catchHandler) Unknown
WindowsBase.dll!System.Windows.Threading.DispatcherOperation.InvokeImpl() Unknown
WindowsBase.dll!System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(object state) Unknown
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) Unknown
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) Unknown
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) Unknown
WindowsBase.dll!System.Windows.Threading.DispatcherOperation.Invoke() Unknown
WindowsBase.dll!System.Windows.Threading.Dispatcher.ProcessQueue() Unknown
WindowsBase.dll!System.Windows.Threading.Dispatcher.WndProcHook(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam, ref bool handled) Unknown
WindowsBase.dll!MS.Win32.HwndWrapper.WndProc(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam, ref bool handled) Unknown
WindowsBase.dll!MS.Win32.HwndSubclass.DispatcherCallbackOperation(object o) Unknown
WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate callback, object args, int numArgs) Unknown
WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.TryCatchWhen(object source, System.Delegate callback, object args, int numArgs, System.Delegate catchHandler) Unknown
WindowsBase.dll!System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority priority, System.TimeSpan timeout, System.Delegate method, object args, int numArgs) Unknown
WindowsBase.dll!MS.Win32.HwndSubclass.SubclassWndProc(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam) Unknown
[Native to Managed Transition]
[Managed to Native Transition]
WindowsBase.dll!System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame frame) Unknown
WindowsBase.dll!System.Windows.Threading.Dispatcher.PushFrame(System.Windows.Threading.DispatcherFrame frame) Unknown
PresentationFramework.dll!System.Windows.Application.RunDispatcher(object ignore) Unknown
PresentationFramework.dll!System.Windows.Application.RunInternal(System.Windows.Window window) Unknown
PresentationFramework.dll!System.Windows.Application.Run(System.Windows.Window window) Unknown
ChatApp.exe!ChatApp.App.Main() C#
[Native to Managed Transition]
[Managed to Native Transition]
mscorlib.dll!System.AppDomain.ExecuteAssembly(string assemblyFile, System.Security.Policy.Evidence assemblySecurity, string[] args) Unknown
Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() Unknown
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state) Unknown
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) Unknown
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) Unknown
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) Unknown
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() Unknown
外部XamlParseException的堆栈跟踪是字符串:
"位于System.Windows.Markup.WpfXamlLoader.Load(XamlReader XamlReader、IXamlObjectWriterFactory writerFactory、布尔skipJournaledProperties、Object rootObject、XamlObjectWriterSettings settings、Uri-baseUri)"
对于套接字内部异常,堆栈跟踪字符串为:
"在System.Net.Socket.Socket.DoBind(EndPoint endPointSnapshot,SocketAddress SocketAddress)''r''n在System.Net.Socket.SSocket.Bind(EndPoint localEP)''r''n在System.Net.Socket.UdpClient.ctor(Int32端口,AddressFamily家族)''r''n在C:''Users''Kaustubh''Documents''Visual Studio 2015''Projects''ChatApplication''ChatApp''MainWindow.xaml.cs:line 100"
谢谢,如果你需要更多的代码,请告诉。
我有一个初始化为的数据成员
private UdpClient broadcast_receiver= new UdpClient(15069);
它未能绑定到端口,因此出现了错误,但由于成员是与InitiliseComponents()调用一起初始化的,因此引发了此异常。