• 【转】WCF 取得客户端IP


    简单定义一个服务:
             
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.Text;

    namespace ClientInfoSample
    {
        [ServiceContract]
        
    public interface IService
        
    {
            [OperationContract]
            
    string GetData(string value);
        }

    }



    在建立通道之后按照可以取得:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.Text;
    using System.ServiceModel.Channels; 

    namespace ClientInfoSample
    {
        
    public class MyService : IService
        
    {
            
    public string GetData(string value)
            
    {
                OperationContext context 
    = OperationContext.Current;
                MessageProperties essageProperties 
    = context.IncomingMessageProperties;
                RemoteEndpointMessageProperty endpointProperty 
    =
                messageProperties  [RemoteEndpointMessageProperty.Name]
                    
    as RemoteEndpointMessageProperty;

                
    return string.Format("Hello {0}! Your IP address is {1} and your port is {2}",  value, endpointProperty.Address, endpointProperty.Port);
            }

        }

    }



    config:
    <?xml version="1.0" encoding="utf-8" ?>

    <configuration>

      
    <system.web>

        
    <compilation debug="true" />

      
    </system.web>

      
    <system.serviceModel>

        
    <services>

          
    <service name="ClientInfoSample.MyService" behaviorConfiguration="ClientInfoSample.MyServiceBehavior">

            
    <host>

              
    <baseAddresses>

                
    <add baseAddress = "http://localhost:8731/Design_Time_Addresses/ClientInfoSample/MyService/" />

              
    </baseAddresses>

            
    </host>

            
    <endpoint address ="" binding="wsHttpBinding" contract="ClientInfoSample.IService">

              
    <identity>

                
    <dns value="localhost"/>

              
    </identity>

            
    </endpoint>

            
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>

          
    </service>

        
    </services>

        
    <behaviors>

          
    <serviceBehaviors>

            
    <behavior name="ClientInfoSample.MyServiceBehavior">

              
    <serviceMetadata httpGetEnabled="True"/>

              
    <serviceDebug includeExceptionDetailInFaults="False" />

            
    </behavior>

          
    </serviceBehaviors>

        
    </behaviors>

      
    </system.serviceModel>

    </configuration>

  • 相关阅读:
    MYSQL触发器的使用
    数据库与信息系统经典例题
    【自考】数据结构第六章查找,期末不挂科指南,第10篇
    hdfs/hbase 程序利用Kerberos认证超过ticket_lifetime期限后异常
    mysql必知必会--MySQL简介
    启用CentOS6.5 64位安装时自带的MySQL数据库服务器
    com.mysql.jdbc.Driver 和 com.mysql.cj.jdbc.Driver的区别 serverTimezone设定
    redis的发布订阅
    使用ClouderaManager管理的HBase的RegionServer无法启动(启动失败)的问题
    mysql 的root 用户无法授权,navicat 远程授权提示1044解决方案
  • 原文地址:https://www.cnblogs.com/chenlhuaf/p/1212833.html
Copyright © 2020-2023  润新知