为什么无法访问Web服务

时间:2012-08-30 13:22:45

标签: c# asp.net-mvc-3 wcf

当我尝试从开发服务器访问Web服务时,它会返回以下错误。 当我从本地计算机访问相同的服务时,它工作正常。 你知道可能是什么原因吗? 非常感谢你提前!

System.ServiceModel.ProtocolException: The remote server returned an unexpected response: (405) Method Not Allowed. ---> System.Net.WebException: The remote server returned an error: (405) Method Not Allowed.
   at System.Net.HttpWebRequest.GetResponse()
   at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
   --- End of inner exception stack trace ---

Server stack trace:
   at System.ServiceModel.Channels.HttpChannelUtilities.ValidateRequestReplyResponse(HttpWebRequest request, HttpWebResponse response, HttpChannelFactory factory, WebException responseException, ChannelBinding channelBinding)
   at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
   at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]:
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at Portal.ExportSvcReference.IExportDataService.AddDataRequest(ExportData request)

1 个答案:

答案 0 :(得分:2)

405错误通常是由跨域问题引起的。如果您已部署服务,但正在localhost(或其他部署)上的客户端进行测试,则所有内容都不再在同一localhost域下运行,因此您需要启用跨域调用。

可以在此处找到启用跨域脚本编写的一个很好的示例: how to avoid cross domain policy in jquery ajax for consuming wcf service?

正如@Monkieboy所说,如果没有看到你的web.config文件就诊断起来有点困难,但是尝试通过添加crossDomainScriptAccessEnabled="true"来判断你正在使用哪种绑定,看看是否有帮助。