WCF:没有端点在监听,可以接受消息

时间:2019-01-03 07:35:44

标签: c# asp.net json wcf

我有WCF服务(JSON),还有ASP.NET网站,我们向连接到wcf服务的该站点添加了服务引用。

当我使用POSTMAN和SOAPUI测试服务时,以及尝试以下代码时:

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);

它工作正常(请注意,我所有的服务功能都是POST)。

但是当我将服务添加到服务中时,引用该服务的连接并尝试调用函数GetInfo(),则会出现以下问题。

异常消息

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.

内部异常消息

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

服务器堆栈跟踪:

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)

我认为此问题是由于Web服务和网站的配置文件上的配置错误所致,因此您可以在以下两个配置文件中找到:

Web服务配置文件:

<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>

网站配置文件:

<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>

请注意,网站中的服务引用名称为WCFService,我们尝试使用公共IP将服务托管到iis并打开防火墙,但是会出现相同的问题。

1 个答案:

答案 0 :(得分:0)

当我们通过客户端代理类调用使用WebHttpBinding创建的服务时,可能与其他绑定有所不同。我们需要手动配置客户端配置,例如将端点行为添加到服务端点,并通过添加服务引用将webget / webinvoke属性添加到自动生成的操作方法中。
我做了一个演示,希望对您有用。
服务器端(控制台应用程序)。

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>

客户端(控制台应用程序)

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>

我们还需要将WebGet属性添加到操作方法。

[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();

结果。
enter image description here
随时让我知道是否有什么可以帮助您的。