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

后端 未结 1 1220
半阙折子戏
半阙折子戏 2020-12-22 06:12

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 S

相关标签:
1条回答
  • 2020-12-22 06:50

    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.

    Feel free to let me know if there is anything I can help with.

    0 讨论(0)
提交回复
热议问题