2009-10-23 41 views
0

我正在编写一个调用WCF服务的Silverlight应用程序。Silverlight忽略来自web.config文件的WCF路径

另一种解决方案包含了Silverlight的以下项目:
- Web项目托管Silverlight应用程序
- Silverlight应用程序项目
- 与服务引用WCF

的Silverlight类库项目,当我上运行Silverlight应用程序我的locahost,Silverlight使用本地主机调用WCF并且正常工作。

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

现在,运行Silverlight应用程序将查找服务的本地主机url,尽管web.config中的端点是部署服务器的端点。

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

任何想法??

感谢您的阅读!

回答

4

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

因此,您有2个选项:
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,那么你不需要任何这样的代码。

+0

@Clay:谢谢兄弟!有效。 – pencilslate

1

我在asp页面中使用silverlight对象的InitParams,该页面托管Silverlight以传递WCF服务url。你可以从asp页面的web.config中获取url。它适用于我的情况。