如何以编程方式修改WCF

如何以编程方式修改WCF

本文介绍了如何以编程方式修改WCF app.config中的端点地址设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以编程方式修改我的app.config文件来设置它的服务端点文件应该使用。什么是在运行时做到这一点的最好方法是什么?供参考:

 <端点地址=HTTP://mydomain/MyService.svc
    绑定=的wsHttpBindingbindingConfiguration =WSHttpBinding_IASRService
    合同=ASRService.IASRServiceNAME =WSHttpBinding_IASRService>
    <同一性GT;
        < D​​NS值=本地主机/>
    < /身份>
< /端点>


解决方案

我想你想要的是在运行时版本的配置文件中换出,如果是创建配置文件的副本(也给它的相关扩展喜欢的.debug或.Release),有正确的地址(它给你一个调试版本和运行时版本),并创建拷贝正确的文件取决于生成类型,一个postbuild一步。

下面是我在它覆盖了正确的版本(调试/运行)

输出文件过去使用的postbuild事件示例

 复制$(PROJECTDIR)ServiceReferences.ClientConfig。$(ConfigurationName)$(PROJECTDIR)ServiceReferences.ClientConfig/ Y

其中:
$(PROJECTDIR)是项目目录所在的配置文件位于
$(ConfigurationName)是主动配置构建类型

编辑:
请参阅马克的回答关于如何做到这一点编程的详细说明。

I'd like to programmatically modify my app.config file to set which service file endpoint should be used. What is the best way to do this at runtime? For reference:

<endpoint address="http://mydomain/MyService.svc"
    binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IASRService"
    contract="ASRService.IASRService" name="WSHttpBinding_IASRService">
    <identity>
        <dns value="localhost" />
    </identity>
</endpoint>
解决方案

I think what you want is to swap out at runtime a version of your config file, if so create a copy of your config file (also give it the relevant extension like .Debug or .Release) that has the correct addresses (which gives you a debug version and a runtime version ) and create a postbuild step that copies the correct file depending on the build type.

Here is an example of a postbuild event that I've used in the past which overrides the output file with the correct version (debug/runtime)

copy "$(ProjectDir)ServiceReferences.ClientConfig.$(ConfigurationName)" "$(ProjectDir)ServiceReferences.ClientConfig" /Y

where :$(ProjectDir) is the project directory where the config files are located$(ConfigurationName) is the active configuration build type

EDIT:Please see Marc's answer for a detailed explanation on how to do this programmatically.

这篇关于如何以编程方式修改WCF app.config中的端点地址设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 02:04