【问题标题】:Programmatically connect Silverlight to WCF以编程方式将 Silverlight 连接到 WCF
【发布时间】:2012-04-16 08:08:38
【问题描述】:

我继承了一个使用 WCF 服务并托管在 ASP.NET Web 表单中的 Silverlight 应用程序。该应用程序需要通过 HTTP 和 HTTPS 运行,并将安装在客户自己的服务器上。客户端代码最初是使用指向本地托管服务的 Add Service Reference 生成的,所以我的 ServicesReferences.ClientConfig 显然包含对 localhost 的硬编码引用 - 部署到另一台服务器时用处不大,所以显然我需要能够以编程方式设置客户端使用的端点地址。

我的代码目前是:

var binding = new BasicHttpBinding
    {
        MaxBufferSize = 2147483647,
        MaxReceivedMessageSize = 2147483647,
    };
binding.Security.Mode = HtmlPage.Document.DocumentUri.Scheme.StartsWith("https")
    ? BasicHttpSecurityMode.Transport
    : BasicHttpSecurityMode.None;

var documentUri = HtmlPage.Document.DocumentUri;
var builder = new UriBuilder(
        documentUri.Scheme,
        documentUri.Host, 
        documentUri.Port, 
        documentUri.LocalPath.Replace("hostpage.aspx", "MyService.svc"));

var client = new CustomerDetailServicesClient(binding, new EndpointAddress(builder.Uri));
client.ChannelFactory.Faulted += OnChannelFactoryFaulted;
client.DoSomething();

虽然当我使用 HTTP 时这可以正常工作,但每次调用 WCF 服务时尝试通过 HTTPS 访问都会失败,并出现 404。使用 Fiddler,我可以看到只是 URI 方案在发生变化,如果我输入服务的 HTTPS 地址,我会得到预期的元数据页面。

我有什么明显的遗漏吗?

【问题讨论】:

  • 不直接回答您的问题,但仅供参考,您可以像任何其他 App.Config 或 Web.Config 一样更改 ServicesReferences.ClintConfig。 Silverlight .xap 文件只是一个 WinZip 文件。您可以提取 .ClientConfig 对其进行修改,然后将其添加回 Zip 文件。在我以前的工作中,我们一直这样做,尽管您必须使用 WinZip(或其他 Zip 程序之一,而不是 Windows Explorer Zip)。从内存中,您还必须修改 Zip 包之外的文件,然后重新添加它。
  • 所以作为部署的一部分,安装程序会提取 .ClientConfig,设置正确的端点地址,然后将其压缩备份?有趣...
  • 是的,我们就是这样做的。

标签: c# .net wcf silverlight


【解决方案1】:

sigh 用湿鲱鱼打我一巴掌。 Web 服务的 HTTPS 绑定已在 web.config 中注释掉(因为并非我们所有的客户都可以/想要在他们的服务器上运行 HTTPS,并且当 IIS 未配置为 HTTPS 时,为 HTTPS 进行 WCF 绑定会在很多嘈杂的情况下破坏 WCF方式)。

一夜好眠可以为问题带来全新的视角和清晰的视野,这真是令人惊讶。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-02
    • 2011-02-25
    • 2012-07-22
    • 2014-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-16
    相关资源
    最近更新 更多