views:

108

answers:

2

Hi! I've got a WPF application containing a WCF service.

The Xaml code is pretty simple:

<Window    x:Class="WpfApplication1.Window1"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           Title="Server" Height="308" Width="560" >
<Grid>
       <Grid Margin="2,2,0,0" Name="grid1">
           <RichTextBox Margin="14,29,12,39"    Name="richTextBox1" />
           <TextBox Height="24" Margin="16,0,80,9" Name="textBox1"    VerticalAlignment="Bottom">Enter your    text here</TextBox>
           <Button Height="24" HorizontalAlignment="Right"    Margin="0,0,12,9" Name="button1"    VerticalAlignment="Bottom"    Width="63">Send</Button>
           <Label Height="23" Margin="16,0,12,0" Name="label1"    VerticalAlignment="Top">Address:</Label>
       </Grid>
</Grid>
</Window>

Here is the service:

namespace WpfApplication1 {

[ServiceContract(CallbackContract=typeof(IMyCallbackContract))]
public interface IMyService
{
    [OperationContract(IsOneWay = true)]
    void NewMessageToServer(string msg);

    [OperationContract(IsOneWay = true)]
    bool ServerIsResponsible();

}



[ServiceContract]
public interface IMyCallbackContract
{
    [OperationContract]
    void NewMessageToClient(string msg);

    [OperationContract]
    void ClientIsResponsible();

}


/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>


       public partial class Window1 : Window
     {


           public Window1()
         {


        InitializeComponent();

        ServiceMetadataBehavior behavior = new
        ServiceMetadataBehavior();
        //behavior.HttpGetEnabled = true;

        //behavior.
        ServiceHost serviceHost = new
        ServiceHost(
        typeof(MyService),
        new Uri("net.tcp://localhost:8080/"));

        serviceHost.Description.Behaviors.Add(behavior);

        serviceHost.AddServiceEndpoint(
            typeof(IMetadataExchange),
            MetadataExchangeBindings.CreateMexTcpBinding(),
            "mex");

        serviceHost.AddServiceEndpoint(
            typeof(IMyService),
            new NetTcpBinding(),
            "ServiceEndpoint");

            serviceHost.Open();
            MessageBox.Show(
                "server is up");

       //  label1.Content = label1.Content + String.Format("   net.tcp://localhost:8080/");
        }

      }

       public class MyService : IMyService
       {
           public void NewMessageToServer(string msg)
           {

           }

           public bool ServerIsResponsible()
           {
               return true;
           }

       }

}

I am getting a Xaml parse exception in Line 1, what can be the problem? Thanks!

+3  A: 

Your Window1 constructor is throwing an exception. Confusingly, WPF wraps such exceptions in a XamlParseException, even though they have nothing to do with XAML.

To find out what is going on:

  1. Break on the exception and bring up the exception assistant.
  2. Open the details.
  3. Look at the InnerException. This will be a TargetInvocationException.
  4. Expand the InnerException and look at its InnerException.
  5. This "inner inner exception" is the exception that was thrown in your constructor. Look at the type and message of the exception, and go from there.

I wrote a few tips on debugging XamlParseExceptions here.

itowlson
Just overlapped my answer. If this is helpful, this should be considered the answer.
Sascha
Excellent article you've written on XamlParseExceptions.
Metro Smurf
Thanks a lot, really helpful!
Yaroslav
A: 

Hi,

often I get a XAML parse exception when something in the constructor or even the program launch fails. Though the XAML is correct, the runtime can not construct all necessary objects to start the XAML up and throws this error.

Have a look at the exception: Any inner exceptions that might show another error?

If this doesn't help, debug step by step. But without any further help, this is hard to tackle.

-sa

Sascha