0

I'm new at WCF. I've managed to put in place a working project in VS 2015. In this, I have two Services Contracts, each with a Operation Contract. I also have a DataContract for the composite Class I need. I've set the wcf solution as a site on IIS, port 8000.

My true problem is this: if the two endpoints corresponding to the two contracts are in basicHttpBinding, they both appear in the WCF Client when I hit F5, they both can be invoked and give expected results, and they both can be consumed by a test console solution.

However, as soon as I switch the second one to webHttpBinding and add a webhttp behaviorConfiguration (to be able to use json data), this endpoint ceases to appear in the WCF Client, of course can no longer be invoked, and I receive a "endpoint not found" from my test solution. In this case, I'm using a HttpWebRequest to try to get to the endpoint; it's in the error html message that I can read the endpoint was not found.

Perhaps I'm missing something obvious here.

Here's the code:

Service.cs

    namespace WcfService2
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class DbQuery : IDbQuery, IDbQueryJason
    {
        public string GetDescriptions(string ParametersString)
        {
            return "received " + ParametersString;
        }
        public string GetDescriptionsInJson(string ParametersString)
        {
            return "received2 " + ParametersString;
        }
    }
}

Iservice.cs :

    namespace WcfService2
    {
        [ServiceContract]
        public interface IDbQuery
        {
            [OperationContract]
            [WebInvoke(UriTemplate = "/getdescriptions",
            BodyStyle = WebMessageBodyStyle.Wrapped)]
            List<Description> GetDescriptions(string Parameters);
        }
        [ServiceContract]
        public interface IDbQueryJason
        {
            [OperationContract]
            [WebInvoke(UriTemplate = "/getdescriptionsinjson",
            BodyStyle = WebMessageBodyStyle.Wrapped)]
            string GetDescriptionsInJson(string Parameters);
        }
        [DataContract]
        public class Description
        {
            [DataMember]
            public string IDInfo;
            [DataMember]
            public string CatalogInfo;
            [DataMember]
            public string TitleInfo;
            [DataMember]
            public string AuthoringInfo;
            [DataMember]
            public string FormatInfo;
            [DataMember]
            public string DateInfo;
        }
}

web.config :

    <?xml version="1.0"?>
    <configuration>

      <appSettings>
        <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
      </appSettings>
      <system.web>
        <compilation debug="true" targetFramework="4.0" />
        <httpRuntime targetFramework="4.0"/>
      </system.web>
      <system.serviceModel>
        <behaviors>
          <serviceBehaviors>
            <behavior name="Mg">
              <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
              <serviceDebug includeExceptionDetailInFaults="true"/>
            </behavior>
          </serviceBehaviors>
          <endpointBehaviors>
            <behavior name="rfb">
              <webHttp/>
            </behavior>
          </endpointBehaviors>
        </behaviors>
        <!--<bindings>
          <webHttpBinding>
            <binding name="crossDomain" crossDomainScriptAccessEnabled="true"></binding>
          </webHttpBinding>
        </bindings>-->
        <services>
          <service name="WcfService2.DbQuery" behaviorConfiguration="Mg">
            <endpoint name="getdescriptions"
              address ="getdescriptions"
              binding ="basicHttpBinding"
              contract ="WcfService2.IDbQuery"/>
            <endpoint name="getdescriptionsinjson"  behaviorConfiguration="rfb"
              address ="getdescriptionsinjson"
              binding ="webHttpBinding" 
              contract ="WcfService2.IDbQueryJason"/>
            <endpoint contract="IMetadataExchange"
            binding="mexHttpBinding"
            address="mex" />
</service>
        </services>
        <protocolMapping>
          <add scheme="http" binding="webHttpBinding"/>
        </protocolMapping>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true">
          <serviceActivations>
            <add factory="System.ServiceModel.Activation.WebServiceHostFactory"
                 relativeAddress="./Service.svc"
                 service="WcfService2.DbQuery"/>
          </serviceActivations>

        </serviceHostingEnvironment>
      </system.serviceModel>
      <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
        <directoryBrowse enabled="true"/>
        <handlers>
          <add name="svc" path="*.svc" verb="*" type="System.ServiceModel.Activation.ServiceHttpHandlerFactory, System.ServiceModel.Activation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" resourceType="File" preCondition="integratedMode" />
        </handlers>
      </system.webServer>

    </configuration>

Abstract from the test solution :

HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://localhost:8000/Service.svc/getdescriptionsinjson");
            req.Method = "POST";
jarlh
  • 42,561
  • 8
  • 45
  • 63
Luc Wanlin
  • 97
  • 2
  • 5
  • `WebHttpBinding` exposes your service as a `REST` based service.I don't think you can use `BasicHttpBinding` with `WebHttpBinding`. – Amit Kumar Ghosh Oct 05 '15 at 09:59
  • Exactly! ``WebHttpBinding`` is a REST endpoint and supposed to be used from javascript or another json consumer. WCF client is a SOAP client and cannot be used with REST endpoint. – Mimas Oct 07 '15 at 12:44

0 Answers0