Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Webservice返回EndPointNotFoundException_C#_Wcf_Web Services_Rest - Fatal编程技术网

C# Webservice返回EndPointNotFoundException

C# Webservice返回EndPointNotFoundException,c#,wcf,web-services,rest,C#,Wcf,Web Services,Rest,我从另一个web服务(AndroidService)调用了一个web服务(LoginService)。我有一个测试连接的简单方法,但它不起作用 在LoginService中,我有一个名为getStuff(字符串密码)的方法—它返回JSON格式的字符串。当我通过浏览器直接访问它时,它可以正常工作。当我通过另一个web服务尝试相同的操作时,它失败了,主题中出现异常,内部异常为“服务器返回404 Not Found” 以下是LoginService定义: [WebInvoke(Meth

我从另一个web服务(AndroidService)调用了一个web服务(LoginService)。我有一个测试连接的简单方法,但它不起作用

在LoginService中,我有一个名为getStuff(字符串密码)的方法—它返回JSON格式的字符串。当我通过浏览器直接访问它时,它可以正常工作。当我通过另一个web服务尝试相同的操作时,它失败了,主题中出现异常,内部异常为“服务器返回404 Not Found”

以下是LoginService定义:

        [WebInvoke(Method = "GET",
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Wrapped,
        UriTemplate = "getStuff/{password}")]
        string getStuff(string password);
    [OperationContract]
    [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);
    }
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json,UriTemplate = "getStuff/{password}")]
string getStuff(string password);
以及AndroidService定义:

        [WebInvoke(Method = "GET",
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Wrapped,
        UriTemplate = "getStuff/{password}")]
        string getStuff(string password);
    [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)
My 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>

显示了与您类似的场景:

登录服务:

[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json,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);
    }
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json,UriTemplate = "getStuff/{password}")]
string getStuff(string password);
现在,我的登录服务实现:

public string getStuff(string password)
{
    if ( password == "test")
          return "Success";
    else
          return "Failure";
}
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";
    }
}
现在,我的AndroidService实现:

public string getStuff(string password)
{
    if ( password == "test")
          return "Success";
    else
          return "Failure";
}
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";
    }
}
现在,我从Fiddler执行以下请求(以原始格式显示)

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的服务元素就足够了。

我想您需要使用WebHttpBinding而不是BasicHttpBinding,因为您的服务似乎是REST,andriod使用REST而不是SOAP访问它。将其更改为WebHttpBinding,现在我得到了InvalidOperationException,表示“此工厂已启用手动寻址,因此发送的所有邮件都必须预寻址。”=(您是否尝试从Fiddler访问服务,并查看得到的响应。另外,请发布您的服务配置文件posted web.config,将代码更改为client=new LoginServiceClient();而不是客户端=新的LoginServiceClient(绑定,地址);你能发布fiddler的原始请求吗?如果我没有错的话,我看到你从andriodservice调用loginservice。对吗?是的,我可以通过手动发出http请求来让它工作,但这会使整个实现有点模糊,必须为每个实现定义请求的url。--我想这样做LoginServiceClient=new LoginServiceClient();然后通过client.getStuff(“string”)使用这些方法;使用LoginServiceClient=new LoginServiceClient()的过程;指使用基于SOAP的web服务,也可以通过在AndroidService中添加LoginService的服务引用,并使用上述代码,而不是使用WebRequest类对其执行调用来实现。我已将LoginService的服务引用添加到AndroidService。当我键入LoginServiceClient=new LoginServi时ceClient();然后是客户端。它建议使用getStuff方法,因此我知道它可以看到公开的web服务。它不起作用,我完全不知道如何让它工作。您可以发布LoginService的服务器配置吗?也可以在androidService上查看我更新的web.config,其中有一个客户端元素引用LoginService端点及其应用程序关联的BasicHttpBinding元素(假设登录服务在没有任何安全性的情况下使用BasicHttpBinding公开)将您的答案标记为正确,因为它会有效地给出使用loginservice作为rest的正确结果。我启动了一个新项目,复制粘贴了其中的所有内容,现在它可以工作了。:S