如何从.ClientConfig文件中删除Web服务地址的硬编码

时间:2014-03-25 04:26:08

标签: silverlight asp.net-mvc-4

我的ASP .NET MVC 4应用程序中有5个SilverLight项目。对于这些项目中的每一个,都有一个Web服务,可以将数据传入和传出Silverlight控件。

当我们在客户端的服务器上部署应用程序时,我们通过更改Web服务的地址来更新每个SilverLight项目的.ClientConfig文件。

那就是我们改变 -

<endpoint address="http://localhost:52213/SchemeReimbursementMasterService.asmx"
            binding="basicHttpBinding" bindingConfiguration="SchemeReimbursementMasterServiceSoap"
            contract="SchemeServiceReference.SchemeReimbursementMasterServiceSoap"
            name="SchemeReimbursementMasterServiceSoap" />

到 -

<endpoint address="http://192.168.5.48/SCHEME/SchemeReimbursementMasterService.asmx"
            binding="basicHttpBinding" bindingConfiguration="SchemeReimbursementMasterServiceSoap"
            contract="SchemeServiceReference.SchemeReimbursementMasterServiceSoap"
            name="SchemeReimbursementMasterServiceSoap" />

然后我们构建它并发布应用程序。

因此,将来,如果地址发生变化,我们将不得不在所有五个.ClientConfig文件中更改它并再次构建/发布它。有没有办法我们可以删除这个硬编码,以便它自己找出网址的基本部分(即:http://192.168.5.48/SCHEME /)。

3 个答案:

答案 0 :(得分:0)

尝试使用WCF 4的发现和服务宣传功能或代理服务,这是一个链接 - WCF 4 new features

答案 1 :(得分:0)

你必须重写Url,有很多方法。其中一个是

阅读这篇文章Automating Dev, QA, Staging, and Production Web.Config Settings

答案 2 :(得分:0)

在我的应用程序web.config中,我添加了服务名称的密钥。

<appSettings>
    <add key="ReviewUrl" value="ReviewClaimsService.asmx"/>
    <add key="ReimbursementUrl" value="SchemeReimbursementMasterService.asmx"/>
    <add key="DiscountUrl" value="DiscountMasterSchemeService.asmx"/>
</appSettings>

在控制器中,我使用HttpContextweb.config中的服务名称创建了网址,并将其传递给视图。

var baseUrl = "http://" + HttpContext.Request.Headers["Host"];
var applicationPath = HttpContext.Request.ApplicationPath;
if (applicationPath == "/")
    baseUrl = baseUrl + "/";
var url = baseUrl + ConfigurationManager.AppSettings["ReimbursementUrl"];

在视图中,我使用silverlight将此网址传递给InitParams控件。

<object id="TownsGrid" data="data:application/x-silverlight-2," type="application/x-silverlight-2">
    <param name="InitParams" value="ControlName=TownDataGrid,ServiceUrl=@Model.ServiceUrl" />
    <param name="source" value="@Url.Content("~/ClientBin/SilverlightApplication.xap")"/>
</object>

在silverlight项目的Application_Startup方法中,我将URL传递给SilverLight控件。

private void Application_Startup(object sender, StartupEventArgs e)
{
    var url = e.InitParams["ServiceUrl"];
    this.RootVisual = new SilverlightApplication.TownDataGrid(url);
}

最后,在silverlight控件中,我将url分配给端点。

public TownDataGrid(string serviceUrl)
{
    InitializeComponent();
    EndpointAddress endpoint = new EndpointAddress(serviceUrl);
    BasicHttpBinding binding = new BasicHttpBinding();
    client = new SchemeReimbursementMasterServiceSoapClient(binding,endpoint);
}