【问题标题】:Silverlight MEF with WCF service带有 WCF 服务的 Silverlight MEF
【发布时间】:2013-05-09 06:38:57
【问题描述】:

我有一个包含许多用户控件的仪表板项目。本周我创建了一个应用程序,它不仅仅是一个用户控件,并且将它集成到我的仪表板应用程序中似乎很痛苦。所以我搜索了解决方案,发现了 MEF 和 PRISM。 MEF 似乎比 PRISM 更容易一些,我开始使用 this 教程制作 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 应用都找不到/使用我的网络服务。

谢谢

【问题讨论】:

  • 在 IE9+ (F12) 中启动开发工具并开始捕获网络流量。查看正在发送的请求。如果请求构造不正确 - 那么这是客户端问题 - 开始调试客户端。如果请求看起来正确——那么这是服务器问题——开始调试服务器。在许多不同的情况下都会返回“NotFound”,您需要缩小范围。
  • 仅供阅读本文的任何人参考,我不知道如何“添加为链接”。这篇文章解释得很好...global-webnet.net/blogengine/post/2009/01/03/… 摘录:转到主程序集,在我们的例子中是 SDMS.Silverlight,并添加一个现有项目 - 粘贴完整的 ClientConfig 文件路径,然后单击“添加”按钮上的向下三角形并选择“添加链接”

标签: c# wpf wcf silverlight mef


【解决方案1】:

我找到了解决问题的方法。所以如果有人遇到同样的情况,那就是:

而不是我的来自 settings.xml 的GetMyServiceClient()。我这样初始化我的服务客户端:

MyServiceClient client = new MyServiceClient("MyService_CustomBinding");

参数是我在 ServiceReferences.ClientConfig 中的绑定,瞧,它就像一个魅力!

【讨论】:

    猜你喜欢
    • 2011-03-02
    • 2011-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-23
    • 1970-01-01
    相关资源
    最近更新 更多