WCF中此异常的原因

Won*_*ing 3 c# wcf

我正在学习WCF,现在我在运行应用程序时遇到一个异常:异常:

无法在ServiceModel客户端配置部分中找到引用合同"IService1"的默认端点元素.这可能是因为没有为您的应用程序找到配置文件,或者因为在客户端元素中找不到与此合同匹配的端点元素.

服务代码:

namespace StockService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }

            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }

            return composite;
        }

        public string  GetCompositedata()
        {
           CompositeType ct = new CompositeType();
           return ct.StringValue;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

web.config中:

<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)

现在使用svcutil我已生成proxyclass(generatedProxy.cs)和配置文件(serviceapp.config)并将其添加到控制台应用程序(客户端)

客户:

Service1Client sc = new Service1Client();
Console.WriteLine(sc.GetCompositedata());
Console.ReadKey();
Run Code Online (Sandbox Code Playgroud)

配置:

<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IService1" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                    useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <security mode="None">
                        <transport clientCredentialType="None" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
                    </security>
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:2614/Service1.svc" binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_IService1" contract="IService1"
                name="BasicHttpBinding_IService1" />
        </client>
    </system.serviceModel>
</configuration>
Run Code Online (Sandbox Code Playgroud)

我无法弄清楚为什么我会得到这个例外.

请帮忙

mar*_*c_s 6

是否为客户端应用程序的客户端部分显示该配置,app.config或者web.config,如果它是一个网站/ Web应用程序?

您需要在应用程序的配置中包含这些部分 - 将这些配置放在由其创建的单独文件中是不够的svcutil.exe- 它需要成为应用程序配置的一部分.