relative url in wcf service binding

2020-08-19 02:36发布

I have a silverlight control which has a reference to a silverlight enabled wcf service.

When I add a reference to the service in my silverlight control, it adds the following to my clientconfig file:

<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>

How do I specify a relative url in the endpoint address instead of the absolute url? I want it to work no matter where I deploy the web app to without having to edit the clientconfig file, because the silverlight component and the web app will always be deployed together. I thought I'd be able to specify just "DataAccess.svc" but it doesn't seem to like that.

3条回答
一纸荒年 Trace。
2楼-- · 2020-08-19 02:46

You can't use relative URIs in client endpoint configuration. What you can do is just add another constructor to your proxy class that will take some sort of URL parameter that you can perhaps get from another config value or use one of the Dns class methods.

查看更多
一夜七次
3楼-- · 2020-08-19 02:46

My solution:

Instead of using the devault constructor (which uses the ServiceReferences.ClientConfig file) to instantiate my proxy class, I use the following:

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);
查看更多
smile是对你的礼貌
4楼-- · 2020-08-19 02:56

I have used relative uri in configuration and my SL4 app works.

<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>
查看更多
登录 后发表回答