Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 仅将WCF服务访问限制为使用控制台应用程序托管的本地主机_C#_Wcf_Wcf Binding_Wcf Behaviour - Fatal编程技术网

C# 仅将WCF服务访问限制为使用控制台应用程序托管的本地主机

C# 仅将WCF服务访问限制为使用控制台应用程序托管的本地主机,c#,wcf,wcf-binding,wcf-behaviour,C#,Wcf,Wcf Binding,Wcf Behaviour,我对WCF非常陌生。我有一个使用控制台应用程序托管的WCF服务,但WCF需要从托管在同一台机器上的C#webservice调用。那么,如何将端点访问限制为环回ip,即127.0.0.1 现在我可以访问托管在不同机器上的WCF服务端点(比如10.X.X.X)。例如,我可以键入并获得响应。此url应受到限制。我的要求只有http://localhost/api/v1/getStatus应该能够从承载的WCF服务获取响应。根据您的特定场景,您可以使用命名管道 在您提供的链接中,IPFilter是一个自

我对WCF非常陌生。我有一个使用控制台应用程序托管的WCF服务,但WCF需要从托管在同一台机器上的C#webservice调用。那么,如何将端点访问限制为环回ip,即127.0.0.1


现在我可以访问托管在不同机器上的WCF服务端点(比如10.X.X.X)。例如,我可以键入并获得响应。此url应受到限制。我的要求只有http://localhost/api/v1/getStatus应该能够从承载的WCF服务获取响应。

根据您的特定场景,您可以使用命名管道

在您提供的链接中,IPFilter是一个自定义节点,它实现IDispatchMessageInspector接口来拦截IP。这是我的演示:

   public class ServerMessageLogger : IDispatchMessageInspector
{
    public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
    {
        return null;
    }

    public void BeforeSendReply(ref Message reply, object correlationState)
    {
        OperationContext context = OperationContext.Current;
        MessageProperties messageProperties = context.IncomingMessageProperties;
        RemoteEndpointMessageProperty endpointProperty =
          messageProperties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
        if (endpointProperty.Address.Equals("::1"))
        {
            Console.WriteLine("OK");
        }
        else
        {
            reply = null;
        }
    }
}
我们需要实现IDispatchMessageInspector接口。当服务器向客户端发送响应时,首先确定客户端的IP地址是否为localhost。如果不是localhost,服务器将返回空响应

 [AttributeUsage(AttributeTargets.Interface | AttributeTargets.Class, AllowMultiple = false)]
    public class CustContractBehaviorAttribute : Attribute, IContractBehavior
    {
        public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
        {
            return;
        }

        public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
            return;
        }

        public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
        {
            dispatchRuntime.MessageInspectors.Add(new ServerMessageLogger());
        }

        public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
        {
            return;
        }
    }
然后我们需要将ServerMessageLogger添加到服务的行为中


最后,您需要将CustContractBehavior应用于服务。

再次不确定,但在WCF应用程序的app.config文件中,我找不到任何与管道有关的内容。但我可以看到这一点——因此,如何限制环回地址?是的,因为现在您正在使用HTTP绑定。按照我的建议,您必须更改绑定配置。您可以查看以下内容:(尤其是VoteCoffee的答案)并尝试相应地调整服务器和客户端配置。@debanka在Lukasz的答案和注释中添加了WCF下的管道,这正是您想要的。不要与支持本地LAN的本机命名管道混淆。不知道为什么微软会阻止它,可能是为了阻止人们在互联网上使用NP;)@MickyD我仍然希望使用现有的Http绑定。我看了这个()(Steve的答案),但是当我尝试做同样的事情时,我得到了一个错误,元素行为有无效的子元素IPFilter。那么,你对此有何想法?如何解决这个问题?@debanka 1)链接与HTTP有什么关系?2) 更重要的是,当您只需要本地主机时,为什么要这样做?只使用命名管道要容易得多,因为根据定义,网络是阻塞的。不需要IP过滤。无论是否命名为管道,您使用的WCF传输对您的C#代码没有任何影响;HTTP;TCP;或MSMQ。无需更改C#。