我在Web項目中創建了一個啓用了Ajax的WCF,其中包含一個返回消息「Hello World」的簡單方法並將其託管在IIS中。當我打電話WCF從瀏覽器,它是工作,顯示像從Windows應用程序訪問WCF時出現「方法不允許」錯誤
消息{「d」:的「Hello World」}
我創建了一個Windows應用程序添加的代碼如下訪問的WCF
BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
EndpointAddress epa = new EndpointAddress("http://localhost:60/wcf");
ServiceReference2.PurchaseWCFClient objk = new ServiceReference2.PurchaseWCFClient(binding,epa);
label1.Text = objk.DoWork();
但這代碼
label1.Text = objk.DoWork();
返回一個錯誤,指出「遠程服務器返回了意外的響應:(405)方法不允許。」
我在谷歌搜索了很多,並嘗試了很多方法,但沒有任何作品。我是WCF的新手,對WCF沒有太多的瞭解。
相同功能的工作,如果我使用javascript調用或Ajax調用
這是我的purchaseWCF.svc.cs包含來自同一項目調用WCF
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class PurchaseWCF
{
[WebGet()]
[OperationContract]
public string DoWork()
{
return "Hello World";
}
}
我的web.config中包含
<service name="InfraERP.WCF.PurchaseWCF">
<endpoint address="" behaviorConfiguration="InfraERP.WCF.PurchaseWCFAspNetAjaxBehavior"
binding="webHttpBinding" contract="InfraERP.WCF.PurchaseWCF" />
</service>
<behavior name="InfraERP.WCF.PurchaseWCFAspNetAjaxBehavior">
<enableWebScript />
最初我試圖通過使用像這樣
ServiceReference2.PurchaseWCFClient objk = new ServiceReference2.PurchaseWCFClient();
在這個時候我得到這樣的錯誤「無法找到ServiceModel客戶端配置部分中引用合同'ServiceReference2.PurchaseWCF'的默認端點元素。這可能是因爲沒有爲您的應用程序找到配置文件,或者因爲在客戶端元素中找不到與此合同匹配的端點元素。「
之後提到了一些網站,我添加了上面的代碼並獲得了」Method不允許」錯誤 我已經浪費了一個完整的一天,在這...請幫我找出問題...
您是否在IIS上激活了WCF託管?例如有關如何在IIS 2012中執行此操作,請參閱http://stackoverflow.com/questions/14082950/svc-files-in-windows-server-2012-respond-with-405-method-not-allowed – Yaniv