【问题标题】:relative url in wcf service bindingwcf 服务绑定中的相对 url
【发布时间】:2008-12-16 23:55:17
【问题描述】:

我有一个 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”,但它似乎不喜欢那样。

【问题讨论】:

    标签: .net wcf silverlight


    【解决方案1】:

    我的解决方案:

    我没有使用 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);
    

    【讨论】:

    • 我认为这里描述的行为将是默认的 silverlight 行为。我很困惑,我们需要为每个项目都这样做。 :(
    【解决方案2】:

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

    【讨论】:

    • 如果我采用这种方法,我会将我的相对 url 参数放在哪里?我正在查看 ServiceReferences.ClientConfig,但我没有看到可以放置它的地方 - 它似乎没有与 web.config 文件相同的元素。
    【解决方案3】:

    我在配置中使用了相对 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>
    

    【讨论】:

      猜你喜欢
      • 2015-10-15
      • 1970-01-01
      • 2011-09-07
      • 1970-01-01
      • 1970-01-01
      • 2012-11-18
      • 1970-01-01
      • 1970-01-01
      • 2012-01-11
      相关资源
      最近更新 更多