C# IIS Hosted WCF 服务不生成客户端端点 [英] C# IIS Hosted WCF service doesn't generate client endpoints

查看:24
本文介绍了C# IIS Hosted WCF 服务不生成客户端端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Visual Studio 2013 中使用 C# 创建了一个 WCF 服务,并将其托管在 IIS 服务器上.我在新的 WPF 客户端中使用该服务时遇到问题,因为在添加服务引用后缺少端点.

我可以使用 URL 访问该服务,该 URL 为我提供了说明页面,说明我需要创建一个客户端才能测试该服务.该 URL 适用于服务器和我的笔记本电脑.WSDL 确实存在,而且我能够查看 XML 结构.Visual Studio 允许我使用 WSDL URL 添加服务引用,并正确生成代理客户端代码.

添加服务引用不会向我客户端的 App.Config 文件添加任何端点.WSDL 中的 Ctrl-F 显示 WSDL 中没有任何端点数据.由不同部门托管的另一个服务的 WSDL 确实包括它们的端点,所以我知道它们应该以这种方式列出.

我可以做些什么来让 WSDL 自动向客户端添加端点配置?我不想记住每个新客户端的端点,或者要求其他人每次都用服务来问我.

根据 这篇 MSDN 文章,我尝试添加一个部分并重复部分中的端点,重建我的服务,重新启动IIS服务器上的主机网站,并更新我的服务引用,但端点仍未添加.

当我使用添加服务引用"对话框中的发现"选项时,它确实找到了服务项目,但给出了一个错误,指出 URL 不正确.另一个仅包含样板服务信息的测试解决方案适用于该解决方案中的客户端项目,并将端点添加到客户端配置.它的 Web.Config 中没有我遇到问题的服务没有的任何内容.

可能重复这个问题,但没有得到解答.

这是我的主机网站上 Web.Config 的相关部分(服务的 App.Config 是相同的):

<客户><端点绑定=webHttpBinding"contract="TestLib.Service.ITestLibService"/></客户端><服务><服务名称="TestLib.Service.TestLibManager"><endpoint binding="webHttpBinding" contract="TestLib.Service.ITestLibService"/><endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/></服务></服务><绑定><webHttpBinding><绑定><安全模式="TransportCredentialOnly"><传输 clientCredentialType="Windows" proxyCredentialType="Windows"/></安全></binding></webHttpBinding></绑定><行为><服务行为><行为><serviceDebug includeExceptionDetailInFaults="true"/><serviceMetadata httpGetEnabled="true"/></行为></serviceBehaviors></行为>

感谢任何帮助,谢谢!

解决方案

答案:使用基于 SOAP 的端点绑定,而不是基于 REST 的

端点绑定有两种不同的类型:SOAP 和 REST.只要它们具有不同的地址,您的服务就可以将它们包含在 web.config 文件中,但只有 SOAP 端点会在 WSDL 中列出.不需要额外的设置或配置,只要它是受支持的类型,服务就会自动获取这些信息.

webHttpBinding 是一种基于 REST 的端点绑定.它适用于使用该服务的 JSON/JavaScript 客户端,但不会在 WSDL 中列出.

basicHttpBinding 是一种基于 SOAP 的端点绑定.它适用于 C# 客户端.如果您使用 Visual Studio 添加服务引用,它会自动将这些端点添加到您的 app.config 文件中,因为该信息已列在 WSDL 中,因此它可以执行此操作.

感谢@nodots 让我指明了正确的方向.

I have created a WCF service using C# in Visual Studio 2013, and hosted it on an IIS server. I am having trouble consuming the service in a new WPF client because endpoints are missing after I add the service reference.

I can access the service using the URL, which gives me the instructional page stating that I'll need to create a client in order to test the service. The URL works on both the server and my laptop. The WSDL does exist, and I'm able to view the XML structure. Visual Studio lets me add the Service Reference using the WSDL URL, and proxy client code is generated properly.

Adding the service reference does not add any endpoints to my client's App.Config file. Ctrl-F in the WSDL shows that there isn't any endpoint data in the WSDL. The WSDL for another service hosted by a different department does include their endpoints, so I know that they should be listed this way.

Is there something that I can do to make the WSDL add endpoint configurations to the client automatically? I don't want to have to remember the endpoint for each new client, or to require others who use the service to ask me for it each time.

Per this MSDN article I've tried adding a section and repeating the endpoint in the section, rebuilt my service, restarted the host website on the IIS server, and updated my service reference, but the endpoint still hasn't been added.

When I use the Discover option in the Add Service Reference dialog box it does find the service project, but gives an error saying that the URL is incorrect. Another test solution with just the boilerplate service information works fine for a client project in that solution, and adds the endpoint to the client config. It doesn't have anything in it's Web.Config that the service I'm having trouble with doesn't have.

Possible repeat of this question, but it went unanswered.

Here's the relevant part of Web.Config from my host website (the App.Config of the service is identical):

<system.serviceModel>
<client>
  <endpoint binding="webHttpBinding"
            contract="TestLib.Service.ITestLibService" />
</client>
<services>
  <service name="TestLib.Service.TestLibManager">
    <endpoint binding="webHttpBinding" contract="TestLib.Service.ITestLibService" />
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
  </service>
</services>
<bindings>
  <webHttpBinding>
    <binding>
      <security mode="TransportCredentialOnly">
        <transport clientCredentialType="Windows" proxyCredentialType="Windows"/>
      </security>          
    </binding>
  </webHttpBinding>
</bindings>
<behaviors>
  <serviceBehaviors>
    <behavior>
      <serviceDebug includeExceptionDetailInFaults="true" />
      <serviceMetadata httpGetEnabled="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>

Any help is appreciated, thanks!

解决方案

Answer: Use SOAP-based endpoint bindings, not REST-based

Endpoint bindings come in two different types: SOAP and REST. You're service can include both in the web.config file as long as they have different addresses, but only SOAP endpoints will be listed in the WSDL. There are no additional settings or configurations needed, the service will pick up this information automatically as long as it's of the supported type.

webHttpBinding is a REST-based endpoint binding. It will work fine for JSON/JavaScript clients consuming the service, but will not be listed in the WSDL.

basicHttpBinding is a SOAP-based endpoint binding. It will work well for C# clients. If you use Visual Studio to add the Service Reference, it will automatically add these endpoints to your app.config file, something it can do because that information is listed in the WSDL.

Thanks to @nodots for getting me pointed in the right direction.

这篇关于C# IIS Hosted WCF 服务不生成客户端端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆