0

我是 WCF 的新手。我知道如何以 Windows 形式托管 wcf 服务。现在我开发了一个具有 .svc 文件的小型 wcf 服务。我想以 win 形式托管这个 svc 文件。所以只想知道过程是相同的还是不同的?

这是我的 svc 文件标记

<%@ ServiceHost Language="C#" Debug="true" 
Service="Services.ChatService" CodeBehind="ChatService.svc.cs" %>

这是 svc 文件中的小代码 文件后面的代码

namespace Services
{
    /// <summary>
    /// Implements the chat service interface.
    /// </summary>
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, 
        ConcurrencyMode = ConcurrencyMode.Multiple)]
    public class ChatService : IChatService
    {
        private readonly Dictionary<Guid, IChatServiceCallback> clients = 
            new Dictionary<Guid, IChatServiceCallback>();

        #region IChatService

        Guid IChatService.Subscribe()
        {
            IChatServiceCallback callback =
                OperationContext.Current.GetCallbackChannel<IChatServiceCallback>();

            Guid clientId = Guid.NewGuid();

            if (callback != null)
            {
                lock (clients)
                {
                    clients.Add(clientId, callback);
                }
            }

            return clientId;
        }

        void IChatService.Unsubscribe(Guid clientId)
        {
            lock (clients)
            {
                if (clients.ContainsKey(clientId))
                {
                    clients.Remove(clientId);
                }
            }
        }

        void IChatService.KeepConnection()
        {
            // Do nothing.
        }

        void IChatService.SendMessage(Guid clientId, string message)
        {
            BroadcastMessage(clientId, message);
        }

        #endregion

        /// <summary>
        /// Notifies the clients of messages.
        /// </summary>
        /// <param name="clientId">Identifies the client that sent the message.</param>
        /// <param name="message">The message to be sent to all connected clients.</param>
        private void BroadcastMessage(Guid clientId, string message)
        {
            // Call each client's callback method
            ThreadPool.QueueUserWorkItem
            (
                delegate
                {
                    lock (clients)
                    {
                        List<Guid> disconnectedClientGuids = new List<Guid>();

                        foreach (KeyValuePair<Guid, IChatServiceCallback> client in clients)
                        {
                            try
                            {
                                client.Value.HandleMessage(message);
                            }
                            catch (Exception)
                            {
                                // TODO: Better to catch specific exception types.                     

                                // If a timeout exception occurred, it means that the server
                                // can't connect to the client. It might be because of a network
                                // error, or the client was closed  prematurely due to an exception or
                                // and was unable to unregister from the server. In any case, we 
                                // must remove the client from the list of clients.

                                // Another type of exception that might occur is that the communication
                                // object is aborted, or is closed.

                                // Mark the key for deletion. We will delete the client after the 
                                // for-loop because using foreach construct makes the clients collection
                                // non-modifiable while in the loop.
                                disconnectedClientGuids.Add(client.Key);
                            }
                        }

                        foreach (Guid clientGuid in disconnectedClientGuids)
                        {
                            clients.Remove(clientGuid);
                        }
                    }
                }
            );
        }
    }
}

这是绑定信息

<service behaviorConfiguration="Services.ChatServiceBehavior" name="Services.ChatService">
                <endpoint address="" binding="wsDualHttpBinding" bindingConfiguration="WSDualHttpBinding_IChatService" contract="Services.IChatService">
                    <identity>
                        <dns value="localhost"/>
                    </identity>
                </endpoint>
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
            </service>

这里有两个端点,一个用于 wsDualHttpBinding,另一个用于 mex ,所以现在我的 mex 端点是

http://localhost:49722/ChatService.svc?wsdl

现在我想添加另一个 tcp 端点并用两个端点公开这个服务。所以只要告诉我我需要为 tcp 端点写什么,当我添加 tcp 端点时,tcp 的 mex 端点会是什么,因为我希望该用户可以使用两个 url 中的任何一个创建代理,一个是 http url,另一个是 tcp url。所以我需要在这里为 tcp 添加 mex 吗?

请指导我。谢谢

4

1 回答 1

1

您必须手动启动主机

按照 msdn 链接http://msdn.microsoft.com/en-us/library/system.servicemodel.servicehost.aspx


已编辑

ServiceHost _serviceHost;
public void Start(Type type)
{
    _serviceHost = new ServiceHost(type);
    _serviceHost.Open();
}
于 2013-01-03T20:03:46.733 回答