我有一个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>
如何在端点地址中指定相对 url 而不是绝对 url?无论我将 Web 应用程序部署到何处,我都希望它能够工作,而无需编辑 clientconfig 文件,因为 silverlight 组件和 Web 应用程序将始终一起部署。我以为我可以只指定“DataAccess.svc”,但它似乎不喜欢那样。
最佳答案
我的解决方案:
我没有使用 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);
关于.net - wcf 服务绑定(bind)中的相对 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/373219/