无法在WinForms中为WCF服务添加端点

时间:2016-07-18 13:49:38

标签: c# winforms wcf

这是我在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>

4 个答案:

答案 0 :(得分:0)

这样的事情应该有希望成功

userpath

答案 1 :(得分:0)

你可以试试这个

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

您可以像这样打电话给您的休息服务

localhost:port/Service1.svc/Pathname 

此处路径名是您在uritemplate的operationcontract中提到的。

希望它有所帮助!

答案 2 :(得分:0)

嗯,错误消息明确地在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>

答案 3 :(得分:0)

<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....