C# 如何获取WCF服务的配置

C# 如何获取WCF服务的配置,c#,web-services,wcf,C#,Web Services,Wcf,在尝试创建端点之前,我想检查端点是否已在配置中定义 System.ServiceModel.ClientBase<T> sourceClient = new System.ServiceModel.ClientBase<T>(EndpointName, new EndpointAddress(discoveryUri)); System.ServiceModel.ClientBase sourceClient=new System.ServiceModel.Client

在尝试创建端点之前,我想检查端点是否已在配置中定义

System.ServiceModel.ClientBase<T> sourceClient = new System.ServiceModel.ClientBase<T>(EndpointName, new EndpointAddress(discoveryUri));
System.ServiceModel.ClientBase sourceClient=new System.ServiceModel.ClientBase(EndpointName,newendpointaddress(discoveryUri));

要检查它,我需要获得一个配置,但如何定义我是否需要使用
WebConfigurationManager
ConfigurationManager
。有没有办法定义WCF服务的托管位置?

您需要自己拥有这些信息-WCF服务本身不知道它是托管在IIS中还是自托管。毕竟,它只是一个
ServiceHost
实例

所以我想你必须有一些设置,你可以把它们放进
web.config
app.config
-类似于:

<add key="WCFHost" value="IIS" />
这将包含当前AppDomain配置文件的完整路径-如果是web应用程序,则在末尾将包含路径+
web.config

如果你查一下

if(Path.GetFileName(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile) == "web.config")
你可以非常确定你在一个web应用程序中,你有一个
web.config
可以查看

AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
if(Path.GetFileName(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile) == "web.config")