wcf服务绑定中的相对URL

Jer*_*emy 10 .net silverlight wcf

我有一个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>
Run Code Online (Sandbox Code Playgroud)

如何在端点地址中指定相对URL而不是绝对URL?无论我在何处部署Web应用程序而无需编辑clientconfig文件,我希望它能够正常工作,因为silverlight组件和Web应用程序将始终一起部署.我以为我只能指定"DataAccess.svc",但它似乎并不喜欢.

Jer*_*emy 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);
Run Code Online (Sandbox Code Playgroud)


Str*_*lok 4

您不能在客户端端点配置中使用相对 URI。您可以做的只是向代理类添加另一个构造函数,该构造函数将采用某种 URL 参数,您可以从另一个配置值获取该参数或使用 Dns 类方法之一。