简体   繁体   中英

Service metadata may not be accessible In WCF

I am calling my wcf service using Ajax , for this I have configured my web.config file . but now when I run my service, it is giving error like this .

Failed to add a service. Service metadata may not be accessible. Make sure your service is running and exposing metadata.

This is my Interface

namespace WcfWithJson
{
    [ServiceContract]
    public interface IMyservice
    {
        [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json)]
        UserDetails[] GetUserDetails(string Username);
    }
}

Note : userDetails is my class name. Now I have Implemented mY interface here

namespace WcfWithJson
{
   [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class MyService : IMyservice
    {
        public UserDetails[] GetUserDetails(string Username)
        {
            string strConnection = ConfigurationManager.ConnectionStrings["MyDbEntities"].ConnectionString;
            List<UserDetails> userdetails = new List<UserDetails>();
            using (SqlConnection con = new SqlConnection(strConnection))
            {
              // some sql Code here
            }
            return userdetails.ToArray();
        }
    }
}

And This is my web.config file

    <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
    <services>
      <service name="WcfWithJson.MyService" behaviorConfiguration="metadataBehavior">
        <endpoint address="" binding="basicHttpBinding" contract="WcfWithJson.IMyservice" behaviorConfiguration="EndPointBehavior" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="EndPointBehavior">
          <enableWebScript />
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

This Is complete Error :

Error: Cannot obtain Metadata from http://localhost:61762/MyService.svc If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address.
For help enabling metadata publishing, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange Error URI: http://localhost:61762/MyService.svc Metadata contains a reference that cannot be resolved: 'http://localhost:61762/MyService.svc'.
Content Type application/soap+xml; charset=utf-8 was not supported by service http://localhost:61762/MyService.svc. The client and service bindings may be mismatched. The remote server returned an error: (415) Unsupported Media Type.HTTP GET Error URI: http://localhost:61762/MyService.svc The HTML document does not contain Web service discovery information.

In case of WCF with Ajax, The <endpoint /> for the service should use the WebHttpBinding and the ASP.NET AJAX behavior configured under <endpointBehaviors> tag.

So, the entries should be:

<services>
 <service name="WcfWithJson.MyService" behaviorConfiguration="ServiceBehavior">
  <endpoint address="" binding="webHttpBinding" 
    contract="WcfWithJson.IMyservice" behaviorConfiguration="EndPointBehavior" />
  <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
 </service>
</services>

[ The 'behaviourConfiguration' attribute of the <service> tag should be set to the corresponding behavior configured for the service. Here you have configured the behavior for service with name: " ServiceBehaviour " ]

check here the article on correct use of various in-built WCF bindings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM