向c# web应用程序添加WCF引用

本文关键字:WCF 引用 添加 应用程序 web | 更新日期: 2023-09-27 18:12:54

我有一个WCF项目(:53763/WCFTestService.svc)和一个c#的web应用程序(:50238/CSharp/test.aspx)。我试图在我的web应用程序中调用WCF项目方法。我已经添加了WCF项目作为web应用程序的服务参考。

在test.aspx.cs文件我有一个方法如下

[WebMethod()]
    public static string GetWCF()
    {
        WCFTestServiceClient client = new WCFTestServiceClient();
        string result = client.XMLData("1122");
        return result;
    }

当我运行这个获取错误时:

找不到引用契约的默认端点元素"TestWCFServiceReference。在ServiceModel客户端中的IWCFTestService配置部分。这可能是因为没有配置文件为您的应用程序找到,或者因为没有端点元素匹配该合同可以在client元素

中找到。

.

当我在我的web应用程序中添加WCF项目作为服务引用时,web。web应用程序中的配置文件没有自动更新。(默认情况下,在添加引用时不会添加任何代码)。我需要在网上添加任何东西。配置文件使其工作?

网络。WCF项目的配置为:

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="WCFTestService.RestService" behaviorConfiguration="ServiceBehaviour">
        <endpoint address="" binding="webHttpBinding" contract="WCFTestService.IRestService" behaviorConfiguration="web">          
        </endpoint>
      </service>      
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviour">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
        <!--<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>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

向c# web应用程序添加WCF引用

这听起来像有一个错误,你需要解决当你引用你的WCF项目。当您添加引用时,请查看Visual Studio上的信息选项卡(不是警告/错误),您将看到错误。我猜这和牛顿软件有关。Json -有很多关于它的帖子,比如错误添加服务引用:类型是一个不支持的递归集合数据契约

在你的配置文件中,你只有一个webHttpBinding端点,用于restful地址。因此,您可以参考此处配置和测试restful服务。或者添加另一个端点为SOAP客户端提供服务元数据,例如

     <endpoint address="" binding="basicHttpBinding" contract="WCFTestService.IRestService"                      bindingNamespace="">
          </endpoint>