1

The error is

On ctrl+F5 I am getting following error.

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

Here Is my interface declaration

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Web;

namespace DealerAuditWCFService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IDealerAuditService" in both code and config file together.
    [ServiceContract]
    public interface IDealerAuditService
    {
        [OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "/GetDealerbyRegId/{regId}", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        Registration GetDealerbyRegId(string regId);

        //[OperationContract]
        //List<Registration> GetAllDealers();

        //[OperationContract]
        //void SaveDealerReg(List<Registration> regDetails);

    }
}

Here is interface implementation

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data.Entity;

namespace DealerAuditWCFService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "DealerAuditService" in code, svc and config file together.
    public class DealerAuditService : IDealerAuditService
    {
        public Registration GetDealerbyRegId(string regId)
        {
            using (DealerAuditEntities dealerAudit = new DealerAuditEntities())
            {
                Registration regDetails = new Registration();
                var reg = (from r in dealerAudit.tblRegistrationDetails
                              where r.regId == regId
                              select r).First();

                regDetails.regId = reg.regId;
                regDetails.Location = reg.Location;
                regDetails.Region = reg.Region;
                regDetails.typeOfAudit = reg.typeOfAudit;
                return regDetails;
            }
        }
    }
}

class for data members

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;

namespace DealerAuditWCFService
{
    [DataContract]
    public class Registration
    {
        [DataMember]
        public string regId { get; set; }

        [DataMember]
        public string channelPartnerName { get; set; }

        [DataMember]
        public string Location { get; set; }

        [DataMember]
        public string Region { get; set; }

        [DataMember]
        public string typeOfAudit { get; set; }
    }
}

Here is my web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </assemblies>
    </compilation>
  </system.web>
  <system.serviceModel>
    <behaviors>
      
      
      <serviceBehaviors>
        <behavior name="metadataBehavior" >
          <serviceMetadata httpGetEnabled="True"/>
        <!--<behavior name="servicebehavior">-->
          <!-- 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>
      
      
    </behaviors>
    <services>
      <service name="DealerAuditWCFService.DealerAuditService" behaviorConfiguration="metadataBehavior">
        <endpoint address="" binding="basicHttpBinding" contract="DealerAuditWCFService.IDealerAuditService" behaviorConfiguration="DealerAuditWCFService.DealerAuditService"/>

        <endpoint
            address="mex"
            binding="mexHttpBinding"
            contract="IMetadataExchange"/>
      </service>
    </services>

    
    
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
  </system.webServer>
  <connectionStrings>
    <add name="DealerAuditEntities" connectionString="metadata=res://*/DealerAuditEntityDataModel.csdl|res://*/DealerAuditEntityDataModel.ssdl|res://*/DealerAuditEntityDataModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=192.168.8.122;initial catalog=Beat_plan_db;persist security info=True;user id=sa;password=sa#122;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

As i am new to WCF,So trying to invoke a sample method through REST service.I keep on getting the same error.Please help me to solve this issue.

Kiran
  • 39
  • 2

1 Answers1

0

Firstly, you need to decide if you are going to expose a REST or a SOAP endpoint. The decision is complex and not really the topic in question here.

For REST, you need to use the webHttpBinding, for SOAP, either the basicHttpBinding or the wsHttpBinding.

However, to produce metadata (at least of the type supported by the proxy generator via visual studio add service reference functionality), you are limited to a SOAP endpoint.

To consume a REST endpoint you need to model the same DTOs as the service (or just use the same assembly where they are defined), and then use either HttpClient or WebClient (there are advantages to both).

Community
  • 1
  • 1
tom redfern
  • 28,053
  • 12
  • 86
  • 116