使用OperationContract中未提及的方法
问题描述:
我有一个WCF服务。有两种方法,Get和Save。我只想将Get方法公开给将要使用服务的第三方,而我的应用程序应该能够同时使用Get和Save。使用OperationContract中未提及的方法
有没有办法消耗OperationContract中的方法?我正在考虑验证请求的主机名并仅在它是我的应用程序的主机名时授予访问权。
答
为什么不创建第二个ServiceContract
既Get
和Set
为OperationContracts
?然后你可以锁定谁可以得到这个第二份合同。
[ServiceContract]
public interface IFoo
{
[OperationContract]
void Get();
}
[ServiceContract]
public interface IFooInternal : IFoo
{
[OperationContract]
void Set();
}
答
这里是要找出主机IP地址代码:
string GetAddressAsString()
{
RemoteEndpointMessageProperty clientEndpoint =
OperationContext.Current.IncomingMessageProperties[
RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
if (clientEndpoint != null)
{
return String.Format("{0}:{1}", clientEndpoint.Address, clientEndpoint.Port);
}
return "Failed to identify address";
}