2009-10-23 42 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。它適用於我的情況。