【问题标题】:WCF, ChannelFactory, "Could not find endpoint element..."WCF,ChannelFactory,“找不到端点元素...”
【发布时间】:2010-12-30 23:01:01
【问题描述】:

我正在尝试从另一个服务中调用 WCF 服务,部分使用了我在 StackOverflow 上找到的实现 ChannelFactory 的示例。

我在我的测试解决方案中创建了一个单独的控制台应用程序项目(VS 2008,顺便说一句),

namespace MyService.Test
{
    class Program
    {
        static void Main(string[] args)
        {
            MySolution.MyTestClient proxy = new MyTestClient();

            proxy = new MyTestClient();
            proxy.Endpoint.Address = new EndpointAddress("http://localhost:8723/MySolution/");

            // Instantiate a request object and assign values to its member variables.    
            MySolution.RemoteServiceMethod() theObject = new RemoteServiceMethod();            
            theObject.SomeProperty = "123";
            theObject.SomeOtherProperty = "alpha";

            Console.WriteLine("Calling the remote service method now...");

            try
            {
                proxy.SubmitRemoteServiceRequest(proxy.theObject);
            }
            catch (FaultException<MySolution.RequestException> e)
            {
                // exception code hereMySolution
            }
        }
    }
}

这是来自本地服务的 App.Config 显示的端点:

<system.serviceModel>
  <client>
   <endpoint address="http://MyService/MyService.asmx"
     binding="basicHttpBinding" bindingConfiguration="ServiceSoap"
     contract="ServiceReference.ServiceSoap"
     name="ServiceSoap" />
  </client>
  ...
 </system.serviceModel>  

这是来自测试项目自己的App.Config

 <client>
      <endpoint address="http://localhost:8723/MyService"
        binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IServiceContract"
        contract="ServiceContract.IServiceContract" name="BasicHttpBinding_IServiceContract" />
 </client>  

这是我在本地服务中公开的方法,它反过来将请求对象传递给远程服务:

public ServiceContract.Response.ZZZ_Response SubmitRemoteServiceRequest(ServiceContract.Request.ZZZ_Request sc_TheirServiceRequest)
{
     var factory = new ChannelFactory<ServiceReference.ServiceSoap>("ServiceSoap");
     var wcfClient = factory.CreateChannel();
     bool closedSuccessfully = false;

     // Instantiate a response object which I will return to the consumer.
     ServiceContract.Response.ZZZ_Response zzz_Response = new ServiceContract.Response.ZZZ_Response();

     // Instantiate request and response objects, respectively, which I use internal to my service to call remote service.
     ServiceReference.MyServiceRequest scMyServiceRequest = new ServiceReference.MyServiceRequest();

     ServiceReference.MyServiceResponse scMyServiceResponse = new ServiceReference.MyServiceResponse();

     try
     {                
          // Now you can make calls on the wcfClient object
          scMyServiceResponse = wcfClient.MyServiceMethod(scMyServiceRequest );                

          ((ICommunicationObject)wcfClient).Close();
          closedSuccessfully = true;
     }
     finally
     {
          if (!closedSuccessfully)
          {
               ((ICommunicationObject)wcfClient).Abort();
          }
     }

     return zzz_Response;
}

我收到的错误是这样的:

在 ServiceModel 客户端配置部分找不到名为“ServiceSoap”和合同“ServiceReference.ServiceSoap”的端点元素。这可能是因为没有为您的应用程序找到配置文件,或者因为在客户端元素中找不到与此名称匹配的端点元素。


我很困惑,我到底在哪里缺少端点元素,在本地服务的 app.config(定义了指向远程服务的服务引用),测试应用的 app.config,还是其他地方?!


更新:我在阅读 this MSDN article 后找到了一种解决方法,或者我猜只是实例化 ChannelFactory 的另一种方式:

var factory = new ChannelFactory<ServiceReference.ServiceSoap>(new BasicHttpBinding(), "http://urlToRemoteService/RemoteService.asmx");

我没有尝试从 app.config 中不存在或不正确的信息中获取信息,而是手动插入绑定 (BasicHttpBinding) 的值和远程服务的地址。这似乎让我摆脱了“找不到端点元素”错误,但我不确定这是否是最佳解决方案。

【问题讨论】:

    标签: wcf client endpoint channelfactory


    【解决方案1】:

    好吧,在您的测试应用程序代码中,您通过名称“ServiceSoap”请求端点元素:

     var factory = new ChannelFactory<ServiceReference.ServiceSoap>("ServiceSoap");
    

    但在您的配置中,没有这样的端点:

     <client>
          <endpoint address="http://localhost:8723/DelinquencyService"
            binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IServiceContract"
            contract="ServiceContract.IServiceContract" name="BasicHttpBinding_IServiceContract" />
     </client>  
    

    配置包含一个名为“BasicHttpBinding_IServiceContract”的端点元素(由&lt;endpoint&gt; 节点上的name= 属性定义)。

    因此,要么您更改测试应用程序中的行以请求该端点元素:

     var factory = new ChannelFactory<ServiceReference.ServiceSoap>("BasicHttpBinding_IServiceContract");
    

    或者您将测试应用的 app.config 更改为使用 ServiceSoap 作为名称:

    <client>
       <endpoint name="ServiceSoap"
            address="http://localhost:8723/DelinquencyService"
            binding="basicHttpBinding" 
            bindingConfiguration="BasicHttpBinding_IServiceContract"
            contract="ServiceContract.IServiceContract" />
    </client>  
    

    两者中的任何一个都可以解决您的问题。

    在您的“普通”客户端应用程序中,这是有效的,因为当您创建客户端代理时,您根本没有指定端点元素名称 - WCF 将只使用唯一存在的元素。如果您应该有多个端点元素(例如使用不同的协议),则此调用未指定要使用的端点元素将引发异常,并且您必须更改它以指定要使用的端点元素(通过其名称) ,也是。

    【讨论】:

    • 马克,非常感谢您的回复!有一件事,您在回复顶部提到的作为我的测试应用程序代码的一部分的部分实际上是来自本地服务中的代码,我试图用它来调用远程服务,来自测试应用程序的块出现在我的问题的顶部。测试应用程序可以很好地与我的本地服务通信,但异常发生在本地服务尝试创建 ChannelFactory 以与远程服务通信时。抱歉,如果这看起来不清楚,我正在努力不让自己感到困惑,更不用说我正在寻求帮助的人了。
    • 感谢您的帮助,Marc,我已经接受并接受了,它澄清了无论如何我应该做什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-02
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 2018-07-09
    相关资源
    最近更新 更多