0

我有一个 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         
4

1 回答 1

1

显示与您类似的场景:

登录服务:

[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json,UriTemplate = "getStuff/{password}")]
string getStuff(string password);

安卓服务:

[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json,UriTemplate = "getStuff/{password}")]
string getStuff(string password);

现在我的 LoginService 实现:

public string getStuff(string password)
{
    if ( password == "test")
          return "Success";
    else
          return "Failure";
}

现在我的 AndroidService 实现:

public string getStuff(string password)
{
    // NOTE: Below 2 lines to acccess the login service via SOAP 
    // assuming that you have added a service reference to your project and added the needed config entries in you androidservice config file
    LoginServiceClient client = new LoginServiceClient(binding, address);
    return client.getStuff(password);

     // NOTE: Below code to access it RESTfully 
     string responseMessage = null;
     var request = WebRequest.Create("http://localhost/LoginService/LoginService.svc/getStuff/" + password) as HttpWebRequest;
    if (request != null)
    {
        request.ContentType = "application/json";      
        request.Method = "GET";
        var response = request.GetResponse() as HttpWebResponse;
                if(response.StatusCode == HttpStatusCode.OK)
                {
                    Stream responseStream = response.GetResponseStream();
                    if (responseStream != null)
                    {
                        var reader = new StreamReader(responseStream);

                        responseMessage = reader.ReadToEnd();                        
                    }
                }
                else
                {
                    responseMessage = response.StatusDescription;
                }
    }
    if (responseMessage == "Success")
    {
        return "Success";
    }
    else
    {
        return "Failure";
    }
}

现在从提琴手我执行以下请求(显示为原始)

GET http://localhost:50271/AndroidService.svc/getStuff/ggnore HTTP/1.1
Host: localhost
Content-Type: application/json

Web.config 如图所示:

<?xml version="1.0"?>
<configuration>
<configSections>
</configSections>
<system.web>
  <compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
  <bindings >
      <webHttpBinding>
          <binding maxBufferSize="2147483647"
              maxReceivedMessageSize="2147483647">
              <security mode="None" />
          </binding>
      </webHttpBinding>
      <basicHttpBinding>
           <binding name="BasicHttpBinding_ILoginService" closeTimeout="00:01:00"
      openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
      allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
      maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
      messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
      useDefaultWebProxy="true">
      <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
      <security mode="None">
        <transport clientCredentialType="None" proxyCredentialType="None"
          realm="" />
        <message clientCredentialType="UserName" algorithmSuite="Default" />
      </security>
    </binding>
      </basicHttpBinding>
  </bindings>
  <client>
        <endpoint address="http://maker.server/LoginService/LoginService.svc"
            binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ILoginService"
    contract="ILoginService" name="BasicHttpBinding_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 作为单独的 RESTful 服务托管,因此使用 WebRequestClass 访问它。如果 LoginService 在同一个项目中,那么为 LoginService 定义一个类似于 AndroidService 的服务元素就足够了。

于 2012-05-09T11:46:24.733 回答