WCF服务控制台托管方法(不使用配置文件)
来源:互联网 发布:js 重置select 编辑:程序博客网 时间:2024/04/30 06:59
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(); } }}
- WCF服务控制台托管方法(不使用配置文件)
- WCF :通过配置文件来托管服务
- 完全不使用配置文件构建和使用WCF服务
- FastReport的WCF托管到Windows服务的配置文件
- 使用控制台程序寄宿wcf服务时,不正确使用Console.Read()方法导致服务寄宿失败
- (4)WCF宿主与服务托管
- WCF服务自托管(Self-Hosting)
- WCF服务的IIS托管(应用程序)
- 托管 WCF 服务
- WCF服务的IIS托管(网站托管)
- WCF系列(二) -- 使用配置文件构建和使用WCF服务
- JQuery使用$.ajax跨域调用winform托管的WCF服务(原创)
- WCF宿主与服务托管
- WCF宿主与服务托管
- WCF宿主与服务托管
- WCF宿主与服务托管
- WCF宿主与服务托管
- WCF 服务客户端不使用安全措施
- 16、backbone.js实践总结的说
- 文件打包源代码
- WINDOWS优化大师揭密
- 17、backbone.js相关资源
- 如何减小与“大牛”的差距
- WCF服务控制台托管方法(不使用配置文件)
- 错误"XML解析错误:未找到元素"的解决方案。
- make prefix
- 又删了
- How can I uninstall altiris agent on workstation?
- libgettext for android is done.
- 《C和指针》阅读总结
- Script to Uninstall ACLIENT from the Machine [Uninstall & Cleanup Script]
- 乔布斯的演讲技巧