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();
ServiceM开发者_JAVA技巧etadataBehavior 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!
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:
- Break on the exception and bring up the exception assistant.
- Open the details.
- Look at the InnerException. This will be a TargetInvocationException.
- Expand the InnerException and look at its InnerException.
- 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.
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
精彩评论