ajax请求在调用wcf服务时拒绝访问

时间:2014-10-26 17:35:03

标签: asp.net ajax wcf amazon-ec2

我创建了一个亚马逊EC2实例,并在端口80上托管了我的.net Web应用程序,在端口1212端口上托管了WCF服务。

在远程会话本地,我可以浏览应用程序和服务,并能够进行ajax请求调用。

只有当我尝试通过互联网(而不是在同一个远程会话上)执行相同的功能时才会在端口1212上调用wcf服务``给出访问被拒绝错误。我添加了$ .support.cors = true;在提出要求时。

网络应用web.config

<configuration>
    <system.web>
        <connectionStrings>
            <add name="ApplicationServices"   
                 connectionString="Server=somedbinstance.asdkfjlksd.us-west-2.rds.amazonaws.com;Initial Catalog=xed; user id=user ;password=pwd" 
                 providerName="System.Data.SqlClient" />
        </connectionStrings>
    </system.web>
    <system.serviceModel>
        <behaviors>
            <endpointBehaviors>
                <behavior name="webEndpoint">
                    <webHttp defaultBodyStyle="Wrapped" 
                             defaultOutgoingResponseFormat="Xml" helpEnabled="true" />
                </behavior>
            </endpointBehaviors>
        </behaviors>
        <bindings>
            <webHttpBinding>
                <binding name="webHttpBinding" />
            </webHttpBinding>
        </bindings>
        <client>
            <endpoint name="BasicHttpBinding_ICustomerRequestService" 
                address="http://10.90.12.121:1212/myservice.svc" 
                binding="webHttpBinding" bindingConfiguration="webHttpBinding" 
                behaviorConfiguration="webEndpoint" 
                contract="CustomerRequestService.ICustomerRequestService" />
        </client>
        <serviceHostingEnvironment 
             aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    </system.serviceModel>
    <system.webServer>
        <defaultDocument>
          <files>
            <add value="index.aspx" />
          </files>
        </defaultDocument>
    </system.webServer>
</configuration>

WCF服务web.config

<system.serviceModel>
    <services>
        <service name="iHandyService.CustomerRequestService">
            <endpoint 
                address="" 
                behaviorConfiguration="restfulBehavior" 
                binding="webHttpBinding" bindingConfiguration="" 
                contract="Interfac.ICustomerRequestService" />
               <host>
                   <baseAddresses>
                       <add baseAddress="http://10.90.12.121:1212/myservice.svc" />
                   </baseAddresses>
               </host>
        </service>
    </services>
    <behaviors>
        <endpointBehaviors>
            <behavior name="restfulBehavior">
                <webHttp />
            </behavior>
        </endpointBehaviors>
        <serviceBehaviors>
            <behavior name="">
                <serviceMetadata httpGetEnabled="true" />
                <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true">
    </serviceHostingEnvironment>
</system.serviceModel>
<system.webServer>
   <httpProtocol>
       <customHeaders>
           <add name="Access-Control-Allow-Origin" value="*" />
           <add name="Access-Control-Allow-Headers" value="Content-Type" />
       </customHeaders>
   </httpProtocol>
   <modules runAllManagedModulesForAllRequests="true" />
</system.webServer>

Ajax电话:

enter image description here

错误功能提供访问被拒绝错误。

请指导我此错误以及解决方法。

1 个答案:

答案 0 :(得分:0)

您需要将此端口添加到AWS控制台 - &gt; EC2 - &gt;安全组 - &gt;入站规则 - &gt;端口。这个链接会很有帮助。您需要选择与您的实例对应的安全组,这在实例链接中也是可见的。

LINK1LINK2

另外请确保已将此端口添加到Windows防火墙入站规则。 - http://technet.microsoft.com/en-us/library/cc947789(v=ws.10).aspx

注意:您可能需要将WCF服务配置为CORS就绪,请点击此链接 - WCF REST Service Template 40(CS) Cross domain error,我已经在那里回答了如何启用CORS。