WCF 宿主里面显示客户端信息

来源:互联网 发布:程序员必备生活用品 编辑:程序博客网 时间:2024/06/06 01:52


 废话不多说,一直粘代码。

Business  端代码

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.ServiceModel;namespace WCFBusiness{    [ServiceContract]    public interface IDemo    {        [OperationContract]        string GetMessage(string value);    }}

 

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.ServiceModel;using System.ServiceModel.Channels;namespace WCFBusiness{    public class Demo : IDemo    {        public string GetMessage(string value)        {            OperationContext current = OperationContext.Current;            MessageProperties messageProperties = current.IncomingMessageProperties;            RemoteEndpointMessageProperty endpointProperty =           messageProperties[RemoteEndpointMessageProperty.Name]               as RemoteEndpointMessageProperty;            string clientInfo = "IP:" + endpointProperty.Address + "---- 端口:" + endpointProperty.Port;            if (RecieveMessageEvent != null)            {                RecieveMessageEvent(clientInfo);            }            return string.Format("Hello {0}! Your IP address is {1} and your port is {2}", value, endpointProperty.Address, endpointProperty.Port);        }        public static event Action<string> RecieveMessageEvent;    }}


Service 端

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.ServiceModel;using WCFBusiness;namespace WCFService{    class Program    {        static void Main(string[] args)        {            using (ServiceHost sh = new ServiceHost(typeof(Demo)))            {                try                {                    sh.Open();                    Console.WriteLine("服务成功启动.....");                    WCFBusiness.Demo.RecieveMessageEvent += new Action<string>(Demo_RecieveMessageEvent);                    Console.ReadLine();                    sh.Close();                }                catch (Exception ioe)                {                    Console.WriteLine(ioe.Message);                    Console.ReadLine();                }            }        }        static void Demo_RecieveMessageEvent(string obj)        {            Console.WriteLine(obj+"---------已成功连接");        }    }}


App.config

<?xml version="1.0" encoding="utf-8" ?><configuration>  <system.serviceModel>    <services>      <service name="WCFBusiness.Demo" behaviorConfiguration="demoBehavior">        <endpoint address="net" binding="netTcpBinding" bindingConfiguration="demoBinding" contract="WCFBusiness.IDemo" />        <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />        <host>          <baseAddresses>            <add baseAddress="net.tcp://192.168.18.133:1235/Demo"/>          </baseAddresses>        </host>      </service>    </services>        <behaviors>      <serviceBehaviors>        <behavior name="demoBehavior">          <serviceDebug includeExceptionDetailInFaults="True" />          <serviceMetadata httpGetEnabled="false"  />          <serviceThrottling maxConcurrentSessions="10000" />        </behavior>      </serviceBehaviors>    </behaviors>    <bindings>      <netTcpBinding>        <binding name="demoBinding">          <security mode="None"/>        </binding>      </netTcpBinding>    </bindings>  </system.serviceModel></configuration>


Client 端

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace WCFClient{    class Program    {        static void Main(string[] args)        {            DemoService.DemoClient client = new DemoService.DemoClient();            Console.WriteLine(client.GetMessage("xiao hui"));            Console.ReadLine();        }    }}


 

由于本人一搞技术的,文字表达能力不怎么强,所以说明还是不说,怕说了你们会看不懂,所以才直接粘代码,如果有不懂的可以留言问我。

 

 

 

原创粉丝点击