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

Dar*_*ent 10 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
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

<system.serviceModel>
  <client>
   <endpoint address="http://MyService/MyService.asmx"
     binding="basicHttpBinding" bindingConfiguration="ServiceSoap"
     contract="ServiceReference.ServiceSoap"
     name="ServiceSoap" />
  </client>
  ...
 </system.serviceModel>  
Run Code Online (Sandbox Code Playgroud)

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

 <client>
      <endpoint address="http://localhost:8723/MyService"
        binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IServiceContract"
        contract="ServiceContract.IServiceContract" name="BasicHttpBinding_IServiceContract" />
 </client>  
Run Code Online (Sandbox Code Playgroud)

这是我在本地服务中公开的方法,后者又将请求对象传递给远程服务:

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;
}
Run Code Online (Sandbox Code Playgroud)

我收到的错误是这样的:

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


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


更新:我在阅读这篇MSDN文章之后找到了一种解决方法,或者我想只是一种实例化ChannelFactory的不同方式:

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

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

mar*_*c_s 16

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

 var factory = new ChannelFactory<ServiceReference.ServiceSoap>("ServiceSoap");
Run Code Online (Sandbox Code Playgroud)

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

 <client>
      <endpoint address="http://localhost:8723/DelinquencyService"
        binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IServiceContract"
        contract="ServiceContract.IServiceContract" name="BasicHttpBinding_IServiceContract" />
 </client>  
Run Code Online (Sandbox Code Playgroud)

该配置包含一个名为"BasicHttpBinding_IServiceContract"的端点元素(由name=<endpoint>节点上的属性定义).

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

 var factory = new ChannelFactory<ServiceReference.ServiceSoap>("BasicHttpBinding_IServiceContract");
Run Code Online (Sandbox Code Playgroud)

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

<client>
   <endpoint name="ServiceSoap"
        address="http://localhost:8723/DelinquencyService"
        binding="basicHttpBinding" 
        bindingConfiguration="BasicHttpBinding_IServiceContract"
        contract="ServiceContract.IServiceContract" />
</client>  
Run Code Online (Sandbox Code Playgroud)

两者中的任何一个都应该解决你的问题.

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