【问题标题】:How to Host SVC file in win form apps WCF如何在 Win 表单应用程序 WCF 中托管 SVC 文件
【发布时间】:2013-01-03 19:53:30
【问题描述】:

我是 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 吗?

请指导我。谢谢

【问题讨论】:

  • 我在谷歌上搜索了很多关于如何在 win 应用程序中托管 svc 文件的信息,但没有找到 svc 文件示例。为什么我使用 svc 文件,因为 svc 文件可以托管在 IIS 中。如果我可以在 win 应用程序中托管它,那么我可以添加 tcp 端点。

标签: wcf


【解决方案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();
}

【讨论】:

  • 我不是在使用 wcf 服务库,而是在使用生成 svc 文件的 wcf 服务应用程序。你给了我在 win 或控制台应用程序中托管 wcf 服务库 dll 的链接。我知道如何在 IIS 中托管 svc 文件,但我想知道如何在 win 应用程序中借助 ServiceHost 类托管 svc 文件。您提供的链接我认为它适用于那些使用 wcf 服务库而不是 wcf 服务应用程序模板的人。如果你给我正确的文章链接,这将是很大的帮助。谢谢
  • 默认情况下不能通过ServiceHost类来托管svc文件,你可以自己编写类
  • 为 svc 文件编写反序列化器,您将拥有包含以下字段的类:语言、调试、服务、代码隐藏。通过反射从服务中获取类型并在我的答案中使用添加的代码
  • 你说...为 svc 文件编写反序列化器。你能给我一些代码,它可以作为 svc 文件的反序列​​化器。我真的不明白他们指导我的方式,因为我是 WCF 的新手。谢谢
  • 按正则表达式解析并填充类,f.e. Regex.Match(sourseSrting, @"(?
猜你喜欢
  • 2014-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-29
  • 2017-01-20
  • 1970-01-01
相关资源
最近更新 更多