locked
Error encountered in WCF service. RRS feed

  • Question

  • User1351755639 posted

    I am getting an error in my WCF application, please guide me to solve this issue. Here I have tried to implement an autocomplete textbox getting populated from database using WCF service. I am also posting my code for referance.

    Error encountered:

    "The contract name 'IMetadataExchange' could not be found in the list of contracts implemented by the service Service.  Add a ServiceMetadataBehavior to the configuration file or to the ServiceHost directly to enable support for this contract. "

    Thanks in advance.

    Dipak

    ASPX File
    ------------
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default7.aspx.cs" Inherits="Default7" %>
    <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <style type="text/css">
     
    .AutoExtender
     {
     font-family: Verdana, Helvetica, sans-serif;
     font-size: .8em;
     font-weight: normal;
     border: solid 1px #006699;
     line-height: 20px;
     padding: 10px;
     background-color: White;
     margin-left:10px;
     }
     .AutoExtenderList
     {
     border-bottom: dotted 1px #006699;
     cursor: pointer;
     color: Maroon;
     }
     .AutoExtenderHighlight
     {
     color: White;
     background-color: #006699;
     cursor: pointer;
     }
     #divwidth
     {
     width: 150px !important;
     }
     #divwidth div
     {
     width: 150px !important;
     }
     
    </style>
    
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
            </cc1:ToolkitScriptManager>
        <div ID="divwidth"></div>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <cc1:autocompleteextender runat="server" id="AutoComplete1" behaviorid="autoComplete"
                targetcontrolid="TextBox1" servicepath="Service.svc" servicemethod="GetEmpInfoWith_WCF"
                minimumprefixlength="1" completioninterval="10" enablecaching="true" completionsetcount="12"
                completionlistcssclass="AutoExtender" completionlistitemcssclass="AutoExtenderList"
                completionlisthighlighteditemcssclass="AutoExtenderHighlight" completionlistelementid="divwidth" />
        </div>
        </form>
    </body>
    </html>
    
    Service.cs
    -----------------
    using System;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.ServiceModel.Activation;
    using System.ServiceModel.Web;
    using System.Web.Services;
    using System.Data;
    using System.Data.SqlClient;
    
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Service
    {
        // Add [WebGet] attribute to use HTTP GET
        [OperationContract]
        public string[] GetEmpInfoWith_WCF(string prefixText)
        {
            SqlConnection con = new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;User ID=sa;Initial Catalog=TEST_DB;Data Source=DELL-PC");
    
            string sql = "select state_name from tbl_state Where state_name like @myParameter";
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            da.SelectCommand.Parameters.Add("@myParameter", SqlDbType.VarChar, 50).Value = prefixText + "%";
            
            DataTable dt = new DataTable();
            da.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                string[] items = new string[dt.Rows.Count];
                int i = 0;
                foreach (DataRow dr in dt.Rows)
                {
                    items.SetValue(dr["state_name"].ToString(), i);
                    i++;
                }
                return items;
            }
            else
            {
    
                string[] items = new string[1];
    
                items.SetValue("No State Found", 0);
    
                return items;
    
            }
    
        }
    
    
    }
    
    Web.config
    ------------------
    <?xml version="1.0"?>
    <!--
      For more information on how to configure your ASP.NET application, please visit
      http://go.microsoft.com/fwlink/?LinkId=169433
      -->
    <configuration>
    	<connectionStrings>
      <add name="TEST_DBConnectionString" connectionString="Data Source=dell-pc;Initial Catalog=TEST_DB;Integrated Security=True"
       providerName="System.Data.SqlClient" />
     </connectionStrings>
     <system.web>
        <sessionState timeout="1440"></sessionState>
      
    		<compilation debug="true" targetFramework="4.0">
    			<assemblies>
    				<add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
    				<add assembly="System.Web.Extensions.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    				<add assembly="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/></assemblies></compilation>
    	</system.web>
    	<appSettings>
    	<add key="autocomp_ws.AutoComplete" value="http://localhost/WebSite3/AutoComplete.asmx"/>
    	</appSettings><system.serviceModel>
      <behaviors>
       <endpointBehaviors>
        <behavior name="ServiceAspNetAjaxBehavior">
          <!--<serviceMetadata httpGetEnabled="true" />-->
          <!--<serviceDebug includeExceptionDetailInFaults="true"/>-->
          <enableWebScript />
        </behavior>
       </endpointBehaviors>
      </behaviors>
      <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
       multipleSiteBindingsEnabled="true" />
      <services>
        
    
        <service name="Service">
        <endpoint address="" behaviorConfiguration="ServiceAspNetAjaxBehavior"
         binding="webHttpBinding" contract="Service" />
          <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
    
          <host>
            <baseAddresses>
              <add baseAddress="http://localhost/WebSite3/" />
            </baseAddresses>
          </host>
          </service>
      </services>
     </system.serviceModel>
    </configuration>
    
    


       

    Sunday, January 13, 2013 6:44 PM

All replies

  • User1351755639 posted

    Thanx for the reply, links checked, but still getting error, i think there is some mistake/missing in web.config file can u be plz guide to solve this problem.

    Thanx,

    Dipak

    Error:

    "The contract name 'IMetadataExchange' could not be found in the list of contracts implemented by the service Service.  Add a ServiceMetadataBehavior to the configuration file or to the ServiceHost directly to enable support for this contract. "

    Web.config:-

    <?xml version="1.0"?>
    <!--
      For more information on how to configure your ASP.NET application, please visit
      http://go.microsoft.com/fwlink/?LinkId=169433
      -->
    <configuration>
    	<connectionStrings>
      <add name="TEST_DBConnectionString" connectionString="Data Source=dell-pc;Initial Catalog=TEST_DB;Integrated Security=True"
       providerName="System.Data.SqlClient" />
     </connectionStrings>
     <system.web>
        <sessionState timeout="1440"></sessionState>
      
    		<compilation debug="true" targetFramework="4.0">
    			<assemblies>
    				<add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
    				<add assembly="System.Web.Extensions.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    				<add assembly="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/></assemblies></compilation>
    	</system.web>
    	<appSettings>
    	<add key="autocomp_ws.AutoComplete" value="http://localhost/WebSite3/AutoComplete.asmx"/>
    	</appSettings><system.serviceModel>
      <behaviors>
       <endpointBehaviors>
        <behavior name="ServiceAspNetAjaxBehavior">
          <!--<serviceMetadata httpGetEnabled="true" />-->
          <!--<serviceDebug includeExceptionDetailInFaults="true"/>-->
          <enableWebScript />
        </behavior>
       </endpointBehaviors>
      </behaviors>
      <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
       multipleSiteBindingsEnabled="true" />
      <services>
        
    
        <service name="Service">
        <endpoint address="" behaviorConfiguration="ServiceAspNetAjaxBehavior"
         binding="webHttpBinding" contract="Service" />
          <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
    
          <host>
            <baseAddresses>
              <add baseAddress="http://localhost/WebSite3/" />
            </baseAddresses>
          </host>
          </service>
      </services>
     </system.serviceModel>
    </configuration>

     

    Monday, January 14, 2013 4:56 AM
  • User-861818263 posted

    your contract should be the interface not implimantaion.Please share you interface and interface implimantaion code.

    contract="Service"
    Monday, January 14, 2013 6:38 AM
  • User1351755639 posted

    sorry but i donot have any interface implementation, i was only following "http://chandradev819.wordpress.com/2011/04/06/how-to-create-auto-complete-textbox-extender-using-%E2%80%9Cajax-enabled-wcf%E2%80%9D/" to build a autocomple textbox for my project.

    Thanx,

    Dipak

    Monday, January 14, 2013 7:31 AM
  • User-861818263 posted

    Then check and download code for creating ajax enabled WCF service

    http://chandradev819.wordpress.com/2011/04/05/how-to-use-ajax-enabled-wcf-in-asp-net/

    Monday, January 14, 2013 7:34 AM
  • User1351755639 posted

    sorry sir, but here also i didnot find any interface implemention.

    Thanx,

    Dipak

    Monday, January 14, 2013 7:43 AM
  • User-861818263 posted

    if you are using Ajax enabled WCF then there is no need of interface.

    Just chnage the name of service and assign to webconfig service name and contract name.

    <service name="NEW NAME">
        <endpoint address="" behaviorConfiguration="ServiceAspNetAjaxBehavior"
         binding="webHttpBinding" contract="NEW NAME" />
          <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
    
          <host>
            <baseAddresses>
              <add baseAddress="http://localhost/WebSite3/" />
            </baseAddresses>
          </host>
          </service>



    Monday, January 14, 2013 7:56 AM
  • User1351755639 posted

    Thank You sir, my code is now working fine. but when i am  trying to add service i am getting an error

    Metadata contains a reference that cannot be resolved: 'http://localhost/WebSite3/Service.svc'. There was no endpoint listening at http://localhost/WebSite3/Service.svc that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details. The remote server returned an error: (404) Not Found. If the service is defined in the current solution, try building the solution and adding the service reference again.

    Please help.

    Thanx,

    Dipak

    Monday, January 14, 2013 7:59 AM
  • User1351755639 posted

    After making the changes in web.config, when i am browsing my service in ie, i am getting an error.

    "Could not load file or assembly 'App_Web_3zwwgp3w, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

    And when i am tring to add script  reference in my website i  am gettng an error.

    "Could not load file or assembly 'App_Web_3zwwgp3w, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified......"

    My web.config file:

    <?xml version="1.0"?>
    <!--
      For more information on how to configure your ASP.NET application, please visit
      http://go.microsoft.com/fwlink/?LinkId=169433
      -->
    <configuration>
      <connectionStrings>
        <add name="TEST_DBConnectionString" connectionString="Data Source=dell-pc;Initial Catalog=TEST_DB;Integrated Security=True"
         providerName="System.Data.SqlClient" />
      </connectionStrings>
      <system.web>
        <sessionState timeout="1440"></sessionState>
    
        <compilation debug="true" targetFramework="4.0">
          <assemblies>
            <add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
            <add assembly="System.Web.Extensions.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <add assembly="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
          </assemblies>
        </compilation>
      </system.web>
      <appSettings>
        <add key="autocomp_ws.AutoComplete" value="http://localhost/WebSite3/AutoComplete.asmx"/>
      </appSettings>
      <system.serviceModel>
        <behaviors>
          <endpointBehaviors>
            <behavior name="ServiceAspNetAjaxBehavior">
              <!--<serviceMetadata httpGetEnabled="true" />-->
              <enableWebScript />
            </behavior>
          </endpointBehaviors>
        </behaviors>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
         multipleSiteBindingsEnabled="true" />
        <services>
          <service name="NEW NAME">
            <endpoint address="" behaviorConfiguration="ServiceAspNetAjaxBehavior"
             binding="webHttpBinding" contract="NEW NAME" />
            <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
    
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost/WebSite3/" />
              </baseAddresses>
            </host>
          </service>
    
    
          <!--<service name="Service">
            <endpoint address="" behaviorConfiguration="ServiceAspNetAjaxBehavior"
             binding="webHttpBinding" contract="Service" />-->
            <!--<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>-->
           
    
    
          
        </services>
      </system.serviceModel>
    </configuration>
    

    Monday, January 14, 2013 8:10 AM
  • User-861818263 posted

    you should provide new name here :- 

     name="NEW NAME" contract="NEw NAme"
    AFter renaming your service.
    Monday, January 14, 2013 8:16 AM
  • User1351755639 posted

    Sir, as suugested by you i have created new service called autocomplete_textbox.cs and in the web.config automatically added

     <service name="autocomplete_textbox">
            <endpoint address="" behaviorConfiguration="autocomplete_textboxAspNetAjaxBehavior"
              binding="webHttpBinding" contract="autocomplete_textbox" />
     </service>

    Now when i am triying add following line of code in web.config i am getting an error-

    Error

    -------------

    "The contract name 'IMetadataExchange' could not be found in the list of contracts implemented by the service autocomplete_textbox.  Add a ServiceMetadataBehavior to the configuration file or to the ServiceHost directly to enable support for this contract. "

    Web.config

    --------------------

            <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost/WebSite3/" />
              </baseAddresses>
            </host>

    I am posting my autocomplete_textbox.cs for ur reference-

    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class autocomplete_textbox
    {
        // Add [WebGet] attribute to use HTTP GET
        [OperationContract]
        public string[] GetEmpInfoWith_WCF(string prefixText)
        {
            SqlConnection con = new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;User ID=sa;Initial Catalog=TEST_DB;Data Source=DELL-PC");
    
            string sql = "select state_name from tbl_state Where state_name like @myParameter";
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            da.SelectCommand.Parameters.Add("@myParameter", SqlDbType.VarChar, 50).Value = prefixText + "%";
    
            DataTable dt = new DataTable();
            da.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                string[] items = new string[dt.Rows.Count];
                int i = 0;
                foreach (DataRow dr in dt.Rows)
                {
                    items.SetValue(dr["state_name"].ToString(), i);
                    i++;
                }
                return items;
            }
            else
            {
    
                string[] items = new string[1];
    
                items.SetValue("No State Found", 0);
    
                return items;
    
            }
    
        }
    }
    



    Thanx,

    Dipak
     

    Monday, January 14, 2013 9:09 AM