WCF服务控制台托管方法(不使用配置文件)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;

namespace ConsoleApplication1
{


    class Program
    {
        static void Main(string[] args)
        {
            using (ServiceHost host = new ServiceHost(typeof(ChatService), new Uri("http://localhost:8000/WCFServerLibrary/Service1/"), new Uri("net.tcp://localhost:8888/WCFServerLibrary/Service1/")))
            {
                WSHttpBinding wsbinding = new WSHttpBinding();
                wsbinding.Name = "NoneSecurity";
                wsbinding.MaxBufferPoolSize = 12000000;
                wsbinding.MaxReceivedMessageSize = 12000000;
                wsbinding.UseDefaultWebProxy = false;
                WSHttpSecurity security = new WSHttpSecurity();
                security.Mode = SecurityMode.None;
                wsbinding.Security = security;

                //wshttp绑定   
                ServiceEndpoint sed = host.AddServiceEndpoint(typeof(IChatService), wsbinding, "");

                //设置IService1的ServiceContract特性的SessionMode
                sed.Contract.SessionMode = SessionMode.Allowed;

                ServiceBehaviorAttribute behaviorAttr = host.Description.Behaviors.Find<ServiceBehaviorAttribute>();
                if (behaviorAttr == null)
                {
                    behaviorAttr = new ServiceBehaviorAttribute();
                }
                //设置服务类Service1的ServiceBehavior特性的ConcurrencyMode和InstanceContextMode
                behaviorAttr.ConcurrencyMode = ConcurrencyMode.Single;
                behaviorAttr.InstanceContextMode = InstanceContextMode.PerCall;


                //tcp绑定
                NetTcpBinding myBinding = new NetTcpBinding();
                myBinding.Security.Mode = SecurityMode.None;
                myBinding.Security.Transport.ClientCredentialType = TcpClientCredentialType.None;
                host.AddServiceEndpoint(typeof(IChatService), myBinding, "");

                //元数据mex绑定 
                ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
                host.Description.Behaviors.Add(behavior);
                host.AddServiceEndpoint(typeof(IMetadataExchange),MetadataExchangeBindings.CreateMexHttpBinding(), "mex");

                host.Open();
                Console.WriteLine("ConsoleHost 服务开启……");
                Console.ReadLine();
            }

        } 

    }
}

其中ChatService是服务实例,IChatService是服务契约,可自定义

启动后在http://localhost:8000/WCFServerLibrary/Service1/mex可导入服务

 

TCP服务托管完整例子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
using ConsoleApplicationWCF.ServiceReference1;
namespace ConsoleApplicationWCF
{
    [ServiceContract]
    public interface IGetIdentity
    {
        [OperationContract]
        string Get(string ClientIdentity);
    }

    public class GetIdentity : IGetIdentity
    {

        public string Get(string ClientIdentity)
        {
            return ("客户端Identity是 '" + ClientIdentity + "'");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            NetTcpBinding myBinding = new NetTcpBinding();
            myBinding.Security.Mode = SecurityMode.None;
            myBinding.Security.Transport.ClientCredentialType = TcpClientCredentialType.None;

            Uri baseAddress = new Uri("net.tcp://localhost:8056/WCFService/");

            var   myServiceHost = new ServiceHost(typeof(GetIdentity), baseAddress);

            ServiceEndpoint myServiceEndpoint = myServiceHost.AddServiceEndpoint
                (typeof(IGetIdentity), myBinding, "GetIdentity");

            ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
            behavior.HttpGetEnabled = true;
            behavior.HttpGetUrl = new Uri("http://localhost:8057/mex");
            myServiceHost.Description.Behaviors.Add(behavior);

            
            myServiceHost.Open();

            Console.WriteLine("Service started!");
            Console.ReadLine();
            myServiceHost.Close();

        }
    }
}


 


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值