WCF Self-Host to access from internet

ⅰ亾dé卋堺 提交于 2020-08-10 20:12:48

问题


I am trying to create a service to consume it through the internet, but for some reason I cannot access it and I need help finding the error I am making. I leave the code for you to see.

 public class ServiceHost<T> : System.ServiceModel.ServiceHost
    {

        public ServiceHost(Type serviceType, params Uri[] baseAddresses)
            : base(serviceType, baseAddresses)
        {
        }

        public ServiceHost(object singletonInstance, params Uri[] baseAddresses)
            :base(singletonInstance, baseAddresses)
        {

        }
        protected ServiceHost() 
            : base()
        {

        }
        public void EnableMetadataExchange(bool enableHttpGet = true)
        {
            if (State == CommunicationState.Opened)
            {
                throw new InvalidOperationException("La comunicación ya está abierta");
            }
            ServiceMetadataBehavior metadataBehavior
                                = Description.Behaviors.Find<ServiceMetadataBehavior>();

            if (metadataBehavior == null)
            {
                metadataBehavior = new ServiceMetadataBehavior();
                Description.Behaviors.Add(metadataBehavior);

                if (BaseAddresses.Any(uri => uri.Scheme == "http"))
                    metadataBehavior.HttpGetEnabled = enableHttpGet;
                }
            AddAllMexEndPoints();
        }
        public bool HasMexEndpoint
        {
            get
            {
                return Description.Endpoints.Any(
                                              endpoint => endpoint.Contract.ContractType ==
                                              typeof(IMetadataExchange));
            }
        }

        private void AddAllMexEndPoints()
        {
            Debug.Assert(HasMexEndpoint == false);
            foreach (Uri baseAddress in BaseAddresses)
            {
                Binding binding = null;

                switch (baseAddress.Scheme)
                {
                    case "net.tcp":
                        {
                            binding = MetadataExchangeBindings.CreateMexTcpBinding();
                            break;
                        }
                    case "http":
                        {
                            binding = MetadataExchangeBindings.CreateMexHttpBinding();
                            break;
                        }
                    case "https":
                        {
                            binding = MetadataExchangeBindings.CreateMexHttpsBinding();
                            break;
                        }
                    case "net.pipe":
                        {
                            binding = MetadataExchangeBindings.CreateMexNamedPipeBinding();
                            break;
                        }
                }
                if (binding != null)
                {
                    AddServiceEndpoint(typeof(IMetadataExchange), binding, "MEX");
                }
            }
        }
    }

Hosting

        public void HostService()
    {
        try
        {
            Uri tcpBaseAddress = new Uri("net.tcp://192.168.1.110:28620/");
            Uri httpBaseAddress = new Uri("http://192.168.1.110:28621/");
            ServiceHost<wesling.Services.GC> host = new ServiceHost<wesling.Services.GC>(typeof(wesling.Services.GC), tcpBaseAddress, httpBaseAddress);
            //add tcp binding
            var netTcpBinding = new NetTcpBinding()
            {
                MaxBufferPoolSize = int.MaxValue,
                MaxReceivedMessageSize = int.MaxValue,
                ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas()
                {
                    MaxStringContentLength = int.MaxValue
                },
            };
            netTcpBinding.Security.Mode = SecurityMode.None;
            host.AddServiceEndpoint(typeof(wesling.Services.IGC), netTcpBinding, "GC");

            //add WSHttp binding
            var httpBinding = new WSHttpBinding()
            {
                MaxBufferPoolSize = int.MaxValue,
                MaxReceivedMessageSize = int.MaxValue,
                ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas()
                {
                    MaxStringContentLength = int.MaxValue
                },
            };
            httpBinding.Security.Mode = SecurityMode.None;
            host.AddServiceEndpoint(typeof(wesling.Services.IGC), httpBinding, "GC");

            host.EnableMetadataExchange(true);
            host.Open();
        }
        catch (CommunicationException ce)
        {
            MessageBox.Show(ce.Message);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

The uri ip is the lan ip of the pc where the service is hosted

The customer is like this

public async void GetProduct()
        {
            try
            {

                var endPointConfiguration = "WSHttpBinding_IGC";//cfg.GetEndPointConfiguration();
                var address = "http://fabianwesling.dynu.com:28621/GC"; //cfg.getAddress();

                ServiceReference1.GCClient service = new ServiceReference1.GCClient(endPointConfiguration, address);
                var bindins = service.Endpoint.Binding;
                if (bindins is NetTcpBinding tcpBinding)
                {
                    tcpBinding.MaxBufferPoolSize = int.MaxValue;
                    tcpBinding.MaxReceivedMessageSize = int.MaxValue;
                    tcpBinding.ReaderQuotas.MaxStringContentLength = int.MaxValue;
                }
                else if (bindins is WSHttpBinding wS)
                {
                    wS.MaxBufferPoolSize = int.MaxValue;
                    wS.MaxReceivedMessageSize = int.MaxValue;
                    wS.ReaderQuotas.MaxStringContentLength = int.MaxValue;
                }

             var result =  await service.GetProductsAsync();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

I opened the ports in the Reuter so that it directs those ports to the pc, and I also opened the ports in the firewall, I also activated the Windows .net framework features. But when I try to connect from the client, it tells me that there was no end listening There must be some concept that I am not understanding, but I cannot identify what it is ... I need your advice, everything is welcome


回答1:


First try to use the browser on the client computer to visit http//fabianwesling.dynu.com:28621/GC to see if the metadata can be accessed normally.If not, the client and server cannot access normally. Modify the hots file on the client computer:

All in all, you must ensure that the client and server are mutually accessible on the Internet.

If it is not a network problem, you can create a simple WCF service for testing to know where the problem is.Here is a very simple example of WCF service:

https://docs.microsoft.com/en-us/dotnet/framework/wcf/getting-started-tutorial



来源:https://stackoverflow.com/questions/63266705/wcf-self-host-to-access-from-internet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!