繁体   English   中英

具有WCF服务的Silverlight MEF

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM