简体   繁体   English

WCF 自主机从互联网访问

[英]WCF Self-Host to access from internet

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.我正在尝试创建一项服务以通过 Internet 使用它,但由于某种原因我无法访问它,我需要帮助来查找我所犯的错误。 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 uri ip 是托管服务的电脑的局域网 ip

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.我在 Reuter 中打开了端口,以便将这些端口定向到 PC,我还在防火墙中打开了端口,我还激活了 Windows .net 框架功能。 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但是当我尝试从客户端连接时,它告诉我没有结束监听 一定有一些我不理解的概念,但我无法确定它是什么......我需要你的建议,欢迎一切

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.首先尝试使用客户端电脑上的浏览器访问http//fabianwesling.dynu.com:28621/GC,看看元数据是否可以正常访问。如果不能正常访问,则客户端和服务器无法正常访问。 Modify the hots file on the client computer:修改客户端电脑上的hots文件:

在此处输入图像描述

All in all, you must ensure that the client and server are mutually accessible on the Internet.总而言之,您必须确保客户端和服务器可在 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:如果不是网络问题,可以创建一个简单的WCF服务进行测试,就知道问题出在哪里了。下面是一个非常简单的WCF服务示例:

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

UPDATE更新

This is my remote WCF service:这是我的远程 WCF 服务:

在此处输入图像描述

Baseadress is the WAN IP. Baseadress 是 WAN IP。

Ensure that the local computer can access the WSDL of the remote service through a browser确保本地电脑可以通过浏览器访问远程服务的WSDL

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM