WCF应用方法比较灵活,可以为开发人员创造一个跨平台的解决方案。我们在这里会为大家详细讲解一下有关WCF获取客户端IP的操作方法,希望对这方面又需要的朋友们可以从中获得一些帮助。
WCF获取客户端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);
- }
- }
- }
- 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);
- }
- }
【编辑推荐】