c#XamlParseException

时间:2015-10-30 20:54:18

标签: c# sockets exception

我一直在开发一个用于c#网络练习的基本c#聊天应用程序,并为应用程序构建了一个相当稳定的构建,但是当我在visual studio调试器中启动应用程序的第二个实例时,我遇到了一个非常奇怪的事情。当第一个实例运行完美时,第二个实例在XamlParseException函数中给出了InitializeComponents()。它有一个内部例外通常只允许使用每个套接字地址(协议/网络地址/端口)

  • 让我感到困惑的是,套接字的所有工作和创建以及管理都是在后端的后续代码行中完成的,那么xaml的简单解析如何产生这种异常(在例外中完美解析)第一个已经运行的应用程序实例的情况。

  • 即使程序需要因为套接字冲突而引发异常,也应该稍后抛出(在发送UDP时为TCP /绑定),这样我就可以调试哪个传输引发了异常,但是在这种情况下,它仅在第一行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()中引发,甚至不会进入后续行,其中调用实际与socket交互的实际函数(如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,Boolean skipJournaledProperties,Object rootObject,XamlObjectWriterSettings settings,Uri baseUri)”

对于套接字内部异常,stacktrace字符串为:

  

“System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot,SocketAddress socketAddress)\ r \ n,位于System.Net.Sockets的System.Net.Sockets.Socket.Bind(EndPoint localEP)\ r \ n。 UdpClient..ctor(Int32 port,AddressFamily系列)\ r \ n在C:\ Users \ Kaustubh \ Documents \ Visual Studio 2015 \ Projects \ ChatApplication \ ChatApp \ MainWindow.xaml.cs中的ChatApp.MainWindow..ctor():第100行“

谢谢,如果您需要更多代码,请告诉我们。

1 个答案:

答案 0 :(得分:1)

I had a data member initialised as

private UdpClient broadcast_receiver= new UdpClient(15069);

and it was failing to bind to the port and hence the error but since the members were being initialised along with the InitiliseComponents() call so was this exception being thrown.