2008-12-16 56 views
10

我有一個silverlight控件,它有一個silverlight啓用的wcf服務的引用。wcf服務綁定中的相對url

當我在我的Silverlight控件添加到服務的引用,它增加了以下我clientconfig文件:

<configuration> 
    <system.serviceModel> 
     <bindings> 
      <basicHttpBinding> 
       <binding name="BasicHttpBinding_DataAccess" maxBufferSize="2147483647" 
        maxReceivedMessageSize="2147483647"> 
        <security mode="None" /> 
       </binding> 
      </basicHttpBinding> 
     </bindings> 
     <client> 
      <endpoint address="http://localhost:3097/MyApp/DataAccess.svc" 
       binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_DataAccess" 
       contract="svcMyService.DataAccess" name="BasicHttpBinding_DataAccess" /> 
     </client> 
    </system.serviceModel> 
</configuration> 

如何指定端點地址,而不是絕對URL相對URL?無論我在何處部署Web應用程序而無需編輯clientconfig文件,我都希望它能夠正常工作,因爲Silverlight組件和Web應用程序將始終部署在一起。我想我可以指定「DataAccess.svc」,但似乎並不那麼喜歡。

回答

4

您不能在客戶端端點配置中使用相對URI。你可以做的只是添加另一個構造函數到你的代理類,它將採取某種URL參數,你可以從另一個配置值中獲取或者使用一個Dns類方法。

+0

如果我要採取這種方法,我會在哪裏放我的相對url參數?我正在看ServiceReferences.ClientConfig,我沒有看到一個地方 - 它似乎沒有相同的元素和web.config文件。 – Jeremy 2008-12-17 17:17:31

14

我的解決辦法:

而不是使用devault構造函數(使用ServiceReferences.ClientConfig文件)來實例化我的代理類,我用的是以下幾點:

svcMyService.DataAccessClient svcProxy_m; 

System.ServiceModel.BasicHttpBinding binding = new System.ServiceModel.BasicHttpBinding(); 

/* 
Create an end point, build a an absolute uri reference by specifing the host address and a relative referece to the service page. 
Application.Current.Host.Source will be something like Http://server/app/ClientBin/SilverlightApp.xap"<br/><br/> 
Specifying Uri(Application.Current.Host.Source, "../DataAccess.svc")); will return "Http://server/app/DataAccess.svc" 
*/ 

System.ServiceModel.EndpointAddress address = new System.ServiceModel.EndpointAddress(new Uri(Application.Current.Host.Source, "../DataAccess.svc")); 

svcProxy_m = new svcMyService.DataAccessClient(binding, address); 
+0

+1剛剛解決了我的Silverlight部署問題。 – geofftnz 2009-02-18 04:25:20

1

我已經在使用相對URI配置和我的SL4應用程序的作品。

<configuration> 
    <system.serviceModel> 
     <bindings> 
      <basicHttpBinding> 
       <binding name="BasicHttpBinding_ICorrectionService" maxBufferSize="2147483647" 
        maxReceivedMessageSize="2147483647" textEncoding="utf-8" transferMode="Buffered"> 
        <security mode="None" /> 
       </binding> 
      </basicHttpBinding> 
     </bindings>   
     <client> 
      <endpoint address="/CorrectionService.svc" 
      binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ICorrectionService" 
      contract="CorrectionService.ICorrectionService" name="BasicHttpBinding_ICorrectionService" /> 
     </client>   
    </system.serviceModel> 
</configuration>