在我的WCF服务上启用CORS

时间:2015-09-18 12:24:56

标签: wcf cors

情况:

我根本不知道如何在我的WCF服务中正确启用CORS,以便我可以通过JQuery或AJAX进行通信。我已经尝试了所有的东西而且我一直在打砖墙......

我的global.asax文件看起来像这样 - 试图启用CORS:

    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

我的Web.Config文件看起来像这样:

<?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>

我需要以JSON格式从我的服务中读取数据并将其显示在我的移动应用程序上 - 但无论我做什么都没关系,只是不会发生。

我不知道还有什么可以做以及如何启用CORS,这样我才能正常沟通。

0 个答案:

没有答案