C# 为什么<;系统服务模型>;app.config中是否缺少节?

C# 为什么<;系统服务模型>;app.config中是否缺少节?,c#,.net,wcf,app-config,service-reference,C#,.net,Wcf,App Config,Service Reference,我正在尝试使用WCF服务。我添加了一个服务引用,现在我尝试调用它: BusStopServiceBinding.BusStopPortTypeClient client = new BusStopServiceBinding.BusStopPortTypeClient(); 但是,我遇到了以下错误: 找不到引用协定的默认终结点元素 ServiceModel客户端中的“BusStopServiceBinding.BusStopPortType” 配置部分。这可能是因为没有配置文件 为应用程序找到

我正在尝试使用WCF服务。我添加了一个服务引用,现在我尝试调用它:

BusStopServiceBinding.BusStopPortTypeClient client = new BusStopServiceBinding.BusStopPortTypeClient();
但是,我遇到了以下错误:

找不到引用协定的默认终结点元素 ServiceModel客户端中的“BusStopServiceBinding.BusStopPortType” 配置部分。这可能是因为没有配置文件 为应用程序找到,或者因为没有匹配的端点元素 可以在client元素中找到此合同

我的
app.config
文件如下所示:

<?xml version="1.0"?>
<configuration>
<configSections>
</configSections>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>
</configuration>


正如您所看到的,没有
serviceModel
部分。我应该手动添加它吗?如果是,我应该在其中添加什么?

本节介绍WCF配置。在VisualStudio的“工具”部分中,有一个“WCF服务配置编辑器”,可帮助您创建此部分。如果没有此部分,则必须在代码中对其进行配置,但这不是最佳做法。
在本节中,您必须将端点、安全设置、绑定、wcf合约等放在此处。

本节用于wcf配置。在VisualStudio的“工具”部分中,有一个“WCF服务配置编辑器”,可帮助您创建此部分。如果没有此部分,则必须在代码中对其进行配置,但这不是最佳做法。
在本节中,您必须将端点、安全设置、绑定、wcf契约等放到库中,而不是将服务引用添加到主项目(exe或web应用程序等),然后(通过Visual Studio工具)将必要的添加到库项目中的
app.config
,而不是在你的主要项目中


但是,在运行时,只使用主项目的
app.config
,因此您需要将库中(无用的)
app.config
的相关部分复制到主项目的库中。

如果将服务引用添加到库中,而不是添加到主项目(exe或web应用程序等)中,然后(通过Visual Studio工具)将必要的添加到库项目中的
app.config
,而不是主项目中


但是,在运行时,只使用主项目的
app.config
,因此您需要将库中(无用的)
app.config
的相关部分复制到主项目的部分。

我也有同样的错误,不信者Damien_的回答帮助我理解并解决了我的问题:“但是,在运行时,仅使用主项目的app.config”。
我在类库项目中使用wsdl代理。虽然我的UnitTest项目能够读取该类库的app.config,但在运行时该类库的app.config变得无关紧要

我的解决方案是为自动生成的客户机类和 依赖于从代码中配置的System.ServiceModel,而不是从谁知道是什么app.config中读取:

public partial class WsdlClient 
    {
        public WsdlClient (string endpointUrl, TimeSpan timeout, string username, string password) :
            base(WsdlClient.GetBindingForEndpoint(timeout), WsdlClient.GetEndpointAddress(endpointUrl))
        {
            this.ChannelFactory.Credentials.UserName.UserName = username;
            this.ChannelFactory.Credentials.UserName.Password = password;           
        }

        private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(TimeSpan timeout)
        {
            var httpsBinding = new BasicHttpsBinding();
            httpsBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
            httpsBinding.Security.Mode = BasicHttpsSecurityMode.Transport;

            var integerMaxValue = int.MaxValue;
            httpsBinding.MaxBufferSize = integerMaxValue;
            httpsBinding.MaxReceivedMessageSize = integerMaxValue;
            httpsBinding.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
            httpsBinding.AllowCookies = true;

            httpsBinding.ReceiveTimeout = timeout;
            httpsBinding.SendTimeout = timeout;
            httpsBinding.OpenTimeout = timeout;
            httpsBinding.CloseTimeout = timeout;

            return httpsBinding;
        }

        private static System.ServiceModel.EndpointAddress GetEndpointAddress(string endpointUrl)
        {
            if (!endpointUrl.StartsWith("https://"))
            {
                throw new UriFormatException("The endpoint URL must start with https://.");
            }
            return new System.ServiceModel.EndpointAddress(endpointUrl);
        }
    }


我也犯了同样的错误,不信者Damien_的回答帮助我理解并解决了我的问题:“但是,在运行时,只使用主项目的app.config”。
我在类库项目中使用wsdl代理。虽然我的UnitTest项目能够读取该类库的app.config,但在运行时该类库的app.config变得无关紧要

我的解决方案是为自动生成的客户机类和 依赖于从代码中配置的System.ServiceModel,而不是从谁知道是什么app.config中读取:

public partial class WsdlClient 
    {
        public WsdlClient (string endpointUrl, TimeSpan timeout, string username, string password) :
            base(WsdlClient.GetBindingForEndpoint(timeout), WsdlClient.GetEndpointAddress(endpointUrl))
        {
            this.ChannelFactory.Credentials.UserName.UserName = username;
            this.ChannelFactory.Credentials.UserName.Password = password;           
        }

        private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(TimeSpan timeout)
        {
            var httpsBinding = new BasicHttpsBinding();
            httpsBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
            httpsBinding.Security.Mode = BasicHttpsSecurityMode.Transport;

            var integerMaxValue = int.MaxValue;
            httpsBinding.MaxBufferSize = integerMaxValue;
            httpsBinding.MaxReceivedMessageSize = integerMaxValue;
            httpsBinding.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
            httpsBinding.AllowCookies = true;

            httpsBinding.ReceiveTimeout = timeout;
            httpsBinding.SendTimeout = timeout;
            httpsBinding.OpenTimeout = timeout;
            httpsBinding.CloseTimeout = timeout;

            return httpsBinding;
        }

        private static System.ServiceModel.EndpointAddress GetEndpointAddress(string endpointUrl)
        {
            if (!endpointUrl.StartsWith("https://"))
            {
                throw new UriFormatException("The endpoint URL must start with https://.");
            }
            return new System.ServiceModel.EndpointAddress(endpointUrl);
        }
    }


一个好的资源。请浏览它()一个好的资源。请浏览它()谢谢!在我看到你的答案之前,我已经设法手动添加了这个部分,它似乎解决了这个特定的问题:
谢谢!在我看到你的答案之前,我已经设法手动添加了这个部分,它似乎解决了这个特定的问题:
是的,这是我在网上找到的答案,但是在我的案例只有一个项目。无论如何,问题解决了。是的,这是我在网上找到的答案,但在我的案例中只有一个项目。无论如何,问题解决了。