1

我正在尝试编写一个通用主机进程(WinForm),它托管同一合同的不同 WCF 服务实现。当我运行第一个时,它工作正常,但是当我使用不同的地址启动另一个(并行)时,我会两次使用相同的地址(地址和端口)-> 路径虽然不同..

    private bool InitializeServiceHost()
    {
        bool isInitialized = true;
        try
        {
            Log.InfoFormat("Loading service DLL {0} and class {1}", _dllPath, _serviceClassName);
            var asm = Assembly.LoadFile(_dllPath);
            _service = (IGfnService) asm.CreateInstance(_serviceClassName);
            if (_service == null)
                throw new ApplicationException(string.Format("Could not instansiate {0} from DLL {1}", _serviceClassName, _dllPath));

            _service.Init(_myGuidStr);
            Uri uri = new Uri("net.tcp://localhost:9085/GfnService/" + _myGuidStr);

            var host = new ServiceHost(_service, uri);

            Log.InfoFormat("About to open host, State: {0}, URI: {1} ", host.State, uri);
            host.Open();
            _serviceUri = uri.ToString();
            Log.InfoFormat("Gfn service started successfully, State: {0}, URI: {1} ", host.State, uri);
        }
        catch (Exception ex)
        {
            Log.Error(ex.Message, ex);
            isInitialized = false;
            Application.Exit();
        }
        return isInitialized;
    }

任何帮助将不胜感激....

4

1 回答 1

1

知道了!现在可以了!(感谢所有评论者)

           var host = new ServiceHost(_service);
            Log.Info("Service host generated.");

            ServiceEndpoint serviceEndpoint = host.Description.Endpoints.Find(typeof(IGfnService));
            if (serviceEndpoint == null)
            {
                serviceEndpoint = host.AddServiceEndpoint(typeof(IGfnService), new NetTcpBinding
                {
                    MaxConnections = 10,
                    PortSharingEnabled = true
                }, uri);
                Log.InfoFormat("Endpoint [{0}] added", serviceEndpoint);
            }

诀窍是添加 PortSharingEnabled!所以两个实例可以共享同一个端口!(我之前应该有过,但至少我有机会分享!)

谢谢!

于 2012-06-27T13:28:07.003 回答