На просторах Интернета наткнулся на простой пример получения IP: http://nayyeri.net/detect-client-ip-in-wcf-3-5.
В двух словах:
1. У нас есть какой-то сервис. Возьмем самый простой пример:
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);
}
}
2. Переходим к реализации интерфейса. Так же, в методе GetData реализуем получение IP:
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 messageProperties = 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);
}
}
}
Все! Теперь в качестве ответа на вызов функции GetData, мы получаем Строку с приветствием, а так же свой IP-адрес и порт.
Комментариев нет:
Отправить комментарий