【问题标题】:Cannot add endpoint for WCF service in WinForms无法在 WinForms 中为 WCF 服务添加端点
【发布时间】:2016-11-21 03:56:09
【问题描述】:

这是我在 WCF 服务中的Web.config

    <?xml version="1.0" encoding="utf-8"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="Mobile55.Service1">
        <endpoint address="../Service1.svc"
          binding="webHttpBinding"
          contract="Mobile55.IService1"
          behaviorConfiguration="webBehaviour" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="webBehaviour">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
   <httpProtocol>
     <customHeaders>
       <add name="Access-Control-Allow-Origin" value="*" />
       <add name="Access-Control-Allow-Headers" value="Content-Type, Accept" />
     </customHeaders>
   </httpProtocol>
   <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

这是我第一次使用 WCF,我无法在我的 WinForms 应用程序中为此服务添加端点。

我添加了这个服务作为参考,我可以访问这些方法,但是当我调试并尝试调用一个方法时,它会抛出一个异常:

Could not find default endpoint element that references contract 'MyService.IService1' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.

我的应用程序app.config 文件为空。

我尝试了以下答案,但对我不起作用:

Could not find default endpoint element

Could not find default endpoint element that references contract - Hosting wcf

WCF Error - Could not find default endpoint element that references contract 'UserService.UserService'

如何添加端点以最终能够使用我的服务?

提前致谢。

编辑:我的app.config 文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
  <service name="Mobile55.Service1">
    <endpoint address=""
      binding="webHttpBinding"
      contract="Mobile55.IService1"
      behaviorConfiguration="webBehaviour" />
  </service>
</services>

<behaviors>
  <serviceBehaviors>
    <behavior name="webBehaviour">
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>

<client>
  <endpoint address="http://localhost:1008/Service1.svc"
    binding="webHttpBinding"
    contract="Mobile55.IService1"
    behaviorConfiguration="webBehaviour" />
</client>
</system.serviceModel>  
</configuration>

【问题讨论】:

    标签: c# winforms wcf


    【解决方案1】:

    嗯,错误消息在client configuration section 中特别说明了。 因此,错误发生在客户端,即来自您尝试在服务上调用的代码 - 如果没有适当的异常/堆栈跟踪和展示该行为的完整/可编译示例,则很难判断。

    无论如何,您的配置文件完全缺少client 部分。

    添加如下内容:

    <system.serviceModel>
        <!-- ... other stuff you have ... -->
        <client>
            <endpoint address="../Service1.svc"
              binding="webHttpBinding"
              contract="Mobile55.IService1"
              behaviorConfiguration="webBehaviour" />
        </client>
    </system.serviceModel>
    

    【讨论】:

    • 谢谢。但是现在添加此部分后,我得到了这个:There is no endpoint behavior named 'webBehaviour'
    • 您是否真的将client 元素添加到您的system.serviceModel 部分,还是用我写的内容替换了整个部分?
    • 我在上面添加了我的 app.config 内容。可以看看吗?
    • 这看起来和你最初的完全不同?!您不再拥有 address 属性的值,并且“webBehaviour”有所不同。请考虑(再次)编辑您的问题,使其仅反映当前/想要的事物状态,以及当前(完整)错误消息以及可编译代码,以便我们可以重现您的问题。在那之前,我放弃了。
    • 在我的问题中,我发布了我的 WCF 服务的 Web.config 文件,但现在我添加了我的 win 应用程序的 app.configWeb.config 文件仍然和以前一样,当我尝试连接时,当前错误是 There is no endpoint behavior named 'webBehaviour'
    【解决方案2】:

    这样的事情应该可以解决问题

      <service name="MyService.Service1">
        <endpoint address="YourEndPointHere"
          binding="webHttpBinding"
          contract="MyService.IService1"
          behaviorConfiguration="webBehaviour" />
      </service>
    

    【讨论】:

      【解决方案3】:

      你可以试试这个

       <service name="Mobile55.Service1">
              <endpoint address=""
                binding="webHttpBinding"
                contract="Mobile55.IService1"
                behaviorConfiguration="webBehaviour" />
            </service>
      

      你可以这样调用你的休息服务

      localhost:port/Service1.svc/Pathname 
      

      这里的路径名是你在uritemplate的操作合同中提到的。

      希望对你有帮助!

      【讨论】:

        【解决方案4】:
        <system.serviceModel>
        <client>
        <endpoint address="http://localhost:1008/Service1.svc" 
        binding="webHttpBinding"
        bindingConfiguration="webHttpBinding_IService"  
        contract="Mobile55.IService1"
        name="webHttpBinding_IService" />
        </client>
        </system.serviceModel>
        
        
        ----------
        ## Heading ##
        
        
        Try....
        

        【讨论】:

        • web.configapp.config?
        猜你喜欢
        • 1970-01-01
        • 2014-04-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-20
        • 2011-12-23
        • 2014-05-08
        • 1970-01-01
        相关资源
        最近更新 更多