我有一个带有许多用户控件的仪表板项目。本周我创建了一个应用程序,它不仅仅是一个用户控件,并且将它集成到我的仪表板应用程序中似乎很痛苦。所以我搜索了解决方案,发现了 MEF 和 PRISM。MEF 似乎比 PRISM 更容易一些,我开始使用本教程制作 Hello World MEF 应用程序。进展顺利,我成功注入了一个 Hello World xap。
之后我尝试注入我的真实应用程序并遇到了一些问题。我想指出我解决的问题,因为我可能以错误的方式解决了它们,或者它们可能是我当前问题的原因。
注意:我的应用程序使用启用 Silverlight 的 WCF Web 服务来检索数据。
第一个问题
在 xap 包中找不到 ServiceReferences.ClientConfig。我将此文件添加为我的 MEF 项目客户端的链接。问题解决了。
第二个问题
我在客户端使用 Settings.xml,其中包含以下端点:
<?xml version="1.0" encoding="utf-8" ?>
<Services>
<Service Name="MyService">
<HostPath Name="/ClientBin/MyComponent.xap">
<Endpoint Url="/MyService.svc"></Endpoint>
</HostPath>
<HostPath Name="MyComponent.Web/ClientBin/MyComponent.xap">
<Endpoint Url="MyComponent.Web/MyService.svc"></Endpoint>
</HostPath>
</Service>
</Services>
并阅读本文以获取具有我的 2 个功能的 WCF Web 服务服务客户端,它们是:
public MyServiceClient GetMyServiceClient()
{
if (serviceClient == null)
{
serviceClient = new MyServiceClient();
Uri uriEndpointAddress = serviceClient.Endpoint.Address.Uri;
UriBuilder ub = new UriBuilder(uriEndpointAddress)
{
Host = Application.Current.Host.Source.Host,
Path =
GetURLForService("MyService",
Application.Current.Host.Source.AbsolutePath)
};
serviceClient.Endpoint.Address = new System.ServiceModel.EndpointAddress(ub.Uri);
}
return serviceClient;
}
private string GetURLForService(string ServiceName, string HostURL)
{
string retval = "";
XDocument doc = XDocument.Load("Settings.xml");
if (doc.Root != null)
{
XElement elmService = doc.Root.Elements("Service").FirstOrDefault(c =>
{
XAttribute xAttribute = c.Attribute("Name");
return xAttribute != null && xAttribute.Value.ToLower() == ServiceName.ToLower();
});
if (elmService != null)
{
XElement elmHostPath = elmService.Elements("HostPath").FirstOrDefault(c =>
{
XAttribute xAttribute = c.Attribute("Name");
return xAttribute != null && xAttribute.Value.ToLower() == HostURL.ToLower();
});
if (elmHostPath != null)
{
retval = elmHostPath.Element("Endpoint").Attribute("Url").Value;
}
}
}
return retval;
}
我也添加了我的 Settings.xml 文件作为链接并且问题解决了。
主要问题
解决了这两个问题后,我遇到了主要问题。 远程服务器返回错误:NotFound。
我什至在我的 Settings.xml 中尝试过这个:
<HostPath Name="/MEFHubApp/ClientBin/MyComponent.xap">
<Endpoint Url="/MyComponent.Web/MyService.svc"></Endpoint>
</HostPath>
无论我尝试什么,我的 MEF 应用程序都无法找到/使用我的网络服务。
谢谢