我有一个包含WCF服务的WPF应用程序。
Xaml代码非常简单:
<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>
这是服务:
命名空间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;
}
}
}
我在第1行得到一个Xaml解析异常,可能是什么问题? 谢谢!
答案 0 :(得分:3)
您的Window1构造函数抛出异常。令人困惑的是,WPF在XamlParseException中包含此类异常,即使它们与XAML无关。
要了解发生了什么:
答案 1 :(得分:0)
当构造函数中的某些内容甚至程序启动失败时,我经常得到一个XAML解析异常。虽然XAML是正确的,但运行时无法构造所有必需的对象来启动XAML并抛出此错误。
查看异常:任何可能显示其他错误的内部异常?
如果这没有帮助,请逐步调试。但是没有任何进一步的帮助,这很难解决。
-sa