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


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

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();
        }
    }
}


 

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

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值