我嘗試直接從Web應用程序訪問Windows服務中承載的WCF服務時出現問題,我無法理解我在做什麼錯誤。在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.
你試過了什麼,結果是什麼?你能看到服務嗎?你有錯誤嗎? – Tim
從瀏覽器時,訪問URL我得到異常[無法加載資源:服務器響應狀態400(壞請求] – user2156275