将WCF引用添加到C#Web应用程序

问题描述:

我在C#中有一个WCF项目(:53763/WCFTestService.svc)和一个Web应用程序(:50238/CSharp/test.aspx)。我正在尝试在我的Web应用程序中调用WCF项目方法。我已经将WCF项目添加为Web应用程序中的服务引用。将WCF引用添加到C#Web应用程序

在test.aspx.cs文件

我有一个方法如下

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

当我运行此得到错误:

找不到默认终结点元素引用合同 “TestWCFServiceReference。 IWMFTestService“在ServiceModel客户端 配置部分。这可能是因为没有配置文件 发现你的应用程序,或者是因为匹配 本合同没有终结点元素可以在客户端元素

被发现。

当我在我的Web应用程序中添加WCF项目作为服务引用时,Web应用程序中的web.config文件不会自动更新。 (在添加引用时默认添加代码)。 我是否需要在web.config文件中添加任何内容才能使其工作? WCF项目

的web.config是:

<?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> 
+0

你能分享你的WCF服务的配置吗? – 2013-05-10 10:56:03

+0

好的..我已经添加了我的WCF项目的web.config文件。 – Sudha 2013-05-10 12:32:53

+0

在您的配置文件中,您只有一个带有webHttpBinding的端点,用于宁静的地址。所以你可以参考[这里](http://sandeepkumar1234.blogspot.in/2013/04/how-to-test-restful-service.html)来配置和测试宁静的服务。 – 2013-05-11 13:02:15

这听起来像有一个错误,你会需要你的时候你引用WCF项目来解决。在添加引用时,在Visual Studio上查看信息选项卡(而不是警告/错误),您将看到错误。我猜这将与Newtonsoft.Json有关 - 有许多帖子,比如Error adding service reference: Type is a recursive collection data contract which is not supported

在你的配置文件中,你只有一个带有webHttpBinding的端点用于平静地址。所以你可以参考here来配置和测试宁静的服务。或添加另一个端点来为SOAP客户端提供服务元数据,例如

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