Consume external WCF service using channel factory and not by using proxy or adding service reference












0















I would like to know if there is a possibility to consume an external wcf service (by external wcf service i mean a service that is not part of my solution) using channel factory. I'm aware that we can consume by generating a proxy or adding service reference but i want to know if we can use channel factory. Since its an external service we will not be having the interface class with use so need to know how will the channel factory instance look like?










share|improve this question



























    0















    I would like to know if there is a possibility to consume an external wcf service (by external wcf service i mean a service that is not part of my solution) using channel factory. I'm aware that we can consume by generating a proxy or adding service reference but i want to know if we can use channel factory. Since its an external service we will not be having the interface class with use so need to know how will the channel factory instance look like?










    share|improve this question

























      0












      0








      0








      I would like to know if there is a possibility to consume an external wcf service (by external wcf service i mean a service that is not part of my solution) using channel factory. I'm aware that we can consume by generating a proxy or adding service reference but i want to know if we can use channel factory. Since its an external service we will not be having the interface class with use so need to know how will the channel factory instance look like?










      share|improve this question














      I would like to know if there is a possibility to consume an external wcf service (by external wcf service i mean a service that is not part of my solution) using channel factory. I'm aware that we can consume by generating a proxy or adding service reference but i want to know if we can use channel factory. Since its an external service we will not be having the interface class with use so need to know how will the channel factory instance look like?







      wcf wcf-data-services wcf-binding






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 12:24









      user1531912user1531912

      1321216




      1321216
























          2 Answers
          2






          active

          oldest

          votes


















          0














          You would need to mimic the interface the service has by looking at the WSDL file(metadata file on the service)



          Then you can use a few helper methods to initialise your service,



          public static TChannel GetBasicHttpService<TChannel>(string serviceEndpoint) where TChannel : class
          {
          EndpointAddress myEndpoint = new EndpointAddress(serviceEndpoint);
          ChannelFactory<TChannel> myChannelFactory = new ChannelFactory<TChannel>(DefaultHttpBinding(), myEndpoint);

          // Create a channel.
          return myChannelFactory.CreateChannel();
          }

          public static BasicHttpBinding DefaultHttpBinding()
          {
          BasicHttpBinding defaultBinding = new BasicHttpBinding();
          defaultBinding.MaxReceivedMessageSize = 2147483647;
          defaultBinding.MaxBufferPoolSize = 2147483647;
          defaultBinding.MaxBufferSize = 2147483647;
          defaultBinding.ReaderQuotas.MaxArrayLength = 2147483647;
          defaultBinding.ReaderQuotas.MaxStringContentLength = 2147483647;

          return defaultBinding;
          }


          where TChannel is the Mimicked interfaced






          share|improve this answer































            0














            You should know the format of the service contract interface and endpoint, or we could not create the channel factory. The reason why the channel factory is used to invoke the service is that in order to protect the WCF service, server-side disable publishing service metadata. I have made a simple demo, wish it is useful to you.
            Server-side.



            class Program
            {
            static void Main(string args)
            {
            Uri uri = new Uri("http://localhost:1900");
            BasicHttpBinding binding = new BasicHttpBinding();
            using (ServiceHost sh=new ServiceHost(typeof(MyService),uri))
            {
            sh.AddServiceEndpoint(typeof(IService), binding, "");
            sh.Open();
            Console.WriteLine("Service is ready...");

            Console.ReadLine();
            sh.Close();
            }

            }
            }
            [ServiceContract(Namespace ="mydomain")]
            public interface IService
            {
            [OperationContract(Name ="AddInt")]
            int Add1(int x, int y);

            }
            public class MyService : IService
            {
            public int Add(int x, int y)
            {
            return x + y;
            }
            }


            Client-side.



            class Program
            {
            static void Main(string args)
            {
            Uri uri = new Uri("http://localhost:1900");
            BasicHttpBinding binding = new BasicHttpBinding();
            using (ChannelFactory<IService> factory = new ChannelFactory<IService>(binding, new EndpointAddress(uri)))
            {
            IService sv = factory.CreateChannel();
            var result = sv.Add(34, 3);
            try
            {
            Console.WriteLine(result);
            }
            catch (Exception ex)
            {
            throw;
            }
            }
            }
            }
            [ServiceContract(Namespace = "mydomain")]
            public interface IService
            {
            [OperationContract(Name = "AddInt")]
            int Add2(int x, int y);
            }


            There is no need to make sure that the client and the server has a same service interface, but they at least need to ensure that the namespace and name property of the interface is consistent between the client and server.
            Feel free to let me know if there is anything I can help with.






            share|improve this answer
























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446695%2fconsume-external-wcf-service-using-channel-factory-and-not-by-using-proxy-or-add%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              You would need to mimic the interface the service has by looking at the WSDL file(metadata file on the service)



              Then you can use a few helper methods to initialise your service,



              public static TChannel GetBasicHttpService<TChannel>(string serviceEndpoint) where TChannel : class
              {
              EndpointAddress myEndpoint = new EndpointAddress(serviceEndpoint);
              ChannelFactory<TChannel> myChannelFactory = new ChannelFactory<TChannel>(DefaultHttpBinding(), myEndpoint);

              // Create a channel.
              return myChannelFactory.CreateChannel();
              }

              public static BasicHttpBinding DefaultHttpBinding()
              {
              BasicHttpBinding defaultBinding = new BasicHttpBinding();
              defaultBinding.MaxReceivedMessageSize = 2147483647;
              defaultBinding.MaxBufferPoolSize = 2147483647;
              defaultBinding.MaxBufferSize = 2147483647;
              defaultBinding.ReaderQuotas.MaxArrayLength = 2147483647;
              defaultBinding.ReaderQuotas.MaxStringContentLength = 2147483647;

              return defaultBinding;
              }


              where TChannel is the Mimicked interfaced






              share|improve this answer




























                0














                You would need to mimic the interface the service has by looking at the WSDL file(metadata file on the service)



                Then you can use a few helper methods to initialise your service,



                public static TChannel GetBasicHttpService<TChannel>(string serviceEndpoint) where TChannel : class
                {
                EndpointAddress myEndpoint = new EndpointAddress(serviceEndpoint);
                ChannelFactory<TChannel> myChannelFactory = new ChannelFactory<TChannel>(DefaultHttpBinding(), myEndpoint);

                // Create a channel.
                return myChannelFactory.CreateChannel();
                }

                public static BasicHttpBinding DefaultHttpBinding()
                {
                BasicHttpBinding defaultBinding = new BasicHttpBinding();
                defaultBinding.MaxReceivedMessageSize = 2147483647;
                defaultBinding.MaxBufferPoolSize = 2147483647;
                defaultBinding.MaxBufferSize = 2147483647;
                defaultBinding.ReaderQuotas.MaxArrayLength = 2147483647;
                defaultBinding.ReaderQuotas.MaxStringContentLength = 2147483647;

                return defaultBinding;
                }


                where TChannel is the Mimicked interfaced






                share|improve this answer


























                  0












                  0








                  0







                  You would need to mimic the interface the service has by looking at the WSDL file(metadata file on the service)



                  Then you can use a few helper methods to initialise your service,



                  public static TChannel GetBasicHttpService<TChannel>(string serviceEndpoint) where TChannel : class
                  {
                  EndpointAddress myEndpoint = new EndpointAddress(serviceEndpoint);
                  ChannelFactory<TChannel> myChannelFactory = new ChannelFactory<TChannel>(DefaultHttpBinding(), myEndpoint);

                  // Create a channel.
                  return myChannelFactory.CreateChannel();
                  }

                  public static BasicHttpBinding DefaultHttpBinding()
                  {
                  BasicHttpBinding defaultBinding = new BasicHttpBinding();
                  defaultBinding.MaxReceivedMessageSize = 2147483647;
                  defaultBinding.MaxBufferPoolSize = 2147483647;
                  defaultBinding.MaxBufferSize = 2147483647;
                  defaultBinding.ReaderQuotas.MaxArrayLength = 2147483647;
                  defaultBinding.ReaderQuotas.MaxStringContentLength = 2147483647;

                  return defaultBinding;
                  }


                  where TChannel is the Mimicked interfaced






                  share|improve this answer













                  You would need to mimic the interface the service has by looking at the WSDL file(metadata file on the service)



                  Then you can use a few helper methods to initialise your service,



                  public static TChannel GetBasicHttpService<TChannel>(string serviceEndpoint) where TChannel : class
                  {
                  EndpointAddress myEndpoint = new EndpointAddress(serviceEndpoint);
                  ChannelFactory<TChannel> myChannelFactory = new ChannelFactory<TChannel>(DefaultHttpBinding(), myEndpoint);

                  // Create a channel.
                  return myChannelFactory.CreateChannel();
                  }

                  public static BasicHttpBinding DefaultHttpBinding()
                  {
                  BasicHttpBinding defaultBinding = new BasicHttpBinding();
                  defaultBinding.MaxReceivedMessageSize = 2147483647;
                  defaultBinding.MaxBufferPoolSize = 2147483647;
                  defaultBinding.MaxBufferSize = 2147483647;
                  defaultBinding.ReaderQuotas.MaxArrayLength = 2147483647;
                  defaultBinding.ReaderQuotas.MaxStringContentLength = 2147483647;

                  return defaultBinding;
                  }


                  where TChannel is the Mimicked interfaced







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 23 '18 at 16:02









                  mahlatsemahlatse

                  1,012519




                  1,012519

























                      0














                      You should know the format of the service contract interface and endpoint, or we could not create the channel factory. The reason why the channel factory is used to invoke the service is that in order to protect the WCF service, server-side disable publishing service metadata. I have made a simple demo, wish it is useful to you.
                      Server-side.



                      class Program
                      {
                      static void Main(string args)
                      {
                      Uri uri = new Uri("http://localhost:1900");
                      BasicHttpBinding binding = new BasicHttpBinding();
                      using (ServiceHost sh=new ServiceHost(typeof(MyService),uri))
                      {
                      sh.AddServiceEndpoint(typeof(IService), binding, "");
                      sh.Open();
                      Console.WriteLine("Service is ready...");

                      Console.ReadLine();
                      sh.Close();
                      }

                      }
                      }
                      [ServiceContract(Namespace ="mydomain")]
                      public interface IService
                      {
                      [OperationContract(Name ="AddInt")]
                      int Add1(int x, int y);

                      }
                      public class MyService : IService
                      {
                      public int Add(int x, int y)
                      {
                      return x + y;
                      }
                      }


                      Client-side.



                      class Program
                      {
                      static void Main(string args)
                      {
                      Uri uri = new Uri("http://localhost:1900");
                      BasicHttpBinding binding = new BasicHttpBinding();
                      using (ChannelFactory<IService> factory = new ChannelFactory<IService>(binding, new EndpointAddress(uri)))
                      {
                      IService sv = factory.CreateChannel();
                      var result = sv.Add(34, 3);
                      try
                      {
                      Console.WriteLine(result);
                      }
                      catch (Exception ex)
                      {
                      throw;
                      }
                      }
                      }
                      }
                      [ServiceContract(Namespace = "mydomain")]
                      public interface IService
                      {
                      [OperationContract(Name = "AddInt")]
                      int Add2(int x, int y);
                      }


                      There is no need to make sure that the client and the server has a same service interface, but they at least need to ensure that the namespace and name property of the interface is consistent between the client and server.
                      Feel free to let me know if there is anything I can help with.






                      share|improve this answer




























                        0














                        You should know the format of the service contract interface and endpoint, or we could not create the channel factory. The reason why the channel factory is used to invoke the service is that in order to protect the WCF service, server-side disable publishing service metadata. I have made a simple demo, wish it is useful to you.
                        Server-side.



                        class Program
                        {
                        static void Main(string args)
                        {
                        Uri uri = new Uri("http://localhost:1900");
                        BasicHttpBinding binding = new BasicHttpBinding();
                        using (ServiceHost sh=new ServiceHost(typeof(MyService),uri))
                        {
                        sh.AddServiceEndpoint(typeof(IService), binding, "");
                        sh.Open();
                        Console.WriteLine("Service is ready...");

                        Console.ReadLine();
                        sh.Close();
                        }

                        }
                        }
                        [ServiceContract(Namespace ="mydomain")]
                        public interface IService
                        {
                        [OperationContract(Name ="AddInt")]
                        int Add1(int x, int y);

                        }
                        public class MyService : IService
                        {
                        public int Add(int x, int y)
                        {
                        return x + y;
                        }
                        }


                        Client-side.



                        class Program
                        {
                        static void Main(string args)
                        {
                        Uri uri = new Uri("http://localhost:1900");
                        BasicHttpBinding binding = new BasicHttpBinding();
                        using (ChannelFactory<IService> factory = new ChannelFactory<IService>(binding, new EndpointAddress(uri)))
                        {
                        IService sv = factory.CreateChannel();
                        var result = sv.Add(34, 3);
                        try
                        {
                        Console.WriteLine(result);
                        }
                        catch (Exception ex)
                        {
                        throw;
                        }
                        }
                        }
                        }
                        [ServiceContract(Namespace = "mydomain")]
                        public interface IService
                        {
                        [OperationContract(Name = "AddInt")]
                        int Add2(int x, int y);
                        }


                        There is no need to make sure that the client and the server has a same service interface, but they at least need to ensure that the namespace and name property of the interface is consistent between the client and server.
                        Feel free to let me know if there is anything I can help with.






                        share|improve this answer


























                          0












                          0








                          0







                          You should know the format of the service contract interface and endpoint, or we could not create the channel factory. The reason why the channel factory is used to invoke the service is that in order to protect the WCF service, server-side disable publishing service metadata. I have made a simple demo, wish it is useful to you.
                          Server-side.



                          class Program
                          {
                          static void Main(string args)
                          {
                          Uri uri = new Uri("http://localhost:1900");
                          BasicHttpBinding binding = new BasicHttpBinding();
                          using (ServiceHost sh=new ServiceHost(typeof(MyService),uri))
                          {
                          sh.AddServiceEndpoint(typeof(IService), binding, "");
                          sh.Open();
                          Console.WriteLine("Service is ready...");

                          Console.ReadLine();
                          sh.Close();
                          }

                          }
                          }
                          [ServiceContract(Namespace ="mydomain")]
                          public interface IService
                          {
                          [OperationContract(Name ="AddInt")]
                          int Add1(int x, int y);

                          }
                          public class MyService : IService
                          {
                          public int Add(int x, int y)
                          {
                          return x + y;
                          }
                          }


                          Client-side.



                          class Program
                          {
                          static void Main(string args)
                          {
                          Uri uri = new Uri("http://localhost:1900");
                          BasicHttpBinding binding = new BasicHttpBinding();
                          using (ChannelFactory<IService> factory = new ChannelFactory<IService>(binding, new EndpointAddress(uri)))
                          {
                          IService sv = factory.CreateChannel();
                          var result = sv.Add(34, 3);
                          try
                          {
                          Console.WriteLine(result);
                          }
                          catch (Exception ex)
                          {
                          throw;
                          }
                          }
                          }
                          }
                          [ServiceContract(Namespace = "mydomain")]
                          public interface IService
                          {
                          [OperationContract(Name = "AddInt")]
                          int Add2(int x, int y);
                          }


                          There is no need to make sure that the client and the server has a same service interface, but they at least need to ensure that the namespace and name property of the interface is consistent between the client and server.
                          Feel free to let me know if there is anything I can help with.






                          share|improve this answer













                          You should know the format of the service contract interface and endpoint, or we could not create the channel factory. The reason why the channel factory is used to invoke the service is that in order to protect the WCF service, server-side disable publishing service metadata. I have made a simple demo, wish it is useful to you.
                          Server-side.



                          class Program
                          {
                          static void Main(string args)
                          {
                          Uri uri = new Uri("http://localhost:1900");
                          BasicHttpBinding binding = new BasicHttpBinding();
                          using (ServiceHost sh=new ServiceHost(typeof(MyService),uri))
                          {
                          sh.AddServiceEndpoint(typeof(IService), binding, "");
                          sh.Open();
                          Console.WriteLine("Service is ready...");

                          Console.ReadLine();
                          sh.Close();
                          }

                          }
                          }
                          [ServiceContract(Namespace ="mydomain")]
                          public interface IService
                          {
                          [OperationContract(Name ="AddInt")]
                          int Add1(int x, int y);

                          }
                          public class MyService : IService
                          {
                          public int Add(int x, int y)
                          {
                          return x + y;
                          }
                          }


                          Client-side.



                          class Program
                          {
                          static void Main(string args)
                          {
                          Uri uri = new Uri("http://localhost:1900");
                          BasicHttpBinding binding = new BasicHttpBinding();
                          using (ChannelFactory<IService> factory = new ChannelFactory<IService>(binding, new EndpointAddress(uri)))
                          {
                          IService sv = factory.CreateChannel();
                          var result = sv.Add(34, 3);
                          try
                          {
                          Console.WriteLine(result);
                          }
                          catch (Exception ex)
                          {
                          throw;
                          }
                          }
                          }
                          }
                          [ServiceContract(Namespace = "mydomain")]
                          public interface IService
                          {
                          [OperationContract(Name = "AddInt")]
                          int Add2(int x, int y);
                          }


                          There is no need to make sure that the client and the server has a same service interface, but they at least need to ensure that the namespace and name property of the interface is consistent between the client and server.
                          Feel free to let me know if there is anything I can help with.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 27 '18 at 3:05









                          Abraham QianAbraham Qian

                          1,071119




                          1,071119






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446695%2fconsume-external-wcf-service-using-channel-factory-and-not-by-using-proxy-or-add%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              這個網誌中的熱門文章

                              Xamarin.form Move up view when keyboard appear

                              Post-Redirect-Get with Spring WebFlux and Thymeleaf

                              Anylogic : not able to use stopDelay()