我有一个 Web 服务 (LoginService) 我从另一个 Web 服务 (AndroidService) 调用。我有一个简单的方法来测试连接,但它不起作用。
在 LoginService 中,我有一个名为 getStuff(string password) 的方法——它以 JSON 格式返回一个字符串。当我通过浏览器直接访问它时,在http://localhost/LoginService/LoginService.svc/getStuff/foo它应该可以正常工作。当我通过其他 Web 服务尝试相同的操作时,它失败并出现主题异常和 InnerException“服务器返回 404 Not Found”。
这是 LoginService 定义:
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "getStuff/{password}")]
string getStuff(string password);
这是来自 AndroidService 的代码:
public string getStuff(string password)
{
string endpoint_address = "http://localhost/LoginService/LoginService.svc/";
BasicHttpBinding binding = new BasicHttpBinding();
EndpointAddress address = new EndpointAddress(endpoint_address);
client = new LoginServiceClient(binding, address);
return client.getStuff(password);
}
和 AndroidService 定义:
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "getStuff/{password}")]
string getStuff(string password);
这是完整的例外:
Request Error
The server encountered an error processing the request. The exception message is 'There was no endpoint listening at http://localhost/LoginService/LoginService.svc that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.'. See server logs for more details. The exception stack trace is:
Server stack trace: at System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason) at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout) at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout) at System.ServiceModel.Dispatcher.RequestChannelBinder.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 AndroidServiceSpace.LoginServiceReference.ILoginService.getStuff(String password) at AndroidServiceSpace.LoginServiceReference.LoginServiceClient.getStuff(String password) in C:\AndroidServiceUUS\trunk\services\AndroidService\AndroidService\Service References\LoginServiceReference\Reference.cs:line 264 at AndroidServiceSpace.AndroidService.getStuff(String password) in C:\AndroidServiceUUS\trunk\services\AndroidService\AndroidService\AndroidService.svc.cs:line 77 at SyncInvokegetStuff(Object , Object[] , Object[] ) at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc) at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)
我的 web.config:
<?xml version="1.0"?>
<configuration>
<configSections>
</configSections>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<bindings >
<webHttpBinding>
<binding name="BasicHttpBinding_UserService" maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647">
<security mode="None" />
</binding>
</webHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost/LoginService/LoginService.svc" binding="webHttpBinding"
bindingConfiguration="BasicHttpBinding_UserService" contract="LoginServiceReference.ILoginService" />
</client>
<services>
<service behaviorConfiguration="AndroidBehaviour" name="AndroidServiceSpace.AndroidService">
<endpoint address="" behaviorConfiguration="web" binding="webHttpBinding"
contract="AndroidServiceSpace.IAndroidService" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="AndroidBehaviour">
<serviceMetadata httpGetEnabled="True" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="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="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
移动 LoginService 以删除服务器以获取 Fiddler 请求:
# Result Protocol Host URL Body Caching Content-Type Process Comments Custom
4 502 HTTP 10.0.4.12:50271 /AndroidService.svc/getStuff/ggnore 512 text/html; charset=UTF-8 chrome:4132
5 502 HTTP 10.0.4.12:50271 /favicon.ico 512 text/html; charset=UTF-8 chrome:4132
6 400 HTTP localhost:50271 /AndroidService.svc/getStuff/ggnore 4 565 private text/html chrome:4132
7 404 HTTP localhost:50271 /favicon.ico 2 310 private text/html; charset=utf-8 chrome:4132
8 200 HTTP Tunnel to clients4.google.com:443 0 chrome:4132
9 404 HTTP maker.server /LoginService/LoginService.svc/$metadata 1 565 private text/html; charset=UTF-8 vwdexpress:5772
10 200 HTTP maker.server /LoginService/LoginService.svc 2 790 private text/html; charset=UTF-8 vwdexpress:5772
11 200 HTTP maker.server /LoginService/LoginService.svc/mex 10 195 private application/soap+xml; charset=utf-8 vwdexpress:5772
12 404 HTTP maker.server /LoginService/LoginService.svc 1 565 private text/html; charset=UTF-8 vwdexpress:5772
13 200 HTTP maker.server /LoginService/LoginService.svc?disco 289 private text/xml; charset=UTF-8 vwdexpress:5772
14 200 HTTP maker.server /LoginService/LoginService.svc?wsdl 3 902 private text/xml; charset=UTF-8 vwdexpress:5772
15 200 HTTP maker.server /LoginService/LoginService.svc?xsd=xsd0 1 969 private text/xml; charset=UTF-8 vwdexpress:5772
16 200 HTTP maker.server /LoginService/LoginService.svc?xsd=xsd2 1 521 private text/xml; charset=UTF-8 vwdexpress:5772
17 200 HTTP maker.server /LoginService/LoginService.svc?xsd=xsd1 2 273 private text/xml; charset=UTF-8 vwdexpress:5772
18 200 HTTP localhost:50271 / 2 756 text/html; charset=utf-8 chrome:4132
19 404 HTTP localhost:50271 /favicon.ico 2 310 private text/html; charset=utf-8 chrome:4132
20 400 HTTP localhost:50271 /AndroidService.svc/getStuff/ggnore 4 565 private text/html chrome:4132
21 404 HTTP localhost:50271 /favicon.ico 2 310 private text/html; charset=utf-8 chrome:4132