[英]Silverlight MEF with WCF service
我有一個包含許多用戶控件的儀表板項目。 本周,我創建了一個不僅僅是用戶控件的應用程序,並將其集成到我的儀表板應用程序中似乎很痛苦。 因此,我尋找解決方案並找到了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應用程序都無法找到/使用我的Web服務。
謝謝
我找到了解決我問題的方法。 因此,這就是如果有人遇到相同的情況:
而不是我的GetMyServiceClient()
。 我這樣初始化我的服務客戶端:
MyServiceClient client = new MyServiceClient("MyService_CustomBinding");
參數是我在ServiceReferences.ClientConfig和voila中的綁定,它的工作原理很吸引人!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.