使用相同的合同启动不同的服务主机失败

时间:2012-06-27 12:46:26

标签: wcf

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

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

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

1 个答案:

答案 0 :(得分: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!所以这两个实例可以共享同一个端口! (我之前应该知道,但至少我有机会分享!)

谢谢!