我有一个silverlight控件,它引用了支持silverlight的wcf服务。
当我在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>
如何在端点地址中指定相对网址而不是绝对网址?无论我在何处部署Web应用程序而无需编辑clientconfig文件,我希望它能够正常工作,因为silverlight组件和Web应用程序将始终一起部署。我以为我只能指定“DataAccess.svc”,但似乎并不喜欢。
答案 0 :(得分: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);
答案 1 :(得分:4)
您无法在客户端端点配置中使用相对URI。你可以做的只是在代理类中添加另一个构造函数,它将采用某种URL参数,你可以从另一个配置值中获取或使用其中一个Dns类方法。
答案 2 :(得分: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>