我有一个测试WCF服务,当我在visual studio上尝试它时工作正常,当我在浏览器中粘贴链接http://localhost:1347/Service1.svc/DoWork时,它显示浏览器空白屏幕的错误请求
无法加载资源:服务器响应状态为400 (错误请求)
当我尝试使用JSON时显示CORS问题,我在Web.Config中添加了一些脚本,但仍存在错误请求的问题,WCF有以下操作合同;
namespace WcfService1
{
[ServiceContract]
public interface IService1
{
[OperationContract]
string DoWork();
[OperationContract]
string GetData(int value);
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
}
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";
[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}
[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}
}
他们的实施来到这里。
public class Service1 : IService1
{
public string DoWork()
{
return string.Format("This is DoWork()");
}
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
}
的Web.Config
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5.2"/>
<httpRuntime targetFramework="4.5.2"/>
<httpModules>
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"/>
</httpModules>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="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>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https"/>
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<remove name="ApplicationInsightsWebTracking"/>
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"
preCondition="managedHandler"/>
</modules>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*"/>
<add name="Access-Control-Allow-Headers" value="Content-Type, Accept" />
<add name="Access-Control-Allow-Methods" value="POST,GET,OPTIONS" />
<add name="Access-Control-Max-Age" value="1728000" />
</customHeaders>
</httpProtocol>
<!--
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"/>
<validation validateIntegratedModeConfiguration="false"/>
</system.webServer>
</configuration>
JSON致电
$(document).ready(function(){
$.get("http://localhost:1347/Service1.svc/DoWork", function(data, status){
alert("Data: " + data + "\nStatus: " + status);
});
答案 0 :(得分:0)
在/*set leaf node css*/
.leaf {
position: relative;
top: 1px;
font-family: 'Glyphicons Halflings';
font-style: normal;
font-weight: normal;
line-height: 1;
}
/*set arrow head at the right of leaf*/
.in-leaf:after {
content: "\e072";
position: absolute;
right: 0;
}
文件中添加以下代码。这将允许global.asax
个请求成功完成。
preflight
答案 1 :(得分:0)
“错误请求”响应将归因于HTTP动词不匹配。 在WCF中使用basicHttpBinding进行所有操作的SOAP请求使用HTTP POST,而不是HTTP GET。 如果要使用HTTP GET,请使用webHttpBinding。
或者更好的是,使用Web API而不是WCF。