.net - wcf 服务绑定(bind)中的相对 url

标签 .net wcf silverlight

我有一个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/

相关文章:

c# - 在ASP.NET MVC 4应用程序中检查SSL协议(protocol),密码和其他属性

c# - 自定义 WCF channel 在 ChannelBase.EndInvoke 上引发 NullReferenceException

c# - WCF 客户端如何了解服务器类型

Silverlight数据网格无法显示数据

c# - 使用反射初始化对象的自动属性——有什么注意事项吗?

c# - 找出哪个控件具有焦点

.net - FluentMigrator迁移成功,但数据库没有变化

c# - 引用基于编译器开关的 .NET 程序集

c# - 如何使用 Orchard 使用的程序集的新版本

c# - WCF契约不匹配问题