0

I am making the jump from asmx webservice to WCF (mostly to support working with Json)

I have gotten to the point that the Service works (locally only) in http only but not https. (On my server neither works as the server forces https)

Here is my simple Codes:

MyNewService.VB

Public Class MyNewService
Implements IMyNewService

Public Sub New()
End Sub

Public Function GetResults() As List(Of Person) Implements IMyNewService.GetResults
    Dim rslt As New List(Of Person)

    rslt.Add(New Person("Mike", "Anderson", 40))
    rslt.Add(New Person("Drew", "Carry", 38))
    rslt.Add(New Person("John", "Tavares", 43))

    Return rslt
End Function
End Class

IMyNewService.VB

<ServiceContract()>
Public Interface IMyNewService
    <OperationContract()>
    <WebInvoke(Method:="GET", RequestFormat:=WebMessageFormat.Json, ResponseFormat:=WebMessageFormat.Json, UriTemplate:="getPeople")>
    Function GetResults() As List(Of Person)
End Interface

<DataContract()>
Public Class Person
    <DataMember()>
    Public Property FirstName() As String

    <DataMember()>
    Public Property LastName() As String

    <DataMember()>
    Public Property Age() As Integer

    Public Sub New(firstname As String, lastname As String, age As Integer)
        Me.FirstName = firstname
        Me.LastName = lastname
        Me.Age = age
    End Sub
End Class

Web.Config

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.7.2" />
    <httpRuntime targetFramework="4.7.2"/>
    <pages>
      <namespaces>
        <add namespace="System.Runtime.Serialization" />
        <add namespace="System.ServiceModel" />
        <add namespace="System.ServiceModel.Web" />
      </namespaces>
    </pages>
  </system.web>
  <system.serviceModel>
        <protocolMapping>
      <add scheme="http" binding="webHttpBinding"/>

    </protocolMapping>
    <behaviors>
      <serviceBehaviors>
        <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>
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>
</configuration>

So, currently I can run: http://localhost:61028/MyNewService.svc/getPeople

and I correctly get :

[{"Age":40,"FirstName":"Yoni","LastName":"Sudwerts"},{"Age":38,"FirstName":"Joshua","LastName":"Kishinef"},{"Age":43,"FirstName":"Saul","LastName":"Kaye"}]

But if I run: https://localhost:44386/MyNewService.svc/getPeople

I get a 404 Error.

Can anyone find my mistake and help a guy out?

Thanks in advance

Yoni
  • 316
  • 2
  • 13
  • https://docs.jexusmanager.com/tutorials/binding-diagnostics.html#background Check site bindings first. – Lex Li Mar 06 '19 at 17:16

1 Answers1

1

Generally speaking, I add two service endpoint address to host the service over http and https.
WCF Service not hitting from postman over https
Or the following simplified configuration.

<system.serviceModel>
<behaviors>
  <serviceBehaviors>
    <behavior>
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior>
      <webHttp />
    </behavior>
  </endpointBehaviors>
</behaviors>
<bindings>
  <webHttpBinding>
    <binding name="https">
      <security mode="Transport">
        <transport clientCredentialType="None"></transport>
      </security>
    </binding>
  </webHttpBinding>
</bindings>
<protocolMapping>
  <add binding="webHttpBinding" scheme="http" />
  <add binding="webHttpBinding" scheme="https" bindingConfiguration="https" />
</protocolMapping>    
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />

Feel free to let me know if there is anything I can help with.

Abraham Qian
  • 7,117
  • 1
  • 8
  • 22
  • Works great. I got http and https working both locally and on the server with the endPoint GetPeople. Only question I have is that the WCF Test Client is not loading anything. No errors. It just seems to process the file but it does not show me anything in the Projects List – Yoni Mar 07 '19 at 10:24
  • Yes,it is a little complex that calling the method by using wcf test client when we use Webhttpbind to create the service,just like adding service reference, We need to add the Webget attributes, webhttp behavior to the auto-generated service class/interface/operation manually. I will make an example if you post a new thread. 0.0 – Abraham Qian Mar 07 '19 at 10:55