0
Angular CLI: 6.1.5
Node: 8.11.4
OS: win32 x64

I'm struggling to understand this error. I under stand it has to do with CORS, but I can't seem to get around it and it's localhost, connecting to localhost. Angular is running on port 4200, and my service is running on port 80 via WCF from a Windows Service. Both are on localhost, so I'm not sure why I'm getting the error.. Even if I guess this working, I would need to understand the error when I'm rolling into upper environments. I tried http.cors.enabled: false without success. I'm not sure I understand this purpose.

import { Response } from '@angular/http';
import { HttpClient, HttpHeaders } from '@angular/common/http';

export enum POST_TYPE {
  POST = 1,
  GET = 2
};

export class RestService {
  private headers: HttpHeaders;

  constructor(private http: HttpClient) {};

  public getResponse = function(postType: POST_TYPE, restURL: string, postData: string) {
    this.headers = new HttpHeaders({
      'Content-Type': 'text/xml;charset=UTF-8',
      'http.cors.enabled': 'true',
      'http.cors.allow-origin': '*',
      'http.cors.allow-methods': 'OPTIONS, HEAD, GET, POST, PUT, DELETE',
    });

    if ( postType === POST_TYPE.POST && postData != null) {
      return this.http.post(restURL, postData, {headers: this.headers})
        .map((res: Response) => res.json());
    } else {
      return this.http.get(restURL)
        .map((res: Response) => res.json());
    }
  };
}

EDIT (ADD, app.config used by WCF Library, not on IIS, within a Windows Service):

  <system.serviceModel>
    <services>
      <service name="WCFListener.ABCMonitor" behaviorConfiguration="Throttled">
        <endpoint address="" binding="webHttpBinding" contract="WCFListener.IABCMonitor" behaviorConfiguration="web">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <!--<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>-->
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:80/ABC/"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="Throttled">
          <serviceTimeouts transactionTimeout="00:01:00"/>
          <!-- 
              https://learn.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/wcf/servicethrottling
              maxConcurrentCalls:
                A positive integer that limits the number of messages that currently process across a ServiceHost. 
                Calls in excess of the limit are queued. Setting this value to 0 is equivalent to setting it to Int32.MaxValue. 
                The default is 16 * processor count. 
              maxConcurrentSessions:
                A positive integer that limits the number of InstanceContext objects that execute at one time across a ServiceHost. 
                Requests to create additional instances are queued and complete when a slot below the limit becomes available. 
                The default is the sum of maxConcurrentSessions and MaxConcurrentCalls 
              maxConcurrentInstances:
                A positive integer that limits the number of sessions a ServiceHost object can accept.
                The service will accept connections in excess of the limit, but only the channels below the limit are active 
                (messages are read from the channel). 
                Setting this value to 0 is equivalent to setting it to Int32.MaxValue. 
                The default is 100 * processor count. 
          -->
          <serviceThrottling maxConcurrentCalls="5" maxConcurrentSessions="5" maxConcurrentInstances="5"/>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="False" httpsGetEnabled="False"/>
          <!-- 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="True"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6"/>
  </startup>
Chizl
  • 2,004
  • 17
  • 32
  • You have a CORS issue if your origins are different. Having a different port will cause the origin to be different. In order to configure CORS, your web server needs to be configured to accept the cross-origin requests from your Angular app's origin (`localhost:4200`). – Daniel W Strimpel Sep 11 '18 at 17:21
  • You'd need to share your server/service configuration and what you did on the server side to allow cross origin requests. Adding these headers on the client side will not prevent server from rejecting cross origin requests. – Alexander Staroselsky Sep 11 '18 at 17:22
  • This is WCF coming from a Windows Service not IIS, so not sure about a cross origin request option. Never needed that before and is being used by other services with no issue. Just seems to be a problem from Angular – Chizl Sep 11 '18 at 19:34
  • I'm going to try and add something I see on https://stackoverflow.com/questions/14047754/how-to-add-cross-domain-support-to-wcf-service once I get my TFS issue resolved and can compile again. – Chizl Sep 11 '18 at 19:40
  • I found the Preflighted Requests in CORS.page of which it's referring and that doesn't tell me the answer, just the problem. This is also in just Javascript, event though Angular is JavaScript, it's not the same call as I'm sure JavaScript doesn't have the HttpClient object, but instead uses XMLHttpRequest. – Chizl Sep 12 '18 at 11:58
  • Angular's `HttpClient` library uses `XMLHttpRequest` under the hood: https://github.com/angular/angular/blob/cf0968f98e844043a0f6c2548201f3c0dfd329a7/packages/common/http/src/xhr.ts#L45-L49 – Daniel W Strimpel Sep 12 '18 at 15:13
  • I think it's messed up that Angular 4 is marked as my solution, when I'm using Angular 6 and yes, it doesn't work the same way!!!! – Chizl Sep 12 '18 at 16:02
  • Not sure why I even try stackoverflow anymore.. It's become a bunch of power trip people, that don't know the problem, assume a solution, then downgrade people because they think that everyone should know what they do. Isn't that what we are here for? Yet, they don't even have the answer or they would answer it !!!! Just assume I haven't already searched and found no solution, so how dare I post the question again. Real developers know, not every solution is everyone's answer, but what do I know, only been developing for 25 years. – Chizl Sep 12 '18 at 17:03
  • I doubt that there were no CORS issue on angular 4 as this is enforced by BROWSER and is angular independent . – Antoniossss Sep 12 '18 at 20:48

0 Answers0