Silverlight忽略来自web.config文件的WCF路径

时间:2009-10-23 19:49:46

标签: .net-3.5 silverlight-3.0 wcf

我正在编写一个调用WCF服务的Silverlight应用程序。

另一个解决方案包含以下Silverlight项目:
- 用于托管silverlight app的Web项目 - Silverlight应用项目
- 具有对WCF的服务引用的Silverlight类库项目

当我在我的locahost上运行Silverlight应用程序时,Silverlight使用localhost调用WCF并且工作正常。

然后我发布了服务和Web应用程序并将其部署到另一台服务器。修改了web.config文件以指向已部署的端点地址。

现在,运行Silverlight应用程序会查找该服务的localhost url,尽管web.config中的端点是已部署服务器的端点。

silverlight应用程序从哪里查找svc url? 它似乎没有从web.config文件中读取它。但是,似乎更像是在构建/发布过程中将url嵌入到程序集中。

有什么想法吗?

感谢阅读!

2 个答案:

答案 0 :(得分:4)

Silverlight应用程序根本不会查看托管服务器的web.config - 它位于服务器端,而不是客户端上运行的silverlight应用程序可见。 Silverlight应用程序查找自己的ServiceReferences.clientconfig文件或在代码中创建本地服务代理时以编程方式指定的URL。

所以,你有两个选择:
1.在构建Silverlight应用程序的可部署版本之前修改ServiceReferences.clientconfig 2.使用代码构建带有URL的客户端端点。

我们使用第二个选项,因为我们希望有一个标准的编程接口来配置我们的端点。我们做这样的事情(当然,如果它是面向公众的服务,则不是MaxValue的):


        public ImportServiceClient CreateImportServiceClient()
        {
            return new ImportServiceClient(GetBinding(), GetServiceEndpoint("ImportService.svc"));
        }

        public ExportServiceClient CreateExportServiceClient()
        {
            return new ExportServiceClient(GetBinding(), GetServiceEndpoint("ExportService.svc"));
        }

        protected override System.ServiceModel.Channels.Binding GetBinding()
        {
            BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
            binding.MaxBufferSize = int.MaxValue;
            binding.MaxReceivedMessageSize = int.MaxValue;
            binding.SendTimeout = TimeSpan.MaxValue;
            binding.ReceiveTimeout = TimeSpan.MaxValue;
            return binding;
        }

        protected EndpointAddress GetServiceEndpoint(string serviceName)
        {
            if (Settings == null)
            {
                throw new Exception("Service settings not set");
            }
            return
                new EndpointAddress(ConcatenateUri(Settings.ServiceUri,serviceName));
        }

        protected EndpointAddress GetServiceEndpoint(string serviceName, Uri address)
        {
            if (Settings == null)
            {
                throw new Exception("Service settings not set");
            }
            return new EndpointAddress(ConcatenateUri(address, serviceName));
        }

“ImportServiceClient”和“ExportServiceClient”等类是从创建对WCF服务的服务引用生成的代理。 Settings.ServiceUri是我们存储我们应该与之交谈的服务器地址的地方(在我们的例子中,它是通过参数动态设置到托管的页面中的silverlight插件,但你可以使用你喜欢的任何方案来管理这个地址)。

但是如果您只想调整ServiceReferences.ClientConfig,那么您不需要任何这样的代码。

答案 1 :(得分:1)

我在asp页面中使用了Silverlight对象的InitParams,它承载了Silverlight以传递WCF服务URL。您可以从asp页面中的web.config获取URL。它适用于我的情况。

相关问题