WCF,ChannelFactory,“无法找到端点元素......”

时间:2009-12-29 23:32:39

标签: wcf client endpoint channelfactory

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

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

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

我收到的错误是:

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


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


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

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

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

1 个答案:

答案 0 :(得分:16)

好吧,在测试应用的代码中,您通过名称“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”的端点元素(由name=节点上的<endpoint>属性定义。)

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

 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将只使用当前唯一的元素。如果您应该有多个端点元素(例如,使用不同的协议),则此调用未指定要使用的端点元素将引发异常,您必须更改它以指定要使用的端点元素(通过其名称)也是。

相关问题