zoukankan      html  css  js  c++  java
  • Silverlight动态设置WCF服务Endpoint

    去年12月收到一位朋友的邮件,咨询Silverlight使用WCF服务,应用部署后一直无法访问的问题,通过几次交流,才发现在他的项目中,全部使用静态URL作为WCF服务的Endpoint地址,后来修改为动态地址后,问题解决。本篇简单介绍如何创建动态WCF Endpoint。

    Silverlight项目中通过WCF服务进行客户端与服务器端数据交互,其方法是在Silverlight客户端添加服务引用(Service Reference),Silverlight会在客户端项目中生成一个名为ServiceReference.ClientConfig的配置文件。该配置文件中包含有WCF服务的Endpoint URL地址,而该地址将指引Silverlight客户端服务请求到对应服务器端服务接口。默认情况下,客户端生成的Endpoint地址为localhost,当应用发布时,该地址将随着Web服务器的配置不同而改变。

    作为默认设置,每次Web服务器配置切换,开发人员不得不手动修改WCF服务配置,Silverlight应用无法完全依赖于ServiceReference.ClientConfig配置。这样不仅增加了维护成本,而且为代码维护添加难度。 

    首先在项目中添加WCF服务,Silverlight 默认生成ServiceReference.ClientConfig文件,从配置文件中可以看出,Endpoint都是指向本地。

    <configuration>
        <system.serviceModel>
            <bindings>
                <basicHttpBinding>
                    <binding name="BasicHttpBinding_ProxyService" maxBufferSize="2147483647"
                        maxReceivedMessageSize="2147483647">
                        <security mode="None" />
                    </binding>
                    <binding name="BasicHttpBinding_WidgetService" maxBufferSize="2147483647"
                        maxReceivedMessageSize="2147483647">
                        <security mode="None" />
                    </binding>
                </basicHttpBinding>
            </bindings>
            <client>
                <endpoint address="http://localhost/WCFTest/Proxy.svc"
                    binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ProxyService"
                    contract="WCFTestProxy.ProxyService" name="BasicHttpBinding_ProxyService" />
                <endpoint address="http://localhost/WCFTest/Widget.svc"
                    binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_WidgetService"
                    contract="WCFTestWidgetService.WidgetService" name="BasicHttpBinding_WidgetService" />
            </client>
        </system.serviceModel>
    </configuration>

    为了实现动态设置Endpoint,下面将创建一个辅助类DynamicEndpointHelper,

    public class DynamicEndpointHelper
    {
        // BaseUrl是部署服务的Web服务器地址
        private const string BaseUrl = "http://localhost/WCFTest/";
    
        public static string ResolveEndpointUrl(string endpointUrl, string xapPath)
        {
            string baseUrl = xapPath.Substring(0, xapPath.IndexOf("ClientBin"));
            string relativeEndpointUrl = endpointUrl.Substring(BaseUrl.Length);
            string dynamicEndpointUrl = baseUrl + relativeEndpointUrl;
            return dynamicEndpointUrl;
        }
    }

    而在Silverlight客户端,可以通过后台代码实现动态设置Endpoint,

    private WCFTestProxy.ProxyServiceClient GetProxyService()
    {
        WCFTestProxy.ProxyServiceClient service = new WCFTestProxy.ProxyServiceClient();
        service.Endpoint.Address = new EndpointAddress(
            DynamicEndpointHelper.ResolveEndpointUrl(service.Endpoint.Address.Uri.ToString(),
            App.Current.Host.Source.ToString()));
        return service;
    }

    在创建完成代理客户端服务后,Endpoint地址将指向当前运行的Web地址。值得注意的是,该方法仅能支持应用与服务在相同域中,如果服务在不同的域中,必须进行跨域设置才能完成动态设置。

  • 相关阅读:
    linux下的grep命令
    linux下的ps命令
    删除eclipse无效的工作空间路径
    js中 var functionName = function() {} 和 function functionName() {} 两种函数声明的区别
    javascript中的function 函数名(){} 和 函数名:function(){}有什么不同
    Python之路-python(css布局、JavaScript)
    Python之路-python(css、JavaScript)
    Python之路-python(html、css)
    Python之路-python(堡垒机)
    Python之路-python(mysql介绍和安装、pymysql、ORM sqlachemy)
  • 原文地址:https://www.cnblogs.com/jv9/p/2889738.html
Copyright © 2011-2022 走看看