Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
603 views
in Technique[技术] by (71.8m points)

c# - WCF: There was no endpoint listening at, that could accept the message

I have WCF Service (JSON), and ASP.NET website that we add a service reference to that site connected to the wcf service.

when i test the service using POSTMAN and SOAPUI and also when i try the following code:

WebClient client = new WebClient();
client.Headers["Content-type"] = "application/json";
client.Encoding = System.Text.Encoding.UTF8;
string result = client.UploadString("http://localhost:1122/Service.svc/GetInfo", string.Empty);

It works fine (note that all my service functions are POST).

But when i add the service to service reference an connect to the service and try to call the function GetInfo(), the following issue appears.

Exception Message

There was no endpoint listening at http://localhost:1122/Service.svc that could
accept the message. This is often caused by an incorrect address or SOAP action. 
See InnerException, if present, for more details.

Inner Exception Message

The remote server returned an error: (404) Not Found.

Server stack trace:

at System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
at System.ServiceModel.Channels.HttpChannelFactory`1.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)

I think this issue is due to incorrect configuration on the config file for the webservice and website, so you can find below both config files:

Webservice Config File:

<system.serviceModel>    
<services>
  <service name="WCFService.Service" behaviorConfiguration="ServiceBehavior">
    <endpoint address="" binding="webHttpBinding" contract="WCFService.IService" name="WebHttp_IService"
      bindingConfiguration="WebHttpBinding_IService" behaviorConfiguration="ServiceBehaviorEndpoint" />
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
  </service>
</services>

<bindings>
  <webHttpBinding>
    <binding name="WebHttpBinding_IService" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
             maxBufferSize="2147483647" openTimeout="00:20:00" receiveTimeout="00:20:00" closeTimeout="00:20:00" sendTimeout="00:20:00">
      <readerQuotas maxDepth="200" maxStringContentLength="8388608" maxArrayLength="16384" maxBytesPerRead="2147483647" maxNameTableCharCount="16384" />
    </binding>
  </webHttpBinding>
</bindings>

<behaviors>
  <serviceBehaviors>
    <behavior name="ServiceBehavior">
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true"/>
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="ServiceBehaviorEndpoint">
      <webHttp helpEnabled="true" />
    </behavior>
  </endpointBehaviors>
</behaviors>

<protocolMapping>
    <add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>    
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>

Website Config File:

<system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding name="BasicHttpBinding_IService"/>
  </basicHttpBinding>
  <webHttpBinding>
    <binding maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" 
                    maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
    </binding>
  </webHttpBinding>
</bindings>
<client>
  <endpoint address="http://localhost:1122/Service.svc" bindingConfiguration="BasicHttpBinding_IService" 
            contract="WCFServices.IService" name="BasicHttpBinding_IService" binding="basicHttpBinding" />
</client>
</system.serviceModel>

Note that the Service Reference name in the Website is WCFService, and we try to host the service to the iis with public IP and open the firewall, but the same issue appears.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

When we invoke the service which created with WebHttpBinding via client proxy class, there may be some differences from other bindings. We need to configure the client configuration manually, such as adding the endpoint behavior to service endpoint and webget/webinvoke attribute to the automatically generated operation method by adding service reference.
I have made a demo, wish it is useful to you.
Server end (console application).

class Program
    {
        static void Main(string[] args)
        {
            using (ServiceHost sh=new ServiceHost(typeof(MyService)))
            {
                sh.Opened += delegate
                {
                    Console.WriteLine("Service is ready......");
                };
                sh.Closed += delegate
                {
                    Console.WriteLine("Service is closed");
                };
                sh.Open();
                Console.ReadLine();
                sh.Close();

            }
        }
    }
    [ServiceContract]
    public interface IService
    {
        [OperationContract]
        [WebGet]
        string SayHello();
    }
    public class MyService : IService
    {
        public string SayHello()
        {
return "Hello, Busy world";        }
}

App.config

<system.serviceModel>
    <services>
      <service behaviorConfiguration="Service1Behavior" name="VM1.MyService">
        <endpoint address="" binding="webHttpBinding" contract="VM1.IService" behaviorConfiguration="rest" >
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:13008"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="Service1Behavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="False"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="rest">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>

Client(console app)

static void Main(string[] args)
{
    ServiceReference1.ServiceClient client = new ServiceReference1.ServiceClient();
    try
    {
        Console.WriteLine(client.SayHello());
    }
    catch (Exception)
    {

        throw;
    }

}

App.config

  <system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="rest">
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <client>
      <endpoint address="http://10.157.13.69:13008/" binding="webHttpBinding" contract="ServiceReference1.IService" behaviorConfiguration="rest">
      </endpoint>
    </client>
  </system.serviceModel>

We also need to add the WebGet attribute to the operation method.

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]    [System.ServiceModel.ServiceContractAttribute(ConfigurationName="ServiceReference1.IService")]
public interface IService {
            [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IService/SayHello", ReplyAction="http://tempuri.org/IService/SayHelloResponse")]
    [WebGet]
    string SayHello();

Result.
enter image description here
Feel free to let me know if there is anything I can help with.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...