当我尝试直接从Web应用程序访问Windows服务中托管的WCF服务时出现问题,我无法理解我做错了什么。
我尝试了我发现的所有建议,但没有任何帮助。我使用AngularJs,但这并不重要,我接受所有建议。
有我的项目:https://github.com/djromix/Portal.WorckFlow
Portal.Services
是Windows服务。
这是我的Windows服务配置:
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
</customHeaders>
</httpProtocol>
</system.webServer>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="Portal.Services.ServiceContract.PortalContract">
<endpoint
address=""
binding="basicHttpBinding"
contract="Portal.Services.ServiceContract.IPortalContract">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/Portal" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
服务代码:
namespace Portal.Services.ServiceContract
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IPortalContract" in both code and config file together.
[ServiceContract(Namespace = "")]
public interface IPortalContract
{
[WebInvoke(ResponseFormat = WebMessageFormat.Json, Method = "GET", BodyStyle = WebMessageBodyStyle.Wrapped)]
double Ping();
[OperationContract]
object CashInResult(string key);
}
}
namespace Portal.Services.ServiceContract
{
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class PortalContract : IPortalContract
{
public double Ping()
{
return -1;
}
[WebGet]
[WebInvoke(ResponseFormat = WebMessageFormat.Json, Method = "GET", BodyStyle = WebMessageBodyStyle.Wrapped)]
public object CashInResult(string key)
{
return new {Value = "Some Data"};
}
}
}
我只想简单访问url并获取json结果
http://localhost:8000/Portal/CashInResult?key=secretkey
现在我收到错误[Failed to load resource: the server responded with a status of 400 (Bad Request)]
从Web应用程序我得到错误
XMLHttpRequest cannot load /Portal/CashInResult?key=1. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '???' is therefore not allowed access. The response had HTTP status code 400.
答案 0 :(得分:1)
要让您的GET请求正常工作,您可以在浏览器中自己将标题(Access-Control-Allow-Origin)添加到请求中,但只有GET请求才有效。
如果您在Windows服务中运行WCF,则不使用system.webServer,因为没有IIS。
此链接显示如何在IIS之外的WCF中实现完全CORS。
https://code.msdn.microsoft.com/windowsdesktop/Implementing-CORS-support-c1f9cd4b
但在SO帖子中解释有点长,但这就是为什么它现在不适合你......
CORS世界中有两种类型的请求,“正常”请求和“预检”请求。
正常或安全(HTTP GET)请求涉及浏览器向请求发送ORIGIN标头,服务器根据该标头接受/拒绝。
预检或不安全(例如POST,PUT或DELETE)请求涉及浏览器发送HTTP OPTIONS请求,要求获得将实际请求发送到服务器的权限。
当您在system.webServer部分中启用设置时,IIS将为您提供所有这些功能。托管WCF作为Windows服务需要IIS出局,所以在WCF中你需要自己实现CORS。
我认为您应该重新考虑并使用IIS,如果服务的目的是为HTTP请求提供服务。
答案 1 :(得分:0)
经过多次尝试,我找到了解决方案。
namespace Portal.Services.ServiceContract
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IPortalContract" in both code and config file together.
[ServiceContract(Namespace = "")]
public interface IPortalContract
{
[WebInvoke(ResponseFormat = WebMessageFormat.Json, Method = "GET", BodyStyle = WebMessageBodyStyle.Wrapped)]
double Ping();
[OperationContract]
string CashInResult(string key);
}
}
namespace Portal.Services.ServiceContract
{
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class PortalContract : IPortalContract
{
readonly Logger _nLog = LogManager.GetCurrentClassLogger();
public double Ping()
{
using (var tMeter = new TimeMeterLog(_nLog, "Ping"))
{
tMeter.Info("-1");
return -1;
}
}
[WebGet(UriTemplate = "/CashInResult/{key}", ResponseFormat = WebMessageFormat.Json)]
public string CashInResult(string key)
{
using (var tMeter = new TimeMeterLog(_nLog, "CashInResult"))
{
var result = JsonConvert.SerializeObject(new { Value = "Some Data" });
tMeter.Info(result);
return result;
}
}
}
}
从浏览器调用服务:
http://localhost:8000/rest/Ping
结果:{"PingResult":-1}