-
September 18th, 2015, 07:33 AM
#1
Enabling CORS on a WCF Service
Hi guys.
I am really at my wits end, and I am going crazy because I am not getting much help ( by searching google etc. )
The situation:
I simply do not know how to enable CORS properly in my WCF Service, so that I can communicate through JQuery or AJAX. I have tried everything and I keep hitting a brick wall.....
My global.asax file looks like This - in a miserable attempt to enable CORS:
Code:
Imports System.Web.SessionState
Public Class Global_asax
Inherits System.Web.HttpApplication
''' <summary>
''' Enables cross domain POST, MERGE, DELETE for Firefox and Chrome
''' This requires:
''' <system.ServiceModel>
''' <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
''' </summary>
Shared Sub EnableCrossDomain()
Dim origin As String = HttpContext.Current.Request.Headers("Origin")
If String.IsNullOrEmpty(origin) Then
Return
End If
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", origin)
Dim method As String = HttpContext.Current.Request.Headers("Access-Control-Request-Method")
If (Not String.IsNullOrEmpty(method)) Then
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", method)
End If
Dim headers As String = HttpContext.Current.Request.Headers("Access-Control-Request-Headers")
If (Not String.IsNullOrEmpty(headers)) Then
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", headers)
End If
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Credentials", "true")
If HttpContext.Current.Request.HttpMethod = "OPTIONS" Then
HttpContext.Current.Response.StatusCode = 204
HttpContext.Current.Response.End()
End If
End Sub
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the application is started
End Sub
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the session is started
End Sub
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
'HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*")
'If HttpContext.Current.Request.HttpMethod = "OPTIONS" Then
' HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "POST, PUT, DELETE")
' HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept")
' HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000")
' HttpContext.Current.Response.End()
'End If
'' Fires at the beginning of each request
EnableCrossDomain()
End Sub
Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)
' Fires upon attempting to authenticate the use
End Sub
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
' Fires when an error occurs
End Sub
Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the session ends
End Sub
Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the application ends
End Sub
End Class
My Web.Config file looks like This :
HTML Code:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<!--DEFINE YOUR SERVICES WITH ENDPOINTS-->
<services>
<service name="UmbrellaMobileService"
behaviorConfiguration="MyServiceBehavior">
<endpoint
address=""
binding="webHttpBinding"
bindingConfiguration="webHttpBindingWithJsonP"
behaviorConfiguration="webEndPointBehavior"
name="webEndPoint"
contract="IUmbrellaMobileService"/>
</service>
</services>
<bindings>
<webHttpBinding>
<binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true" />
</webHttpBinding>
</bindings>
<behaviors>
<!--SERVICE BEHAVIORS-->
<serviceBehaviors>
<behavior name="MyServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<!--ENDPOINT BEHAVIORS-->
<endpointBehaviors>
<behavior name="webEndPointBehavior">
<webHttp defaultOutgoingResponseFormat="Json" defaultBodyStyle="Bare"/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true"/>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*"/>
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
I need to read the data from my Service in JSON format and display it on my mobile app - but it doesn't matter what I do, it just doesn't happen.
I do not know what else to do and how to do it to enable CORS, so that I can communicate properly
Can anyone help?
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|