簡體   English   中英

WCF 自主機從互聯網訪問

[英]WCF Self-Host to access from internet

我正在嘗試創建一項服務以通過 Internet 使用它,但由於某種原因我無法訪問它,我需要幫助來查找我所犯的錯誤。 我把代碼留給你看。

 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");
                }
            }
        }
    }

托管

        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);
        }
    }

uri ip 是托管服務的電腦的局域網 ip

客戶是這樣的

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);
            }
        }

我在 Reuter 中打開了端口,以便將這些端口定向到 PC,我還在防火牆中打開了端口,我還激活了 Windows .net 框架功能。 但是當我嘗試從客戶端連接時,它告訴我沒有結束監聽 一定有一些我不理解的概念,但我無法確定它是什么......我需要你的建議,歡迎一切

首先嘗試使用客戶端電腦上的瀏覽器訪問http//fabianwesling.dynu.com:28621/GC,看看元數據是否可以正常訪問。如果不能正常訪問,則客戶端和服務器無法正常訪問。 修改客戶端電腦上的hots文件:

在此處輸入圖像描述

總而言之,您必須確保客戶端和服務器可在 Internet 上相互訪問。

如果不是網絡問題,可以創建一個簡單的WCF服務進行測試,就知道問題出在哪里了。下面是一個非常簡單的WCF服務示例:

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

更新

這是我的遠程 WCF 服務:

在此處輸入圖像描述

Baseadress 是 WAN IP。

確保本地電腦可以通過瀏覽器訪問遠程服務的WSDL

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM