Why doesn't my ChannelFactory see my endpoint configuration?

后端 未结 5 1306
旧时难觅i
旧时难觅i 2020-12-24 00:42

I\'ve been following Miguel Castro\'s excellent article on WCF here and it\'s all working nicely, except that I have the following code

public AdminClient()
         


        
相关标签:
5条回答
  • 2020-12-24 01:10

    If you don't want to specify endpoint Name explicity, you may write:

        public AdminClient()
        {
            ChannelFactory<IProductAdmin> factory =  
               new ChannelFactory<IProductAdmin>(string.Empty);
            productAdminChannel = factory.CreateChannel();
        }
    

    Parameterless constructor unnecessary doesn't work.

    0 讨论(0)
  • 2020-12-24 01:13

    Use "*" to use the first qualifying endpoint.

    public AdminClient()
    {
        ChannelFactory<IProductAdmin> factory  
             = new ChannelFactory<IProductAdmin>("*");
    
        productAdminChannel = factory.CreateChannel();
    }
    

    MSDN Example

    0 讨论(0)
  • 2020-12-24 01:16

    This has been bugging me for a few days, so I went through the examples shown in the article you linked above. Everything works correctly except for the second client proxy example that you're having a problem with. As you noted and the other answerers creating a proxy in that way requires an endpoint name, coupling it with the client (which is where the endpoint is defined). I'm still not sure why it behaves the way it does, but I found no way to use that example without explicitly coupling the proxy to the endpoint.

    On the other hand, the first example showing how to create the proxy requires no explicit coupling of endpoint address or binding:

     using System;
       using System.ServiceModel;
    
       namespace CoDeMagazine.ServiceArticle
       {
           public class ProductClient 
              : ClientBase<IProductBrowser>, 
                IProductBrowser
           {
    
               #region IProductBrowser Members
    
               public ProductData GetProduct(
                  Guid productID)
               {
                   return Channel.GetProduct(productID);
               }
    
               public ProductData[] GetAllProducts()
               {
                   return Channel.GetAllProducts();
               }
    
               public ProductData[] FindProducts(
                  string productNameWildcard)
               {
                   return Channel.FindProducts(
                      productNameWildcard);
               }
    
               #endregion
           }
    
       }
    

    That seems to work just fine. So, maybe the second proxy example is just a poor way to do things, or maybe we're missing something obvious...

    0 讨论(0)
  • 2020-12-24 01:22

    You can get away without specifying endpoint name on service side. For client side, you need to specify the name, because you may be connecting to multiple services that have the same contract. How would WCF know then which one you want?

    0 讨论(0)
  • 2020-12-24 01:28

    You need to specify the endpoint name, because you can have many endpoints for the same type of contract. (For instance a service being deployed on one tcp and one http ws endpoint). Microsoft could have of course built something in WCF to check if there is only one client specified for the contract interface, but that wouldn't have been very consistent. (that it would work if there is only one endpoint specified for the contract). When you would add another endpoint for the same contract later, the code would break in that case.

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