[英]Silverlight MEF with WCF service
I have a dashboard project with many user controls. 我有一个包含许多用户控件的仪表板项目。 This week I've created an application which is more than just a user control and integrating it into my dashboard application seemed like a pain.
本周,我创建了一个不仅仅是用户控件的应用程序,并将其集成到我的仪表板应用程序中似乎很痛苦。 So I searched for solutions and found MEF and PRISM.
因此,我寻找解决方案并找到了MEF和PRISM。 MEF seemed a bit easier than PRISM and I started to do a Hello World MEF app with this tutorial.
MEF似乎比PRISM容易一些,我开始用本教程制作一个Hello World MEF应用程序。 It went well and I injected a Hello World xap successfully.
一切顺利,我成功注入了Hello World xap。
After that I tried to inject my real application and encountered some problems. 之后,我尝试注入我的真实应用程序,遇到了一些问题。 I want to point out problems that I solved because I might solved them in a wrong way or they may be the reason of my current problem.
我想指出我解决的问题,因为我可能以错误的方式解决了它们,或者它们可能是我当前问题的原因。
Note: My application uses a Silverlight enabled WCF Web Service to retrieve data. 注意:我的应用程序使用启用了Silverlight的WCF Web服务来检索数据。
First Problem 第一个问题
ServiceReferences.ClientConfig was not found in xap package. 在xap软件包中找不到ServiceReferences.ClientConfig。 I added this file as link to my MEF project client side.
我将此文件添加为指向我的MEF项目客户端的链接。 Problem solved.
问题解决了。
Second Problem 第二个问题
I am using a Settings.xml on my client side which holds the endpoints like: 我在客户端使用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>
and read this to get WCF Web Service service client with my 2 functions which are: 并阅读本文以获取具有我的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;
}
I've added my Settings.xml file as link also and problem solved. 我还添加了Settings.xml文件作为链接,并解决了问题。
Main Problem 主要问题
After solving these two problems, I've encountered main problem. 解决了这两个问题之后,我遇到了主要问题。 The remote server returned an error: NotFound.
远程服务器返回错误:NotFound。
I even tried this in my Settings.xml: 我什至在我的Settings.xml中尝试了此操作:
<HostPath Name="/MEFHubApp/ClientBin/MyComponent.xap">
<Endpoint Url="/MyComponent.Web/MyService.svc"></Endpoint>
</HostPath>
My MEF app can't find/use my web service no matter what I try. 无论我做什么,我的MEF应用程序都无法找到/使用我的Web服务。
Thanks 谢谢
I found the solution for my problem. 我找到了解决我问题的方法。 So here it is if anyone encounters the same:
因此,这就是如果有人遇到相同的情况:
Instead of my GetMyServiceClient()
from settings.xml. 而不是我的
GetMyServiceClient()
。 I initialized my service client like this: 我这样初始化我的服务客户端:
MyServiceClient client = new MyServiceClient("MyService_CustomBinding");
Parameter is my binding in the ServiceReferences.ClientConfig and voila it worked like a charm! 参数是我在ServiceReferences.ClientConfig和voila中的绑定,它的工作原理很吸引人!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.